Java Utililty Methods Json to Object

List of utility methods to do Json to Object

Description

The list of methods to do Json to Object are organized into topic(s).

Method

TjsonToObj(String json, Class clazz)
json To Obj
if (json == null)
    return null;
try {
    return objectMapper.readValue(json, clazz);
} catch (Exception e) {
    throw new IllegalStateException("Unable to parse Json String.", e);
TjsonToObj(String json, Class valueType)
 convert json string to class Object. 
try {
    return MAPPER.readValue(json, valueType);
} catch (Exception ex) {
    throw new RuntimeException(ex);
TjsonToObject(String json, Class toValueType)
json To Object
if (json == null) {
    return null;
try {
    return objectMapper.readValue(json, toValueType);
} catch (Exception e) {
    throw new RuntimeException("json to object failed :" + e.getMessage());
TjsonToObject(String jsonString, Class classOfT)
json To Object
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new JsonSerializer<Date>() {
    @Override
    public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
        SimpleDateFormat format = new SimpleDateFormat(dateformat);
        return new JsonPrimitive(format.format(src));
}).setDateFormat(dateformat).create();
Object object = gson.fromJson(jsonString, (Type) classOfT);
...
TjsonToPojo(String jsonData, Class beanType)
json To Pojo
try {
    T t = MAPPER.readValue(jsonData, beanType);
    return t;
} catch (Exception e) {
    e.printStackTrace();
return null;
Mapparse(String jsonLine)
parse
Map jsonJavaRootObject = new Gson().fromJson(jsonLine, Map.class);
return jsonJavaRootObject;
TreadValue(String jsonStr, Class valueType)
read Value
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
try {
    return objectMapper.readValue(jsonStr, valueType);
} catch (Exception e) {
    e.printStackTrace();
return null;
...
StringstringForJSON(String input)
Format String for JSON Serialization
if (input == null || input.isEmpty())
    return "";
final int len = input.length();
final StringBuilder result = new StringBuilder(len + len / 4);
final StringCharacterIterator iterator = new StringCharacterIterator(input);
char ch = iterator.current();
while (ch != CharacterIterator.DONE) {
    if (ch == '\n') {
...