一、前言
关于camel-core源码包中org.apache.camel.json.simple.JsonObject、org.apache.camel.json.simple.JsonArray关于JSON对象、数组实现类,具体实现参见源码说明
二、源码说明
1.Jsonable定义接口
package org.apache.camel.json.simple;@b@@b@import java.io.IOException;@b@import java.io.Writer;@b@@b@public abstract interface Jsonable@b@{@b@ public abstract String toJson();@b@@b@ public abstract void toJson(Writer paramWriter)@b@ throws IOException;@b@}
2.JsonObject对象类
package org.apache.camel.json.simple;@b@@b@import java.io.IOException;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@import java.math.BigDecimal;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.LinkedHashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@@b@public class JsonObject extends LinkedHashMap<String, Object>@b@ implements Jsonable@b@{@b@ private static final long serialVersionUID = 1L;@b@@b@ public JsonObject()@b@ {@b@ }@b@@b@ public JsonObject(Map<String, ?> map)@b@ {@b@ super(map);@b@ }@b@@b@ public BigDecimal getBigDecimal(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable instanceof BigDecimal) break label57:@b@@b@ if (returnable instanceof Number)@b@ {@b@ returnable = new BigDecimal(returnable.toString());@b@ } else if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ label57: return ((BigDecimal)returnable);@b@ }@b@@b@ public BigDecimal getBigDecimalOrDefault(String key, BigDecimal defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return defaultValue;@b@@b@ if (returnable instanceof BigDecimal) break label70:@b@@b@ if (returnable instanceof Number)@b@ {@b@ returnable = new BigDecimal(returnable.toString());@b@ } else if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ label70: return ((BigDecimal)returnable);@b@ }@b@@b@ public Boolean getBoolean(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable instanceof String)@b@ returnable = Boolean.valueOf((String)returnable);@b@@b@ return ((Boolean)returnable);@b@ }@b@@b@ public Boolean getBooleanOrDefault(String key, boolean defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Boolean.valueOf(defaultValue);@b@@b@ if (returnable instanceof String)@b@ returnable = Boolean.valueOf((String)returnable);@b@@b@ return ((Boolean)returnable);@b@ }@b@@b@ public Byte getByte(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Byte.valueOf(((Number)returnable).byteValue());@b@ }@b@@b@ public Byte getByteOrDefault(String key, byte defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Byte.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Byte.valueOf(((Number)returnable).byteValue());@b@ }@b@@b@ public <T extends Collection<?>> T getCollection(String key)@b@ {@b@ return ((Collection)get(key));@b@ }@b@@b@ public <T extends Collection<?>> T getCollectionOrDefault(String key, T defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return defaultValue;@b@@b@ return ((Collection)returnable);@b@ }@b@@b@ public Double getDouble(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Double.valueOf(((Number)returnable).doubleValue());@b@ }@b@@b@ public Double getDoubleOrDefault(String key, double defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Double.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Double.valueOf(((Number)returnable).doubleValue());@b@ }@b@@b@ public <T extends Enum<T>> T getEnum(String key)@b@ throws ClassNotFoundException@b@ {@b@ String value = getStringOrDefault(key, "");@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ String[] splitValues = value.split("\\.");@b@ int numberOfSplitValues = splitValues.length;@b@ StringBuilder returnTypeName = new StringBuilder();@b@ StringBuilder enumName = new StringBuilder();@b@ for (int i = 0; i < numberOfSplitValues; ++i)@b@ if (i == numberOfSplitValues - 1)@b@ {@b@ enumName.append(splitValues[i]);@b@ } else if (i == numberOfSplitValues - 2)@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ }@b@ else@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ returnTypeName.append(".");@b@ }@b@@b@@b@ Class returnType = Class.forName(returnTypeName.toString());@b@ Enum returnable = Enum.valueOf(returnType, enumName.toString());@b@ return returnable;@b@ }@b@@b@ public <T extends Enum<T>> T getEnumOrDefault(String key, T defaultValue)@b@ throws ClassNotFoundException@b@ {@b@ String value;@b@ String[] splitValues;@b@ int numberOfSplitValues;@b@ StringBuilder returnTypeName;@b@ StringBuilder enumName;@b@ Class returnType;@b@ Enum returnable;@b@ if (containsKey(key))@b@ {@b@ value = getStringOrDefault(key, "");@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ splitValues = value.split("\\.");@b@ numberOfSplitValues = splitValues.length;@b@ returnTypeName = new StringBuilder();@b@ enumName = new StringBuilder();@b@ for (int i = 0; i < numberOfSplitValues; ++i)@b@ if (i == numberOfSplitValues - 1)@b@ {@b@ enumName.append(splitValues[i]);@b@ } else if (i == numberOfSplitValues - 2)@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ }@b@ else@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ returnTypeName.append(".");@b@ }@b@@b@@b@ returnType = Class.forName(returnTypeName.toString());@b@ returnable = Enum.valueOf(returnType, enumName.toString());@b@ }@b@ else {@b@ return defaultValue;@b@ }@b@ return returnable;@b@ }@b@@b@ public Float getFloat(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Float.valueOf(((Number)returnable).floatValue());@b@ }@b@@b@ public Float getFloatOrDefault(String key, float defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Float.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Float.valueOf(((Number)returnable).floatValue());@b@ }@b@@b@ public Integer getInteger(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Integer.valueOf(((Number)returnable).intValue());@b@ }@b@@b@ public Integer getIntegerOrDefault(String key, int defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Integer.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Integer.valueOf(((Number)returnable).intValue());@b@ }@b@@b@ public Long getLong(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Long.valueOf(((Number)returnable).longValue());@b@ }@b@@b@ public Long getLongOrDefault(String key, long defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Long.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Long.valueOf(((Number)returnable).longValue());@b@ }@b@@b@ public <T extends Map<?, ?>> T getMap(String key)@b@ {@b@ return ((Map)get(key));@b@ }@b@@b@ public <T extends Map<?, ?>> T getMapOrDefault(String key, T defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ returnable = defaultValue;@b@@b@ return ((Map)returnable);@b@ }@b@@b@ public Short getShort(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Short.valueOf(((Number)returnable).shortValue());@b@ }@b@@b@ public Short getShortOrDefault(String key, short defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return Short.valueOf(defaultValue);@b@@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Short.valueOf(((Number)returnable).shortValue());@b@ }@b@@b@ public String getString(String key)@b@ {@b@ Object returnable = get(key);@b@ if (returnable instanceof Boolean)@b@ returnable = returnable.toString();@b@ else if (returnable instanceof Number)@b@ returnable = returnable.toString();@b@@b@ return ((String)returnable);@b@ }@b@@b@ public String getStringOrDefault(String key, String defaultValue)@b@ {@b@ Object returnable;@b@ if (containsKey(key))@b@ returnable = get(key);@b@ else@b@ return defaultValue;@b@@b@ if (returnable instanceof Boolean)@b@ returnable = returnable.toString();@b@ else if (returnable instanceof Number)@b@ returnable = returnable.toString();@b@@b@ return ((String)returnable);@b@ }@b@@b@ public String toJson()@b@ {@b@ StringWriter writable = new StringWriter();@b@ try {@b@ toJson(writable);@b@ }@b@ catch (IOException localIOException) {@b@ }@b@ return writable.toString();@b@ }@b@@b@ public void toJson(Writer writable)@b@ throws IOException@b@ {@b@ boolean isFirstEntry = true;@b@ Iterator entries = entrySet().iterator();@b@ writable.write(123);@b@ while (entries.hasNext()) {@b@ if (isFirstEntry)@b@ isFirstEntry = false;@b@ else@b@ writable.write(44);@b@@b@ Map.Entry entry = (Map.Entry)entries.next();@b@ writable.write(Jsoner.serialize(entry.getKey()));@b@ writable.write(58);@b@ writable.write(Jsoner.serialize(entry.getValue()));@b@ }@b@ writable.write(125);@b@ }@b@}
3.JsonArray类
package org.apache.camel.json.simple;@b@@b@import java.io.IOException;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@import java.math.BigDecimal;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.Map;@b@@b@public class JsonArray extends ArrayList<Object>@b@ implements Jsonable@b@{@b@ private static final long serialVersionUID = 1L;@b@@b@ public JsonArray()@b@ {@b@ }@b@@b@ public JsonArray(Collection<?> collection)@b@ {@b@ super(collection);@b@ }@b@@b@ public <T> void asCollection(Collection<T> destination)@b@ {@b@ for (Iterator localIterator = iterator(); localIterator.hasNext(); ) { Object o = localIterator.next();@b@ destination.add(o);@b@ }@b@ }@b@@b@ public BigDecimal getBigDecimal(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable instanceof BigDecimal) break label57:@b@@b@ if (returnable instanceof Number)@b@ {@b@ returnable = new BigDecimal(returnable.toString());@b@ } else if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ label57: return ((BigDecimal)returnable);@b@ }@b@@b@ public Boolean getBoolean(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable instanceof String)@b@ returnable = Boolean.valueOf((String)returnable);@b@@b@ return ((Boolean)returnable);@b@ }@b@@b@ public Byte getByte(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Byte.valueOf(((Number)returnable).byteValue());@b@ }@b@@b@ public <T extends Collection<?>> T getCollection(int index)@b@ {@b@ return ((Collection)get(index));@b@ }@b@@b@ public Double getDouble(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Double.valueOf(((Number)returnable).doubleValue());@b@ }@b@@b@ public <T extends Enum<T>> T getEnum(int index)@b@ throws ClassNotFoundException@b@ {@b@ String element = getString(index);@b@ if (element == null) {@b@ return null;@b@ }@b@@b@ String[] splitValues = element.split("\\.");@b@ int numberOfValues = splitValues.length;@b@ StringBuilder returnTypeName = new StringBuilder();@b@ StringBuilder enumName = new StringBuilder();@b@ for (int i = 0; i < numberOfValues; ++i)@b@ if (i == numberOfValues - 1)@b@ {@b@ enumName.append(splitValues[i]);@b@ } else if (i == numberOfValues - 2)@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ }@b@ else@b@ {@b@ returnTypeName.append(splitValues[i]);@b@ returnTypeName.append(".");@b@ }@b@@b@@b@ Class returnType = Class.forName(returnTypeName.toString());@b@ Enum returnable = Enum.valueOf(returnType, enumName.toString());@b@ return returnable;@b@ }@b@@b@ public Float getFloat(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Float.valueOf(((Number)returnable).floatValue());@b@ }@b@@b@ public Integer getInteger(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Integer.valueOf(((Number)returnable).intValue());@b@ }@b@@b@ public Long getLong(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Long.valueOf(((Number)returnable).longValue());@b@ }@b@@b@ public <T extends Map<?, ?>> T getMap(int index)@b@ {@b@ return ((Map)get(index));@b@ }@b@@b@ public Short getShort(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable == null)@b@ return null;@b@@b@ if (returnable instanceof String)@b@ {@b@ returnable = new BigDecimal((String)returnable);@b@ }@b@ return Short.valueOf(((Number)returnable).shortValue());@b@ }@b@@b@ public String getString(int index)@b@ {@b@ Object returnable = get(index);@b@ if (returnable instanceof Boolean)@b@ returnable = returnable.toString();@b@ else if (returnable instanceof Number)@b@ returnable = returnable.toString();@b@@b@ return ((String)returnable);@b@ }@b@@b@ public String toJson()@b@ {@b@ StringWriter writable = new StringWriter();@b@ try {@b@ toJson(writable);@b@ }@b@ catch (IOException localIOException) {@b@ }@b@ return writable.toString();@b@ }@b@@b@ public void toJson(Writer writable)@b@ throws IOException@b@ {@b@ boolean isFirstElement = true;@b@ Iterator elements = iterator();@b@ writable.write(91);@b@ while (elements.hasNext()) {@b@ if (isFirstElement)@b@ isFirstElement = false;@b@ else@b@ writable.write(44);@b@@b@ writable.write(Jsoner.serialize(elements.next()));@b@ }@b@ writable.write(93);@b@ }@b@}