deep
deep

Reputation: 1

Unmarshal a json array with eclipse link

I'm trying to unmarshal a json array in a dynamic object using eclipselink moxy and JSON metatdata model.

Here's my Json metadata model

{
   "package-name" : "com.deep.eclipse.link",
   "xml-schema" : {
      "element-form-default" : "QUALIFIED",
      "namespace" : "http://www.com.deep.eclipse.link/data"
   },
   "java-types" : {
      "java-type" : [ {
         "name" : "Data",
         "xml-type" : {
            "prop-order" : "releves"
         }, 
         "xml-root-element" : {},
         "java-attributes" : {
            "xml-element" : [
                {"java-attribute" : "releves", "name" : "releves" , "type" :"com.deep.eclipse.link.Releves", "container-type":"java.util.Collections"}
            ]
         }
      },{
         "name" : "Releves",
         "java-attributes" : {
            "xml-element" : [ 
                {"java-attribute" : "name", "xml-path" : "name/text()"},
                {"java-attribute" : "date", "xml-path" : "date/text()"},
                {"java-attribute" : "data", "xml-path" : "data/text()"}
            ]
         }
      }  ]
   }
}

This is the input JSON input file

{ 
    "releves": [ 
        {
            "name": "aaa",
            "date": "2015-07-11",
            "data": "dddAXsxOkYwMUZTQktEWkFMQVhYWDAwMDAwMDAwMDB9ezI"
        },
        {
            "name": "bbb",
            "date": "2015-07-12",
            "data": "eeAXsxOkYwMUZTQktEWkFMQVhYWDAwMDAwMDAwMDB9ezI"
        }
    ]
}

I want to convert this input file to a dynamic object and i'm only half successful. I get the dynamic Object Releves but I have only the second set of data containing "name":"bbb". What I want to achieve is an Array/List containing both blocs.

Dynamic Object Created

Here' my code

Map<String, Object> properties = new HashMap<>();
properties.put(JAXBContextProperties.MEDIA_TYPE,"application/json");
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "src/main/resources/bank.statements.json");
properties.put(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
ClassLoader myClassLoader =  Thread.currentThread().getContextClassLoader();
        
DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromOXM(myClassLoader, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
Class releves = jc.newDynamicEntity("com.deep.eclipse.link.Releves").getClass();
JAXBElement<DynamicEntity> element = unmarshaller.unmarshal(new StreamSource(new File("src/main/resources/input.bank.statement.json")), releves);
System.out.println(element.getValue());

Thanks in advance for your help

Upvotes: 0

Views: 59

Answers (1)

deep
deep

Reputation: 1

I made some changes and it's working fine. Here is the code

Map<String, Object> properties = new HashMap<>();
properties.put(JAXBContextProperties.MEDIA_TYPE,"application/json");
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "src/main/resources/bank.statements.json");
ClassLoader myClassLoader =  Thread.currentThread().getContextClassLoader();
DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromOXM(myClassLoader, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
Class statement = jc.newDynamicEntity("com.deep.eclipse.link.Statement").getClass();
JAXBElement<DynamicEntity> element = unmarshaller.unmarshal(new StreamSource(new File("src/main/resources/input.bank.statement.json")), statement);

And the OXM metadata file

{
   "package-name" : "com.deep.eclipse.link",
   "xml-schema" : {
      "element-form-default" : "QUALIFIED",
      "namespace" : "http://www.com.deep.eclipse.link/statement"
   },
   "java-types" : {
      "java-type" : [ {
         "name" : "Statement",
         "xml-type" : {
            "prop-order" : "datas"
         }, 
         "java-attributes" : {
            "xml-element" : [
                {"java-attribute" : "datas" , "name": "releves", "type" :"com.deep.eclipse.link.Data","container-type":"java.util.List"}
            ]
         }
      },{
         "name" : "Data",
         "java-attributes" : {
            "xml-element" : [ 
                {"java-attribute" : "name", "xml-path" : "name/text()"},
                {"java-attribute" : "date", "xml-path" : "date/text()"},
                {"java-attribute" : "data", "xml-path" : "data/text()"}
            ]
         }
      }]
   }
}

Now I get a dynamic object containing an ArrayList with two entries inside.

Thanks!

Upvotes: 0

Related Questions