|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.openntf.jsonbeanx.J2BConverter
public class J2BConverter
The J2BConverter
class can do the following:
jsonToBean(Class, String)
beanToJson(Object, J2BSettings)
createStubClasses(String)
It uses reflection to discover getter and setter classes within the Java bean classes,
and the IBM com.ibm.commons.util.io.json.JsonParser
to parse and generate
the JSON code before and after converting to/from Java beans. In other words, the code
goes:
JSON String -> JsonParser -> Java bean
or
Java bean -> JsonParser -> JSON String
You can use J2BSettings
with the conversion methods to further refine how the
JSON gets parsed or created, including adding checks to force the converter to fail if
specific JSON fields are missing or null. See the package
summary for usage details.
This code may be used under the terms of the Apache license, version 2.0. Copyright 2016 Julian Robichaux
Field Summary | |
---|---|
static java.lang.String |
UNLOGGABLE_FIELD_VALUE
The value we write to the log if a particular field has been marked as unloggable in J2BSettings |
static java.lang.String |
VERSION
The version of this class (and library) |
Method Summary | ||
---|---|---|
static java.lang.String |
beanToJson(java.lang.Object bean)
Convert the given object to a JSON string using default settings. |
|
static java.lang.String |
beanToJson(java.lang.Object bean,
J2BSettings settings)
Convert the given object to a JSON string. |
|
static java.lang.String |
createStubClasses(java.lang.String json)
Parse the json String and return the Java source for a bean or a set of beans that can be used to model the data. |
|
static
|
jsonToBean(java.lang.Class<T> beanClass,
java.lang.String json)
Convert a JSON string to a Java bean of type beanClass
using default settings. |
|
static
|
jsonToBean(java.lang.Class<T> beanClass,
java.lang.String json,
J2BSettings settings)
Convert a JSON string to a Java bean of type beanClass
using the given settings. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String VERSION
public static final java.lang.String UNLOGGABLE_FIELD_VALUE
Method Detail |
---|
public static <T> java.util.List<T> jsonToBean(java.lang.Class<T> beanClass, java.lang.String json) throws J2BException
beanClass
using default settings.
J2BException
jsonToBean(Class, String, J2BSettings)
public static <T> java.util.List<T> jsonToBean(java.lang.Class<T> beanClass, java.lang.String json, J2BSettings settings) throws J2BException
beanClass
using the given settings. The JSON string is read and converted by
the IBM XPages com.ibm.commons.util.io.json.JsonParser, and further
converted to a bean from there.
This method always returns a List of beanClass
objects,
even when the JSON contains only a single object. If the JSON contains
nested objects that correspond to child objects in the bean, these will
be created as well. The bean class and all potential child classes must
have public no-arg constructors.
JSON fields will be mapped to bean setter methods of the same name. For
example, a JSON value of "cost": 100
will attempt to use
a bean method of setCost(int i)
. The method name used to
map a JSON field can be overridden with
J2BSettings.addFieldMapping(String, String)
. Also, by default
a JSON fieldname with no mapped setter will be silently skipped. You
can change this behavior to throw an Exception instead with
J2BSettings.setMappingFailureFatal(boolean)
.
NOTE: beanClass must be a custom object with getters and setters. It cannot be a Map, List, primitive, etc. Don't get cute.
J2BException
public static java.lang.String beanToJson(java.lang.Object bean) throws J2BException
J2BException
beanToJson(Object, J2BSettings)
public static java.lang.String beanToJson(java.lang.Object bean, J2BSettings settings) throws J2BException
J2BException
public static java.lang.String createStubClasses(java.lang.String json) throws J2BException
The data used to determine field data types is also included as a comment after each field, so you can further refine your final classes. For example, all numbers are represented as Doubles, but you might want to change those to int based on the data you get. Date values are represented as Strings, and you can use the data to determine an appropriate date format String in your J2BSettings. Lists and arrays are interchangeable, but this always returns Lists.
J2BException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |