public final class Genson extends Object
GensonBuilder
class to have control over Gensons configuration. Genson genson = new Genson(); String json = genson.serialize(new int[] { 1, 2, 3 }); int[] arrayOfInt = genson.deserialize(json, int[].class); // (multi-dimensional arrays are also supported) // genson can also deserialize primitive types without class information Number[] arrayOfNumber = genson.deserialize("[1, 2.03, 8877463]", Number[].class); // or even Object[] arrayOfUnknownTypes = genson .deserialize("[1, false, null, 4.05, \"hey this is a string!\"]", Object[].class); // it can also deserialize json objects of unknown types to standard java types such as Map, Array, Long etc. Map<String, Object> map = (Map<String, Object>) genson.deserialize("{\"foo\":1232}", Object.class);Every object serialized with Genson, can be deserialized back into its concrete type! Just enable it via the builder
GensonBuilder.useClassMetadata(boolean)
.
You can also deserialize to objects that don't provide a default constructor with no arguments
GensonBuilder.useConstructorWithArguments(boolean)
.Type | Property and Description |
---|---|
boolean |
failOnMissing |
Modifier and Type | Class and Description |
---|---|
static class |
Genson.Builder
Deprecated.
use GensonBuilder
|
Constructor and Description |
---|
Genson()
The default constructor will use the default configuration provided by the
GensonBuilder . |
Genson(Factory<Converter<?>> converterFactory,
BeanDescriptorProvider beanDescProvider,
boolean skipNull,
boolean htmlSafe,
Map<String,Class<?>> classAliases,
boolean withClassMetadata,
boolean strictDoubleParse,
boolean indent,
boolean withMetadata,
boolean failOnMissingProperty,
Map<Class<?>,Object> defaultValues,
RuntimePropertyFilter runtimePropertyFilter)
Instead of using this constructor you should use
GensonBuilder . |
Modifier and Type | Method and Description |
---|---|
<T> String |
aliasFor(Class<T> clazz)
Searches if an alias has been registered for clazz.
|
Class<?> |
classFor(String alias)
Searches for the class matching this alias, if none will try to use the alias as the class
name.
|
ObjectReader |
createReader(byte[] in) |
ObjectReader |
createReader(InputStream is)
Creates a new ObjectReader with this Genson instance configuration and tries to detect the encoding
from the stream content.
|
ObjectReader |
createReader(InputStream is,
Charset charset)
Creates a new ObjectReader with this Genson instance configuration.
|
ObjectReader |
createReader(Reader reader)
Creates a new ObjectReader with this Genson instance configuration.
|
ObjectWriter |
createWriter(OutputStream os)
Creates a new ObjectWriter with this Genson instance configuration and default encoding to
UTF8.
|
ObjectWriter |
createWriter(OutputStream os,
Charset charset)
Creates a new ObjectWriter with this Genson instance configuration.
|
ObjectWriter |
createWriter(Writer writer)
Creates a new ObjectWriter with this Genson instance configuration.
|
<T> T |
defaultValue(Class<T> clazz) |
<T> T |
deserialize(byte[] input,
Class<T> toType)
Deserializes the incoming json byte array into an instance of T.
|
<T> T |
deserialize(byte[] input,
GenericType<T> toType)
Deserializes the incoming json byte array into an instance of T.
|
<T> T |
deserialize(GenericType<T> type,
ObjectReader reader,
Context ctx) |
<T> T |
deserialize(GenericType<T> type,
Reader reader,
Class<? extends BeanView<?>>... withViews) |
<T> T |
deserialize(InputStream input,
Class<T> toType)
Deserializes the incoming json stream into an instance of T.
|
<T> T |
deserialize(InputStream input,
GenericType<T> toType)
Deserializes the incoming json stream into an instance of T.
|
<T> T |
deserialize(Reader reader,
Class<T> toType)
Deserializes the incoming json stream into an instance of T.
|
<T> T |
deserialize(Reader reader,
GenericType<T> toType)
Deserializes the incoming json stream into an instance of T.
|
<T> T |
deserialize(String fromSource,
Class<T> toClass)
Deserializes fromSource String into an instance of toClass.
|
<T> T |
deserialize(String fromSource,
Class<T> toType,
Class<? extends BeanView<?>>... withViews) |
<T> T |
deserialize(String fromSource,
GenericType<T> toType)
Deserializes to an instance of T.
|
<T> T |
deserialize(String fromSource,
GenericType<T> toType,
Class<? extends BeanView<?>>... withViews) |
<T> T |
deserializeInto(byte[] jsonBytes,
T object) |
<T> T |
deserializeInto(InputStream is,
T object) |
<T> T |
deserializeInto(ObjectReader reader,
T object,
Context ctx)
Deserializes the stream in the existing object.
|
<T> T |
deserializeInto(Reader reader,
T object) |
<T> T |
deserializeInto(String json,
T object) |
<T> Iterator<T> |
deserializeValues(InputStream is,
Class<T> type) |
<T> Iterator<T> |
deserializeValues(ObjectReader reader,
GenericType<T> type)
This can be used to deserialize in an efficient streaming fashion a sequence of objects.
|
boolean |
failOnMissingProperty() |
BeanDescriptorProvider |
getBeanDescriptorProvider() |
boolean |
isHtmlSafe() |
boolean |
isSkipNull() |
boolean |
isWithClassMetadata() |
<T> Converter<T> |
provideConverter(Type forType)
Provides an instance of Converter capable of handling objects of type forType.
|
RuntimePropertyFilter |
runtimePropertyFilter() |
String |
serialize(Object object)
Serializes the object into a json string.
|
String |
serialize(Object object,
Class<? extends BeanView<?>> firstView,
Class<? extends BeanView<?>>... withViews)
Serializes the object using the specified BeanViews.
|
String |
serialize(Object object,
GenericType<?> type)
Serializes the object using the type of GenericType instead of using its runtime type.
|
void |
serialize(Object object,
ObjectWriter writer,
Context ctx)
Serializes this object and writes its representation to writer.
|
void |
serialize(Object object,
OutputStream output)
Serializes this object to the passed OutputStream, as Genson did not instantiate it, you are
responsible of calling close on it.
|
void |
serialize(Object object,
Type type,
ObjectWriter writer,
Context ctx)
Serializes this object and writes its representation to writer.
|
void |
serialize(Object object,
Writer writer)
Serializes this object to the passed Writer, as Genson did not instantiate it, you are
responsible of calling close on it.
|
byte[] |
serializeBytes(Object object)
Serializes this object to its json form in a byte array.
|
public Genson()
GensonBuilder
.
In most cases using this default constructor will suffice.public Genson(Factory<Converter<?>> converterFactory, BeanDescriptorProvider beanDescProvider, boolean skipNull, boolean htmlSafe, Map<String,Class<?>> classAliases, boolean withClassMetadata, boolean strictDoubleParse, boolean indent, boolean withMetadata, boolean failOnMissingProperty, Map<Class<?>,Object> defaultValues, RuntimePropertyFilter runtimePropertyFilter)
GensonBuilder
.converterFactory
- providing instance of converters.beanDescProvider
- providing instance of BeanDescriptor
used during bean serialization and deserialization.skipNull
- indicates whether null values should be serialized. False by default, null values
will be serialized.htmlSafe
- indicates whether \,<,>,&,= characters should be replaced by their Unicode
representation.classAliases
- association map between classes and their aliases, used if withClassMetadata is
true.withClassMetadata
- indicates whether class name should be serialized and used during deserialization
to determine the type. False by default.strictDoubleParse
- indicates whether to use or not double approximation. If false (by default) it
enables Genson custom double parsing algorithm, that is an approximation of
Double.parse but is a lot faster. If true, Double.parse method will be usead
instead. In most cases you should be fine with Genson algorithm, but if for some
reason you need to have 100% match with Double.parse, then enable strict parsing.indent
- true if outputed json must be indented (pretty printed).withMetadata
- true if ObjectReader instances must be configured with metadata feature enabled.
if withClassMetadata is true withMetadata will be automatically true.failOnMissingProperty
- throw a JsonBindingException when a key in the json stream does not match a property in the Java Class.defaultValues
- contains a mapping from the raw class to the default value that should be used when the property is missing.runtimePropertyFilter
- is used to define what bean properties should be excluded from ser/de at runtime.public <T> Converter<T> provideConverter(Type forType)
forType
- the type for which a converter is needed.JsonBindingException
- if a problem occurs during converters lookup/construction.public String serialize(Object object)
object
- object to be serialized.JsonBindingException
- if there was any kind of error during serialization.JsonStreamException
- if there was a problem during writing of the object to the output.public String serialize(Object object, GenericType<?> type)
object
- object to be serialized.type
- the type of the object to be serialized.JsonBindingException
JsonStreamException
public String serialize(Object object, Class<? extends BeanView<?>> firstView, Class<? extends BeanView<?>>... withViews)
object
- withViews
- the BeanViews to apply during this serialization.JsonBindingException
JsonStreamException
BeanView
public void serialize(Object object, Writer writer)
public void serialize(Object object, OutputStream output)
public byte[] serializeBytes(Object object)
public void serialize(Object object, ObjectWriter writer, Context ctx)
object
- writer
- into which to write the serialized object.JsonBindingException
JsonStreamException
public void serialize(Object object, Type type, ObjectWriter writer, Context ctx)
public <T> T deserialize(String fromSource, Class<T> toClass)
fromSource
- source from which to deserialize.toClass
- type into which to deserialize.JsonBindingException
JsonStreamException
public <T> T deserialize(String fromSource, GenericType<T> toType)
fromSource
- toType
- JsonBindingException
JsonStreamException
GenericType
public <T> T deserialize(Reader reader, GenericType<T> toType)
public <T> T deserialize(Reader reader, Class<T> toType)
public <T> T deserialize(InputStream input, Class<T> toType)
public <T> T deserialize(InputStream input, GenericType<T> toType)
public <T> T deserialize(byte[] input, Class<T> toType)
public <T> T deserialize(byte[] input, GenericType<T> toType)
public <T> T deserialize(String fromSource, GenericType<T> toType, Class<? extends BeanView<?>>... withViews)
public <T> T deserialize(String fromSource, Class<T> toType, Class<? extends BeanView<?>>... withViews)
public <T> T deserialize(GenericType<T> type, Reader reader, Class<? extends BeanView<?>>... withViews)
public <T> T deserialize(GenericType<T> type, ObjectReader reader, Context ctx)
public <T> T deserializeInto(String json, T object)
public <T> T deserializeInto(byte[] jsonBytes, T object)
public <T> T deserializeInto(InputStream is, T object)
public <T> T deserializeInto(Reader reader, T object)
public <T> T deserializeInto(ObjectReader reader, T object, Context ctx)
public <T> Iterator<T> deserializeValues(InputStream is, Class<T> type)
public <T> Iterator<T> deserializeValues(ObjectReader reader, GenericType<T> type)
Genson genson = new Genson(); ObjectReader reader = genson.createReader(json); for (Iterator<LogEntry> it = genson.deserializeValues(reader, GenericType.of(LogEntry.class)); it.hasNext(); ) { // do something LogEntry p = it.next(); }
T
- reader
- an instance of the ObjectReader to use (obtained with genson.createReader(...) for ex.),
note that you are responsible of closing it.type
- to deserialize topublic <T> String aliasFor(Class<T> clazz)
public Class<?> classFor(String alias) throws ClassNotFoundException
alias
- ClassNotFoundException
- thrown if no class has been registered for this alias and the alias it self does
not correspond to the full name of a class.public ObjectWriter createWriter(OutputStream os)
public ObjectWriter createWriter(OutputStream os, Charset charset)
public ObjectWriter createWriter(Writer writer)
public ObjectReader createReader(byte[] in)
createReader(java.io.InputStream)
public ObjectReader createReader(InputStream is)
public ObjectReader createReader(InputStream is, Charset charset)
public ObjectReader createReader(Reader reader)
public boolean isSkipNull()
public boolean isHtmlSafe()
public boolean isWithClassMetadata()
public BeanDescriptorProvider getBeanDescriptorProvider()
public boolean failOnMissingProperty()
public <T> T defaultValue(Class<T> clazz)
public RuntimePropertyFilter runtimePropertyFilter()
Copyright © 2019. All rights reserved.