discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

support basic yaml parsing #327

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
other libraries that support yaml are extremely bad at the things you guys are 
good at. 

for example, you guys are very good at handling injecting data into classes 
with custom constructors, coercing generic types, intuitive serializers, 
deserializers.

I'm not necessarily asking for advanced yaml, with munging, etc.  mainly a 
gson-like deserializer for yaml which is as clean as you current code is, and 
clearly benefits from all the core stuff in gson that isn't json dependent.

Here's an example of what we otherwise have to deal with:

   class SelectiveConstructor extends Constructor {
      public SelectiveConstructor() {
         // define a custom way to create a mapping node
         yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
      }

      class MyPersistentObjectConstruct extends Constructor.ConstructMapping {
         @Override
         protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
            Class type = node.getType();
            if (type.equals(MyPersistentObject.class)) {
               // create a map
               Map map = constructMapping(node);
               String id = (String) map.get("id");
               return new MyPersistentObject(id, 17);
            } else {
               // create JavaBean
               return super.constructJavaBean2ndStep(node, object);
            }
         }
      }
   }

   public void testConstructor() throws IOException {
      Yaml yaml = new Yaml(new SelectiveConstructor());
      List<?> data = (List<?>) yaml.load("- 1\n- 2\n- !!examples.MyPersistentObject {amount: 222, id: persistent}");
      // System.out.println(data);
      assertEquals(3, data.size());
      MyPersistentObject myObject = (MyPersistentObject) data.get(2);
      assertEquals(17, myObject.getAmount());
      assertEquals("persistent", myObject.getId());
   }

Original issue reported on code.google.com by adrian.f...@gmail.com on 15 May 2011 at 4:08

GoogleCodeExporter commented 9 years ago
Yaml is outside of the scope of GSON. If you'd like to create a GSON-inspired 
YAML project, that would be great!

Original comment by limpbizkit on 1 Jul 2011 at 9:38

GoogleCodeExporter commented 9 years ago
worth a shot :)  thx anyway

Original comment by adrian.f...@gmail.com on 1 Jul 2011 at 10:14