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!" }
public <T> T deserialize(GenericType<T> type, ObjectReader reader, Context ctx) { Deserializer<T> deser = provideConverter(type.getType()); try { return deser.deserialize(reader, ctx); } catch (Exception e) { throw new JsonBindingException("Could not deserialize to type " + type.getRawClass(), e); } }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of Reader so it will not be closed */ public <T> T deserialize(Reader reader, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(reader), new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of InputStream so it will not be closed. */ public <T> T deserialize(InputStream input, GenericType<T> toType) { return deserialize(toType, createReader(input), new Context(this)); }
/** * @see #deserializeInto(com.owlike.genson.stream.ObjectReader, Object, Context) */ public <T> T deserializeInto(byte[] jsonBytes, T object) { return deserializeInto(createReader(jsonBytes), object, new Context(this)); }
/** * @see #deserializeValues(com.owlike.genson.stream.ObjectReader, GenericType) */ public <T> Iterator<T> deserializeValues(final InputStream is, final Class<T> type) { return deserializeValues(createReader(is), GenericType.of(type)); }
@Override public void serialize(T object, ObjectWriter writer, Context ctx) throws Exception { if (object == null) { throw new JsonBindingException("Serialization of null primitives is forbidden"); } else { wrapped.serialize(object, writer, ctx); } }
/** * Register converter by mapping it to the parameterized type of type argument. * * @param converter to register * @param type of objects this converter handles * @return a reference to this builder. */ public <T> GensonBuilder withConverter(Converter<T> converter, GenericType<? extends T> type) { registerConverter(converter, type.getType()); return this; }
public <T> GensonBuilder withDeserializer(Deserializer<T> deserializer, GenericType<? extends T> type) { registerDeserializer(deserializer, type.getType()); return this; }
/** * Deserializes the incoming json byte array into an instance of T. */ public <T> T deserialize(byte[] input, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(input), new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of Reader so it will not be closed */ public <T> T deserialize(Reader reader, GenericType<T> toType) { return deserialize(toType, createReader(reader), new Context(this)); }
public <T> T deserialize(GenericType<T> type, ObjectReader reader, Context ctx) { Deserializer<T> deser = provideConverter(type.getType()); try { return deser.deserialize(reader, ctx); } catch (Exception e) { throw new JsonBindingException("Could not deserialize to type " + type.getRawClass(), e); } }
/** * @see #deserializeInto(com.owlike.genson.stream.ObjectReader, Object, Context) */ public <T> T deserializeInto(byte[] jsonBytes, T object) { return deserializeInto(createReader(jsonBytes), object, new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of Reader so it will not be closed */ public <T> T deserialize(Reader reader, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(reader), new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of InputStream so it will not be closed. */ public <T> T deserialize(InputStream input, GenericType<T> toType) { return deserialize(toType, createReader(input), new Context(this)); }
/** * @see #deserializeInto(com.owlike.genson.stream.ObjectReader, Object, Context) */ public <T> T deserializeInto(InputStream is, T object) { return deserializeInto(createReader(is), object, new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of InputStream so it will not be closed */ public <T> T deserialize(InputStream input, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(input), new Context(this)); }
/** * Deserializes the incoming json byte array into an instance of T. */ public <T> T deserialize(byte[] input, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(input), new Context(this)); }
/** * Deserializes the incoming json stream into an instance of T. * Genson did not create the instance of InputStream so it will not be closed */ public <T> T deserialize(InputStream input, Class<T> toType) { return deserialize(GenericType.of(toType), createReader(input), new Context(this)); }