public static void main(String[] args) throws IOException { // init class Place place = new Place(); place.setName("World"); Human human = new Human(); human.setMessage("Hi"); human.setPlace(place); // convert to json String jsonString = new Genson().serialize(human); System.out.println("json " + jsonString); //print "json {"message":"Hi","place":{"name":"World"}}" // convert from json Human newHuman = new Genson().deserialize(jsonString, Human.class); newHuman.say(); // print "Hi , World!" }
/** * Serializes this object and writes its representation to writer. As you are providing the * writer instance you also must ensure to call flush and close on it when you are done. * * @param object * @param writer into which to write the serialized object. * @throws com.owlike.genson.JsonBindingException * @throws JsonStreamException */ public void serialize(Object object, ObjectWriter writer, Context ctx) { if (object == null) serializeNull(writer); else serialize(object, object.getClass(), writer, ctx); }
// renames all "fieldOfName" to "toName", excludes from serialization // and deserialization fields named "fieldNamed" and declared in DefinedInClass // and uses fields with all visibility (protected, private, etc) Genson genson = new Genson.Builder().rename("fieldOfName", "toName") .exclude("fieldNamed", DefinedInClass.class) .setFieldFilter(VisibilityFilter.ALL) .create(); genson.serialize(myObject);
/** * Serializes this object to the passed Writer, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, Writer writer) { ObjectWriter objectWriter = createWriter(writer); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
public void serialize(Object obj, ObjectWriter writer, Context ctx) { if (Object.class.equals(obj.getClass())) throw new UnsupportedOperationException( "Serialization of type Object is not supported by default serializers."); ctx.genson.serialize(obj, obj.getClass(), writer, ctx); } }
/** * Serializes this object to the passed OutputStream, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, OutputStream output) { ObjectWriter objectWriter = createWriter(output); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
// this defines aliases for classes, if you don't care of class names being // serialized then just enabled type ser/deser using builder.setWithClassMetadata(true) Genson genson = new Genson.Builder() .addAlias("person", Person.class) .addAlias("other", Some.class) .create(); // serialize using with type information String json = genson.serialize(object); // deserializing to an unkown type based on the type information in the json string genson.deserialize(json, Object.class);
/** * Serializes this object to its json form in a byte array. */ public byte[] serializeBytes(Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectWriter objectWriter = createWriter(baos); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); return baos.toByteArray(); }
// first configure your Genson instance to enable polymorphic types support and // serialization based on concrete types Genson genson = new Genson.Builder() .setWithClassMetadata(true) .setUseRuntimeTypeForSerialization(true) .create(); // and now just use it to serialize/deser String json = genson.serialize(routineData); RoutineData data = genson.deserialize(json, RoutineData.class);
/** * Serializes this object to its json form in a byte array. */ public byte[] serializeBytes(Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectWriter objectWriter = createWriter(baos); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); return baos.toByteArray(); }
public void serialize(T obj, ObjectWriter writer, Context ctx) throws Exception { if (obj != null && !tClass.equals(obj.getClass())) ctx.genson.serialize(obj, obj.getClass(), writer, ctx); else wrapped.serialize(obj, writer, ctx); }
Genson genson = new Genson.Builder().setWithClassMetadata(true) .setWithDebugInfoPropertyNameResolver(true) .create(); String jsonCircle = genson.serialize(circle); String jsonRectangle = genson.serialize(rectangle); System.out.println(jsonCircle); // {"@class":"your.package.Circle","radius":123} System.out.println(jsonRectangle); // {"@class":"your.package.Rectangle","w":20,"h":30} // Throws nothing :) Shape newCircle = genson.deserialize(jsonCircle, Shape.class); Shape newRectangle = genson.deserialize(jsonRectangle, Shape.class);
// tell genson to enable polymorphic types support Genson genson = new Genson.Builder().setWithClassMetadata(true).create(); // json value will be {"@class":"mypackage.LoginRequest", ... other properties ...} String json = genson.serialize(someRequest); // the value of @class property will be used to detect that the concrete type is LoginRequest Request request = genson.deserialize(json, Request.class);
Genson genson = new Genson.Builder() // enables polymorphic types support .setWithClassMetadata(true) // enables no arg support .setWithDebugInfoPropertyNameResolver(true) .create(); // will look like: [{"@class": "com.xxx.SomeConcreteClass", ...}, {"@class": "com.XXX.OtherClass"}] String json = genson.serialize(getListOfAs()); List<A> listOfA = genson.deserialize(json, new GenericType<List<A>>() {});
Base base = new Base(); base.a="a"; base.nested=new Nested(); Genson genson = new Genson.Builder().setWithClassMetadata(true).create(); String json = genson.serialize(base); System.out.println(json.replaceAll("\"@class\":",""));
Genson genson = new Genson.Builder().setUseRuntimeTypeForSerialization(true).create(); String json = genson.serialize(beforeResult); System.out.println(json); GenericType<Wrap<Integer[]>> type = new GenericType<Wrap<Integer[]>>() {}; Wrap<Integer[]> afterResult = genson.deserialize(json, type);
@Override public void serialize(Ts ts, ObjectWriter writer, Context ctx) throws Exception { Map<String, Object> all = new HashMap<>(); all.put("ts", ts.getTs()); if (!ts.isEmpty()) all.putAll(ts.map()); ctx.genson.serialize(all, writer, ctx); }
Planet p = new Planet(); p.setName("foo"); IPlanet ip = getProxy(IPlanet.class, p); Genson genson = new Genson(); System.out.println(genson.serialize(ip));
@Benchmark @Override public Object genson() { ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); JSON_SOURCE().provider().genson().serialize(JSON_SOURCE().nextPojo(), baos); return baos; }
@Override public void serialize(Page page, ObjectWriter writer, Context ctx) throws Exception { writer.beginObject(); writer.writeNumber("pageNo", page.getNumber()) .writeNumber("pageSize", page.getCapacity()) .writeNumber("count", page.getTotalCount()) .writeName("rows"); ctx.genson.serialize(page.getRows(), writer, ctx); writer.endObject(); }