Class JsonBeanEncoder
- java.lang.Object
-
- org.jdrupes.json.JsonCodec
-
- org.jdrupes.json.JsonBeanEncoder
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class JsonBeanEncoder extends JsonCodec implements Flushable, Closeable
Encoder for converting a Java object graph to JSON.Objects may be arrays, collections, maps and JavaBeans.
Arrays and collections are converted to JSON arrays. The type information is lost. Maps and JavaBeans are converted to JSON objects.
The generated JSON objects can have an additional key/value pair with key “class” and a class name. The class information is generated only if it is needed, i.e. if it cannot be derived from the containing object.
Given the following classes:
public static class Person { private String name; private int age; private PhoneNumber[] numbers; 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 PhoneNumber[] getNumbers() { return numbers; } public void setNumbers(PhoneNumber[] numbers) { this.numbers = numbers; } } public static class PhoneNumber { private String name; private String number; public PhoneNumber() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } public static class SpecialNumber extends PhoneNumber { }A serialization result may look like this:
{ "age": 42, "name": "Simon Sample", "numbers": [ { "name": "Home", "number": "06751 51 56 57" }, { "class": "test.json.SpecialNumber", "name": "Work", "number": "030 77 35 44" } ] }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonBeanEncoderaddAlias(Class<?> clazz, String alias)Add an alias for the given class.JsonBeanEncoderaddExcluded(String className)Add a type to excude from encoding, usually because it cannot be converted to JSON.voidclose()static JsonBeanEncodercreate()Create a new encoder using a defaultJsonGeneratorthat writes to an internally createdStringWriter.static JsonBeanEncodercreate(com.fasterxml.jackson.core.JsonGenerator generator)Create a new encoder using the givenJsonGenerator.static JsonBeanEncodercreate(Writer out)Create a new encoder using a defaultJsonGenerator.voidflush()JsonBeanEncoderomitClass()Configure the encoder to not generate theclassinformation even when needed to properly restore the Object graph.StringtoJson()Returns the text written to the output.JsonBeanEncoderwriteArray(Object... items)JsonBeanEncoderwriteObject(Object obj)-
Methods inherited from class org.jdrupes.json.JsonCodec
clearCaches, defaultFactory, findBeanInfo, findPropertyEditor
-
-
-
-
Method Detail
-
addAlias
public JsonBeanEncoder addAlias(Class<?> clazz, String alias)
Description copied from class:JsonCodecAdd an alias for the given class.If defined, the alias will be used instead of the class name by the encoder.
-
omitClass
public JsonBeanEncoder omitClass()
Configure the encoder to not generate theclassinformation even when needed to properly restore the Object graph.While this contradicts the initial objective to provide JSON persistence for JavaBeans, this is a valid option if the generated JSON is used for transferring information to an environment where the information provided by
classisn’t useful.- Returns:
- the encoder for easy chaining
-
addExcluded
public JsonBeanEncoder addExcluded(String className)
Add a type to excude from encoding, usually because it cannot be converted to JSON.Properties of such types should be marked as
Transient. However, sometimes base types don’t follow the rules.- Parameters:
className-- Returns:
- the encoder for easy chaining
-
create
public static JsonBeanEncoder create(Writer out)
Create a new encoder using a defaultJsonGenerator.- Parameters:
out- the sink- Returns:
- the encoder
-
create
public static JsonBeanEncoder create()
Create a new encoder using a defaultJsonGeneratorthat writes to an internally createdStringWriter.The result can be obtained by invoking
toJson().- Returns:
- the encoder
-
create
public static JsonBeanEncoder create(com.fasterxml.jackson.core.JsonGenerator generator)
Create a new encoder using the givenJsonGenerator.- Parameters:
generator- the generator- Returns:
- the encoder
-
flush
public void flush() throws IOException
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
toJson
public String toJson()
Returns the text written to the output.Can only be used if the encoder has been created with
JsonBeanEncoder().- Returns:
- the result
-
writeArray
public JsonBeanEncoder writeArray(Object... items) throws IOException
- Throws:
IOException
-
writeObject
public JsonBeanEncoder writeObject(Object obj) throws IOException
- Throws:
IOException
-
-