java - Mapping freeform XML/JSON to Moxy/JAXB annotated class -
i'm trying find way correctly map following xml/json document equivalent jaxb/moxy annotated class.
note model element of document, in example describes person, freeform, i.e. might any kind of xml element/json object, not statically known.
xml document:
<form> <title>person form</title> <model> <person> <name>john</name> <surname>smith</surname> <address> <street>main st.</street> <city>ny</city> <country>usa</country> </address> <person> </model> </form>
equivalent json document:
{ "title":"form title", "model":{ "person":{ "name":"john", "surname":"smith", "address":{ "street":"main st.", "city":"ny", "country":"usa" } } } }
i thought map model field map, values might primitive types or map themselves. mapping enough expressive needs.
i tried play @xmlreadtransformer, @xmlwritetransformer moxy annotations, no success (the record parameter in buildattributevalue null)
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class form { private string title; private model model; ....getters , setters.... } @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class model { @xmlelement @xmlreadtransformer(transformerclass = transformer.class) @xmlwritetransformers({ @xmlwritetransformer(xmlpath = "./*", transformerclass = transformer.class) }) private map<string, object> data; public map<string, object> getdata() { return data; } public void setdata(map<string, object> data) { this.data = data; } public static class transformer implements attributetransformer, fieldtransformer { private abstracttransformationmapping tm; public transformer() { } @override public void initialize(abstracttransformationmapping tm) { this.tm = tm; } @override public map<string, object> buildattributevalue(record r, object o, session s) { map<string, object> data = new hashmap<string, object>(); // todo: ???? return data; } @override public object buildfieldvalue(object arg0, string arg1, session arg2) { // todo return null; } } }
can suggest me proper way of solve problem or different way of modeling "model" field?
is person (or whatever other class have) class available @ runtime? xml/json modified include type attribute indicates class corresponds to? if example here may http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html
your model have
@xmlanyelement private list<parameter> data;
and using parameteradapter in linked example need include type attribute using in xml , json
{ "title" : "form title", "model" : { "person" : { "type" : "test.person", "name" : "john", "surname" : "smith", ... }
or
<form> <title>person form</title> <model> <person type="test.person"> <name>john</name> <surname>smith</surname> ... </form>
Comments
Post a Comment