com.rockagen.commons.util.JsonUtil Maven / Gradle / Ivy
Show all versions of commons-lib Show documentation
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rockagen.commons.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* JSON UTILS
*
* @author RA
* @since JDK1.6
* @since JACKSON 2.2.0
*/
public class JsonUtil {
// ~ Instance fields ==================================================
/**
*
*/
private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
/**
* ObjectMapper
*/
private static final ThreadLocal threadMapper = new ThreadLocal();
/**
* JsonFactory
*/
private static final ThreadLocal threadJsonFactory = new ThreadLocal();
// ~ Constructors ==================================================
/**
*/
private JsonUtil() {
}
// ~ Methods ==================================================
/**
* return a ObjectMapper if exist,else new ObjectMapper
*
* @return ObjectMapper
*/
public static ObjectMapper getMapper() {
ObjectMapper mapper = threadMapper.get();
if (mapper == null) {
mapper = initMapper();
threadMapper.set(mapper);
}
return mapper;
}
/**
* Initialize mapper
*
* @return initialized {@link ObjectMapper}
*/
private static ObjectMapper initMapper() {
ObjectMapper mapper = new ObjectMapper();
// to enable standard indentation ("pretty-printing"):
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// to allow serialization of "empty" POJOs (no properties to serialize)
// (without this setting, an exception is thrown in those cases)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// set writer flush after writer value
mapper.enable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
// ObjectMapper will call close() and root values that implement
// java.io.Closeable;
// including cases where exception is thrown and serialization does not
// completely succeed.
mapper.enable(SerializationFeature.CLOSE_CLOSEABLE);
// to write java.util.Date, Calendar as number (timestamp):
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// disable default date to timestamp
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
// DeserializationFeature for changing how JSON is read as POJOs:
// to prevent exception when encountering unknown property:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// disable default date to timestamp
mapper.disable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS);
// to allow coercion of JSON empty String ("") to null Object value:
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// Set Default date fromat
mapper.setDateFormat(df);
return mapper;
}
/**
* return a JsonFactory if exist,else new JsonFactory
*
* @return JsonFactory
*/
public static JsonFactory getJsonFactory() {
JsonFactory jsonFactory = threadJsonFactory.get();
if (jsonFactory == null) {
jsonFactory = new JsonFactory();
// JsonParser.Feature for configuring parsing settings:
// to allow C/C++ style comments in JSON (non-standard, disabled by
// default)
jsonFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
// to allow (non-standard) unquoted field names in JSON:
jsonFactory.disable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
// to allow use of apostrophes (single quotes), non standard
jsonFactory.disable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
// JsonGenerator.Feature for configuring low-level JSON generation:
// no escaping of non-ASCII characters:
jsonFactory.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
threadJsonFactory.set(jsonFactory);
}
return jsonFactory;
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonStr,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonStr, MyBean[])
*
* toBean(jsonStr, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonStr json String
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(String jsonStr, TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonStr == null)
return null;
return toBean(new MyJsonParser(jsonStr), valueTypeRef);
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonBytes,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonBytes, MyBean[])
*
* toBean(jsonBytes, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonBytes json bytes
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(byte[] jsonBytes, TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonBytes == null)
return null;
return toBean(new MyJsonParser(jsonBytes), valueTypeRef);
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonReader,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonReader, MyBean[])
*
* toBean(jsonReader, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonReader json reader
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(Reader jsonReader, TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonReader == null)
return null;
return toBean(new MyJsonParser(jsonReader), valueTypeRef);
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonURL,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonURL, MyBean[])
*
* toBean(jsonURL, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonURL json url
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(URL jsonURL, TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonURL == null)
return null;
return toBean(new MyJsonParser(jsonURL), valueTypeRef);
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonFile,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonFile, MyBean[])
*
* toBean(jsonFile, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonFile josn file
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(File jsonFile, TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonFile == null)
return null;
return toBean(new MyJsonParser(jsonFile), valueTypeRef);
}
/**
* Json string to java bean
*
* call {@link MyJsonParser#getJsonParser()} return a JsonParser
*
*
* @param myJsonParser custom json parser
* @param valueTypeRef value type ref
* @see #toBean(JsonParser, TypeReference)
* @param t
* @return bean
*/
protected static T toBean(MyJsonParser myJsonParser,
TypeReference valueTypeRef) {
if (myJsonParser.getJsonParser() == null)
return null;
return toBean(myJsonParser.getJsonParser(), valueTypeRef);
}
/**
* Json string to java bean
*
* List,Array,Map
*
*
* clazz= new TypeReference<List<MyBean>>(){};
*
* e.g:
* toBean(jsonParser,new TypeReference<List<MyBean>>(){})
*
* toBean(jsonParser, MyBean[])
*
* toBean(jsonParser, new TypeReference<Map<String, Map<String, MyBean>>(){})
*
*
* @param jsonParser json parser
* @param valueTypeRef value type ref
* @param t
* @return bean
*/
public static T toBean(JsonParser jsonParser,
TypeReference valueTypeRef) {
if (valueTypeRef == null || jsonParser == null)
return null;
T obj = null;
try {
obj = getMapper().readValue(jsonParser, valueTypeRef);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (JsonMappingException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
} finally {
try {
jsonParser.close();
} catch (IOException e) {
}
}
return obj;
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(String, TypeReference)}
*
*
* @param jsonStr json String
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(String jsonStr, Class clazz) {
if (clazz == null || jsonStr == null)
return null;
return toBean(new MyJsonParser(jsonStr), clazz);
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(byte[], TypeReference)}
*
*
* @param jsonBytes josn bytes
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(byte[] jsonBytes, Class clazz) {
if (clazz == null || jsonBytes == null)
return null;
return toBean(new MyJsonParser(jsonBytes), clazz);
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(Reader, TypeReference)}
*
*
* @param jsonReader json reader
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(Reader jsonReader, Class clazz) {
if (clazz == null || jsonReader == null)
return null;
return toBean(new MyJsonParser(jsonReader), clazz);
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(URL, TypeReference)}
*
*
* @param jsonURL josn url
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(URL jsonURL, Class clazz) {
if (clazz == null || jsonURL == null)
return null;
return toBean(new MyJsonParser(jsonURL), clazz);
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(File, TypeReference)}
*
*
* @param jsonFile json file
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(File jsonFile, Class clazz) {
if (clazz == null || jsonFile == null)
return null;
return toBean(new MyJsonParser(jsonFile), clazz);
}
/**
* Json string to java bean
*
* call {@link MyJsonParser#getJsonParser()} return a JsonParser
*
*
* @param myJsonParser custom json parser
* @param clazz class
* @see #toBean(JsonParser, Class)
* @param t
* @return bean
*/
protected static T toBean(MyJsonParser myJsonParser, Class clazz) {
if (myJsonParser.getJsonParser() == null)
return null;
return toBean(myJsonParser.getJsonParser(), clazz);
}
/**
* Json string to java bean
*
* e.g:
*
*
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
*
* List,Array,Map... see {@link #toBean(String, TypeReference)}
*
*
* @param jsonParser json parser
* @param clazz class
* @param t
* @return bean
*/
public static T toBean(JsonParser jsonParser, Class clazz) {
if (clazz == null || jsonParser == null)
return null;
T obj = null;
try {
obj = getMapper().readValue(jsonParser, clazz);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (JsonMappingException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
} finally {
try {
jsonParser.close();
} catch (IOException e) {
}
}
return obj;
}
/**
* Bean to json string
*
* @param obj obj
* bean object
* @param t
* @return json string
*/
public static String toJson(T obj) {
StringWriter writer = new StringWriter();
String jsonStr = "";
JsonGenerator gen = null;
try {
gen = getJsonFactory().createGenerator(writer);
getMapper().writeValue(gen, obj);
writer.flush();
jsonStr = writer.toString();
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
} finally {
if (gen != null) {
try {
gen.close();
} catch (IOException e) {
}
}
}
return jsonStr;
}
/**
* JsonParser Helper
provide to
* {@link JsonUtil#toBean(MyJsonParser, Class)} and
* {@link JsonUtil#toBean(MyJsonParser, TypeReference)}
*/
protected static class MyJsonParser {
// ~ Instance fields ==================================================
/**
* JsonParser
*/
private JsonParser jsonParser;
// ~ Constructors ==================================================
/**
* Set jsonParser from {@link JsonFactory#createParser(File)}
*
* @param obj obj
*/
public MyJsonParser(File obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
/**
* Set jsonParser from {@link JsonFactory#createParser(Reader)}
*
* @param obj obj
*/
public MyJsonParser(Reader obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
/**
* Set jsonParser from {@link JsonFactory#createParser(byte[])}
*
* @param obj obj
*/
public MyJsonParser(byte[] obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
/**
* Set jsonParser from {@link JsonFactory#createParser(String)}
*
* @param obj obj
*/
public MyJsonParser(String obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
/**
* Set jsonParser from {@link JsonFactory#createParser(URL)}
*
* @param obj obj
*/
public MyJsonParser(URL obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
/**
* Set jsonParser from {@link JsonFactory#createParser(InputStream)}
*
* @param obj obj
*/
public MyJsonParser(InputStream obj) {
try {
this.jsonParser = getJsonFactory().createParser(obj);
} catch (JsonParseException e) {
log.error("{}", e.getMessage(), e);
} catch (IOException e) {
log.error("{}", e.getMessage(), e);
}
}
// ~ Methods ==================================================
/**
* Return a JsonParser by Constructors
*
* @return jsonParser
*/
public JsonParser getJsonParser() {
return jsonParser;
}
}
}