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 JsonBeanEncoder
addAlias(Class<?> clazz, String alias)
Add an alias for the given class.JsonBeanEncoder
addExcluded(String className)
Add a type to excude from encoding, usually because it cannot be converted to JSON.void
close()
static JsonBeanEncoder
create()
Create a new encoder using a defaultJsonGenerator
that writes to an internally createdStringWriter
.static JsonBeanEncoder
create(com.fasterxml.jackson.core.JsonGenerator generator)
Create a new encoder using the givenJsonGenerator
.static JsonBeanEncoder
create(Writer out)
Create a new encoder using a defaultJsonGenerator
.void
flush()
JsonBeanEncoder
omitClass()
Configure the encoder to not generate theclass
information even when needed to properly restore the Object graph.String
toJson()
Returns the text written to the output.JsonBeanEncoder
writeArray(Object... items)
JsonBeanEncoder
writeObject(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:JsonCodec
Add 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 theclass
information 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
class
isn’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 defaultJsonGenerator
that 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:
flush
in interfaceFlushable
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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
-
-