Class JavaBeanSerializer
java.lang.Object
org.jdrupes.jsonb.beans.JavaBeanConverter
org.jdrupes.jsonb.beans.JavaBeanSerializer
- All Implemented Interfaces:
JsonbSerializer<Object>
A serializer that treats all objects as JavaBeans. The JSON description of an object can have an additional key/value pair with key "@class" and a class name. This 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"
}
]
}
As there is no marker interface for JavaBeans, this serializer
considers all objects to be JavaBeans by default and obtains all
information for serialization from the generated (or explicitly
provided) BeanInfo. It is, however, possible to exclude
classes from bing handled by this serializer by either annotating them
with JsonbAnnotation or
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an alias for a class.addIgnored(Class<?>... type) Don't handle the given type(s) as JavaBeans.voidserialize(Object value, JsonGenerator generator, SerializationContext context) setExpected(Class<?> type) Sets the expected class for the object passed as parameter toJsonb.toJson(java.lang.Object).Methods inherited from class org.jdrupes.jsonb.beans.JavaBeanConverter
findBeanInfo, findPropertyEditor
-
Constructor Details
-
JavaBeanSerializer
public JavaBeanSerializer()Create a new instance.
-
-
Method Details
-
addAlias
Adds an alias for a class.- Parameters:
clazz- the clazzalias- the alias- Returns:
- the java bean serializer
-
setExpected
Sets the expected class for the object passed as parameter toJsonb.toJson(java.lang.Object).- Parameters:
type- the type- Returns:
- the java bean serializer
-
addIgnored
Don't handle the given type(s) as JavaBeans. Pass them to the default serialization mechanism instead.- Parameters:
type- the type(s) to ignore- Returns:
- the java bean serializer
-
serialize
- Specified by:
serializein interfaceJsonbSerializer<Object>
-