org.openntf.jsonbeanx
Class J2BConverter

java.lang.Object
  extended by org.openntf.jsonbeanx.J2BConverter

public class J2BConverter
extends java.lang.Object

The J2BConverter class can do the following:

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

Author:
Julian Robichaux since 0.9.2

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
<T> java.util.List<T>
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
<T> java.util.List<T>
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

VERSION

public static final java.lang.String VERSION
The version of this class (and library)

See Also:
Constant Field Values

UNLOGGABLE_FIELD_VALUE

public static final java.lang.String UNLOGGABLE_FIELD_VALUE
The value we write to the log if a particular field has been marked as unloggable in J2BSettings

See Also:
Constant Field Values
Method Detail

jsonToBean

public static <T> java.util.List<T> jsonToBean(java.lang.Class<T> beanClass,
                                               java.lang.String json)
                                    throws J2BException
Convert a JSON string to a Java bean of type beanClass using default settings.

Throws:
J2BException
See Also:
jsonToBean(Class, String, J2BSettings)

jsonToBean

public static <T> java.util.List<T> jsonToBean(java.lang.Class<T> beanClass,
                                               java.lang.String json,
                                               J2BSettings settings)
                                    throws J2BException
Convert a JSON string to a Java bean of type 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.

Throws:
J2BException

beanToJson

public static java.lang.String beanToJson(java.lang.Object bean)
                                   throws J2BException
Convert the given object to a JSON string using default settings.

Throws:
J2BException
See Also:
beanToJson(Object, J2BSettings)

beanToJson

public static java.lang.String beanToJson(java.lang.Object bean,
                                          J2BSettings settings)
                                   throws J2BException
Convert the given object to a JSON string. If the object is a bean, the getter methods in the bean will be used to generate JSON fields and values. If the object is an array or a List of beans, each bean will be processed individually and returned as a JSON array. This method converts the bean information to Maps and Lists, and then uses com.ibm.commons.util.io.json.JsonGenerator to create the JSON string.

Throws:
J2BException

createStubClasses

public static java.lang.String createStubClasses(java.lang.String json)
                                          throws J2BException
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. If beans are used inside of other beans as field values, the source for those are returned as well, and they will be returned multiple times if they are referenced multiple times.

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.

Throws:
J2BException