public final class Genson extends Object
Main class of the library. Instances of this class are intended to be reused. You can instantiate it multiple times but it is better to have an instance per configuration so you can benefit of better performances. This class is immutable and thread safe. For more examples have a look at the wiki.
To create a new instance of Genson you can use the default no arg constructor or the
Genson.Builder class to have control over Gensons configuration.
A basic usage of Genson would be:
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!\"]");
// it can also deserialize json objects of unknown type to maps!
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! Have a look
at ClassMetadataConverter for an
explanation and examples.| Modifier and Type | Class and Description |
|---|---|
static class |
Genson.Builder
Use the Builder class when you want to create a custom Genson instance.
|
| Constructor and Description |
|---|
Genson()
The default constructor will use the default configuration provided by the
Genson.Builder. |
Genson(Factory<Converter<?>> converterFactory,
BeanDescriptorProvider beanDescProvider,
Converter<Object> nullConverter,
boolean skipNull,
boolean htmlSafe,
Map<String,Class<?>> classAliases,
boolean withClassMetadata,
boolean strictDoubleParse,
boolean indent,
boolean withMetadata)
Instead of using this constructor you should use
Genson.Builder. |
| 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)
Creates a new ObjectWriter with this Genson instance configuration.
|
ObjectReader |
createReader(InputStream is)
Creates a new ObjectReader with this Genson instance configuration and default encoding to
UTF8.
|
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 |
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) |
BeanDescriptorProvider |
getBeanDescriptorFactory() |
Converter<Object> |
getNullConverter() |
boolean |
isHtmlSafe() |
boolean |
isSkipNull() |
boolean |
isWithClassMetadata() |
<T> Converter<T> |
provideConverter(Type forType)
Provides an instance of Converter capable of handling objects of type forType.
|
String |
serialize(Object object)
Serializes the object into a json string.
|
String |
serialize(Object object,
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,
Class<? extends BeanView<?>>... withViews)
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()
Genson.Builder.
In most cases using this default constructor will suffice.public Genson(Factory<Converter<?>> converterFactory, BeanDescriptorProvider beanDescProvider, Converter<Object> nullConverter, boolean skipNull, boolean htmlSafe, Map<String,Class<?>> classAliases, boolean withClassMetadata, boolean strictDoubleParse, boolean indent, boolean withMetadata)
Genson.Builder.converterFactory - providing instance of converters.beanDescProvider - providing instance of BeanDescriptor used during bean serialization and deserialization.nullConverter - handling null objects. If its serialize/deserialize methods are called you are
sure that it is a null value. This converter is used in
NullConverterskipNull - 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.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.JsonBindingExceptionJsonStreamExceptionpublic String serialize(Object object, Class<? extends BeanView<?>>... withViews)
object - withViews - the BeanViews to apply during this serialization.JsonBindingExceptionJsonStreamExceptionBeanViewpublic 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, Class<? extends BeanView<?>>... withViews)
object - writer - into which to write the serialized object.withViews - JsonBindingExceptionJsonStreamExceptionpublic 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.JsonBindingExceptionJsonStreamExceptionpublic <T> T deserialize(String fromSource, GenericType<T> toType)
fromSource - toType - JsonBindingExceptionJsonStreamExceptionGenericTypepublic <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> 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)
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 getBeanDescriptorFactory()
Copyright © 2014. All Rights Reserved.