package com.aincc.lib.parse.json; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; /** * * <h3><b>JSonManager</b></h3></br> * * JSon Parse Manager (using jackson library(wrapping)) * <p> * * @author aincc@barusoft.com * @version 1.0.0 * @since 1.0.0 */ public class JSonManager { /** * static singleton instance * * @since 1.0.0 */ private volatile static JSonManager manager; /** * object mapper * * @since 1.0.0 */ private ObjectMapper mapper = new ObjectMapper(); /** * initialized flag * * @since 1.0.0 */ private boolean initialized = false; /** * get singleton instance * * @since 1.0.0 * @return singleton instance */ public static JSonManager getInstance() { if (null == manager) { synchronized (JSonManager.class) { if (null == manager) { manager = new JSonManager(); } } } if (false == manager.isInitialized()) { manager.initialize(); } return manager; } /** * * @since 1.0.0 * @return the initialized */ public boolean isInitialized() { return initialized; } /** * * @since 1.0.0 * @param initialized */ public void setInitialized(boolean initialized) { this.initialized = initialized; } /** * initialize * * @since 1.0.0 */ public void initialize() { setInitialized(true); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); useDefaultPropertynaming(); } /** * * @since 1.0.0 */ public void useCamelCaseNaming() { mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); } /** * * @since 1.0.0 */ public void useDefaultPropertynaming() { mapper.setPropertyNamingStrategy(null); } /** * * @since 1.0.0 * @return the object mapper */ public ObjectMapper getMapper() { return mapper; } /** * parse json to object * * @since 1.0.0 * @param buffer * json string * @param className * target object class * @return new object */ public Object deserialize(String buffer, Class<?> className) { try { return mapper.readValue(buffer, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * parse json to object * * @since 1.0.0 * @param buffer * json input stream * @param className * target object class * @return new object */ public Object deserialize(InputStream buffer, Class<?> className) { try { return mapper.readValue(buffer, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * parse json to object * * @since 1.0.0 * @param file * json input file * @param className * target object class * @return new object */ public Object deserialize(File file, Class<?> className) { try { return mapper.readValue(file, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * parse json to object * * @since 1.0.0 * @param buffer * json byte buffer * @param className * target object class * @return new object */ public Object deserialize(byte[] buffer, Class<?> className) { try { return mapper.readValue(buffer, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * parse json to object * * @since 1.0.0 * @param src * json Reader * @param className * target object class * @return new object */ public Object deserialize(Reader src, Class<?> className) { try { return mapper.readValue(src, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * parse json to object * * @since 1.0.0 * @param url * json URL * @param className * target object class * @return new object */ public Object deserialize(URL url, Class<?> className) { try { return mapper.readValue(url, className); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * make JSon string from map * * @since 1.0.0 * @param list * @return JSon String */ public String makeJSonFromMap(Map<?, ?> map) { try { return mapper.writeValueAsString(map); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * make JSon string from List NameValuePair * * @since 1.0.0 * @param list * @return JSon String */ public String makeJSonFromListNameValuePair(List<NameValuePair> list) { try { LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); for (NameValuePair pair : list) { map.put(pair.getName(), pair.getValue()); } return mapper.writeValueAsString(map); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * make map from JSon string * * @since 1.0.0 * @param json * @return the map */ public Map<?, ?> makeMapFromJSon(String json) { try { return mapper.readValue(json, Map.class); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * make List NameValuePair from JSon string * * @since 1.0.0 * @param json * @return the List NameValuePair */ public List<NameValuePair> makeListNameValuePairFromJSon(String json) { try { @SuppressWarnings("unchecked") LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) mapper.readValue(json, Map.class); ArrayList<NameValuePair> list = new ArrayList<NameValuePair>(); Set<Entry<String, String>> entry = map.entrySet(); Iterator<Entry<String, String>> it = entry.iterator(); while (it.hasNext()) { Entry<String, String> item = it.next(); list.add(new BasicNameValuePair(item.getKey(), item.getValue())); } return list; } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * print json string from object * * @since 1.0.0 * @param obj * target object * @return json string */ public String print(Object obj) { try { String result = mapper.writeValueAsString(obj); return result; } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(String content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(InputStream content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(File content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(byte[] content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(Reader content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } /** * * @since 1.0.0 * @param content * @return the json node object */ public JsonNode getNode(URL content) { JsonNode node = null; try { node = mapper.readTree(content); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return node; } }