Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support basic yaml parsing #327

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments
Closed

support basic yaml parsing #327

GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 15 May 2011 at 4:08

@prosavage
Copy link

Will do this soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants