Compressing Java JSON Objects

I am looking for a Java library to convert my domain objects to flattened JSON

eg.

public class Person { String name Address homeAddress } public class Address { String street String zip } JSON: {name:'John', homeAddress_street: '123 Street', homeAddress_zip: 'xxxxx'} 

I looked at XStream, Eclipse MOXy, FlexJSON, JSON-lib and gson

My goal is to get rid of my json shell classes and minimize the code. I would like to have a generic service that will accept any class of a domain model that I have and get a json representation without having to write xml descriptors or any custom converters for each type of model. For my models, a depth of 1 level is enough. I did not find a simple general solution using annotations or built-in functions in the above libraries, but I probably did not notice them. Is there a non-intrusive library that can do this? or maybe the one I listed? I use Hibernate so the library can deal with CGLib Proxies

+4
source share
2 answers

Note. I am an EclipseLink JAXB (MOXy) and a member of the JAXB Group (JSR-222) .

The following is an example of how this can be done using MOXy using the @XmlPath extension.

Person

 package forum7652387; import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlAccessorType(XmlAccessType.FIELD) public class Person { String name; @XmlPath(".") Address homeAddress; } 

Address

 package forum7652387; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class Address { @XmlElement(name="homeAddress_street") String street; @XmlElement(name="homeAddress_zip") String zip; } 

jaxb.properties

To specify MOXy as the JAXB provider, you need to add a file named jaxb.properties in the same package as the domain classes, with the following entry:

 javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

Demo

 package forum7652387; import java.io.StringReader; import javax.xml.bind.*; import javax.xml.transform.stream.StreamSource; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setProperty("eclipselink.media-type", "application/json"); unmarshaller.setProperty("eclipselink.json.include-root", false); String jsonString = "{\"name\":\"John\", \"homeAddress_street\":\"123 Street\", \"homeAddress_zip\":\"xxxxx\"}"; StreamSource jsonSource = new StreamSource(new StringReader(jsonString)); Person person = unmarshaller.unmarshal(jsonSource, Person.class).getValue(); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty("eclipselink.media-type", "application/json"); marshaller.setProperty("eclipselink.json.include-root", false); marshaller.marshal(person, System.out); } } 

Output

  {"name" : "John", "homeAddress_street" : "123 Street", "homeAddress_zip" : "xxxxx"} 

Additional Information

+2
source

I used GSON very briefly and I think it does what you are looking for. Studying this, I knocked out the following ridiculous simple example to confirm that it was as simple as I needed; POJO first:

 import java.util.List; public class Member { private String name; private int age; private List<String> stuff; public Member() { } public String getName() { return name; } public void setName( String name ) { this.name = name; } public int getAge() { return age; } public void setAge( int age ) { this.age = age; } public List<String> getStuff() { return stuff; } public void setStuff( List<String> stuff ) { this.stuff = stuff; } } 

Then the working class:

 import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class Main { public static void main( String[] args ) { Main m = new Main(); m.execute( args ); } private void execute( String[] args ) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); Member member = new Member(); List<String> stuff = new ArrayList<String>(); stuff.add( "shoes" ); stuff.add( "hat" ); member.setStuff( stuff ); member.setName( "Bert" ); member.setAge( 21 ); String output = gson.toJson( member, Member.class ); log( output ); Member member2 = gson.fromJson( output, Member.class ); log(member2.getName()); } private void log( String text ) { System.out.println( text ); } } 

No XML descriptors or custom converters are required. That's what you need?

0
source

All Articles