Skip to main content

Deserialize nested class using yamlbeans



I'm using yamlbeans to deserialize yaml files into Java objects. This works fine as long as I only have one class. The problem is when I want to nest a field, I am forced to specify the nested class in the yaml description.





Single class example:





Java:







public class MessageField {

public String name;

public String type;

public int length;

public String comment;

}







yaml:







name: field1

type: int

length: 4

comment: first field

---

name: field2

type: string

length: 16

comment: second field

---







Multiple classes (requires !com.mylib.VariableField in yaml file)





Java:







public class MessageField {

public String name;

public String type;

public int length;

public String comment;

public List<VariableField> variableFields;

}

public class VariableField extends MessageField{

public int id;

}







yaml:







name: field3

type: short

length: 2

comment:

variableFields:

- !com.mylib.VariableField

id: 1

name: nestedField 1

type: string

length: -1

comment:

---







The yaml documentation page describes how to deserialize a class by specifying the type when reading the class, which is what I do for my top level class:







YamlReader reader = new YamlReader(new FileReader("sample.yml"));

MessageField mf = reader.read(MessageField.class);







This correctly parses my fields for the top level class, but does not allow me to avoid the !com.mylib.VariableField identifier for my nested class. I'm trying to figure out if there is any way to change the Java code so that the yaml files are not required to know the class name.


Comments

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.