https://gitee.com/xxssyyyyssxx/Json-fastjson
https://gitee.com/xxssyyyyssxx/Json-orgJson
https://gitee.com/xxssyyyyssxx/Json-Gson
https://gitee.com/xxssyyyyssxx/Json-Jackson
https://gitee.com/xxssyyyyssxx/Json-Jsonlib
后续打算实现一些高级特性,比如FastJson、Gson的注解等
- fastjson实现
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import com.alibaba.fastjson.JSON;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author xiongshiyan at 2018/6/10
*/
public class JSONObject extends BaseJson<JSONObject> implements JsonObject {
private com.alibaba.fastjson.JSONObject jsonObject;
public JSONObject(com.alibaba.fastjson.JSONObject jsonObject){
this.jsonObject = jsonObject;
}
public JSONObject(Map<String , Object> map){
this.jsonObject = new com.alibaba.fastjson.JSONObject(map);
}
public JSONObject(){
this.jsonObject = new com.alibaba.fastjson.JSONObject();
}
public JSONObject(String jsonString){
this.jsonObject = JSON.parseObject(jsonString);
}
@Override
public com.alibaba.fastjson.JSONObject unwrap() {
return jsonObject;
}
@Override
public Object get(String key) {
assertKey(key);
return checkNullValue(key , jsonObject.get(key));
}
@Override
public Object get(String key, Object defaultObject) {
assertKey(key);
Object temp = jsonObject.get(key);
return null == temp ? defaultObject : temp;
}
@Override
public JsonObject getJsonObject(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.jsonObject.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof com.alibaba.fastjson.JSONObject){
return new JSONObject((com.alibaba.fastjson.JSONObject) t);
}
if(t instanceof Map){
return new JSONObject((Map<String, Object>) t);
}
return (JsonObject) t;
}
@Override
public JsonArray getJsonArray(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.jsonObject.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof com.alibaba.fastjson.JSONArray){
return new JSONArray((com.alibaba.fastjson.JSONArray)t);
}
if(t instanceof List){
return new JSONArray((List<Object>) t);
}
return (JsonArray) t;
}
@Override
public String getString(String key) {
assertKey(key);
String temp = this.jsonObject.getString(key);
return checkNullValue(key, temp);
}
@Override
public String getString(String key, String defaultValue) {
assertKey(key);
String temp = this.jsonObject.getString(key);
return null == temp ? defaultValue : temp;
}
@Override
public Boolean getBoolean(String key) {
assertKey(key);
Boolean temp = this.jsonObject.getBoolean(key);
return checkNullValue(key, temp);
}
@Override
public Boolean getBoolean(String key, Boolean defaultValue) {
assertKey(key);
Boolean temp = this.jsonObject.getBoolean(key);
return null == temp ? defaultValue : temp;
}
@Override
public Integer getInteger(String key) {
assertKey(key);
Integer temp = this.jsonObject.getInteger(key);
return checkNullValue(key, temp);
}
@Override
public Integer getInteger(String key, Integer defaultValue) {
assertKey(key);
Integer temp = this.jsonObject.getInteger(key);
return null == temp ? defaultValue : temp;
}
@Override
public Long getLong(String key) {
assertKey(key);
Long temp = this.jsonObject.getLong(key);
return checkNullValue(key, temp);
}
@Override
public Long getLong(String key, Long defaultValue) {
assertKey(key);
Long temp = this.jsonObject.getLong(key);
return null == temp ? defaultValue : temp;
}
@Override
public Float getFloat(String key) {
assertKey(key);
Float temp = this.jsonObject.getFloat(key);
return checkNullValue(key, temp);
}
@Override
public Float getFloat(String key, Float defaultValue) {
assertKey(key);
Float temp = this.jsonObject.getFloat(key);
return null == temp ? defaultValue : temp;
}
@Override
public Double getDouble(String key) {
assertKey(key);
Double temp = this.jsonObject.getDouble(key);
return checkNullValue(key, temp);
}
@Override
public Double getDouble(String key, Double defaultValue) {
assertKey(key);
Double temp = this.jsonObject.getDouble(key);
return null == temp ? defaultValue : temp;
}
@Override
public BigInteger getBigInteger(String key) {
assertKey(key);
BigInteger temp = this.jsonObject.getBigInteger(key);
return checkNullValue(key, temp);
}
@Override
public BigInteger getBigInteger(String key, BigInteger defaultValue) {
assertKey(key);
BigInteger temp = this.jsonObject.getBigInteger(key);
return null == temp ? defaultValue : temp;
}
@Override
public BigDecimal getBigDecimal(String key) {
assertKey(key);
BigDecimal temp = this.jsonObject.getBigDecimal(key);
return checkNullValue(key, temp);
}
@Override
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
assertKey(key);
BigDecimal temp = this.jsonObject.getBigDecimal(key);
return null == temp ? defaultValue : temp;
}
@Override
public <T> T get(String key, Class<T> clazz) {
return jsonObject.getObject(key , clazz);
}
@Override
public Set<String> keySet() {
return jsonObject.keySet();
}
@Override
public int size() {
return jsonObject.size();
}
@Override
public boolean isEmpty() {
return jsonObject.isEmpty();
}
@Override
public boolean containsKey(String key) {
return jsonObject.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return jsonObject.containsValue(value);
}
@Override
public JsonObject put(String key, Object value) {
jsonObject.put(key, value);
return this;
}
@Override
public JsonObject putAll(Map<? extends String, ?> m) {
jsonObject.putAll(m);
return this;
}
@Override
public void clear() {
jsonObject.clear();
}
@Override
public Object remove(String key) {
return jsonObject.remove(key);
}
@Override
public JsonObject parse(String jsonString) {
jsonObject = JSON.parseObject(jsonString);
return this;
//return new JSONObject(JSON.parseObject(jsonString));
}
@Override
public JsonObject fromMap(Map<String, Object> map) {
return new JSONObject(new com.alibaba.fastjson.JSONObject(map));
}
@Override
public String serialize(Object javaBean) {
return JSON.toJSONString(javaBean);
}
@Override
public <T> T deserialize(String jsonString, Class<T> clazz) {
return JSON.parseObject(jsonString , clazz);
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<String , Json> map = new HashMap<>();
for (String key : jsonObject.keySet()) {
Object o = jsonObject.get(key);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(key , (Json) o);
}
}
map.forEach((k , v)-> jsonObject.put(k , v.unwrap()));
return jsonObject.toString();
}
@Override
public int hashCode() {
return jsonObject.hashCode();
}
@Override
public boolean equals(Object obj) {
return jsonObject.equals(obj);
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import com.alibaba.fastjson.JSON;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xiongshiyan at 2018/6/11
*/
public class JSONArray extends BaseJson<JSONArray> implements JsonArray {
private com.alibaba.fastjson.JSONArray jsonArray;
public JSONArray(com.alibaba.fastjson.JSONArray jsonArray){
this.jsonArray = jsonArray;
}
public JSONArray(List<Object> list){
this.jsonArray = new com.alibaba.fastjson.JSONArray(list);
}
public JSONArray(){
this.jsonArray = new com.alibaba.fastjson.JSONArray();
}
public JSONArray(String arrayString){
this.jsonArray = JSON.parseArray(arrayString);
}
@Override
public int size() {
return jsonArray.size();
}
@Override
public Object get(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.get(index));
}
@Override
public String getString(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getString(index));
}
@Override
public Boolean getBoolean(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getBoolean(index));
}
@Override
public Integer getInteger(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getInteger(index));
}
@Override
public Long getLong(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getLong(index));
}
@Override
public Double getDouble(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getDouble(index));
}
@Override
public Float getFloat(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getFloat(index));
}
@Override
public BigInteger getBigInteger(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getBigInteger(index));
}
@Override
public BigDecimal getBigDecimal(int index) {
assertIndex(index , size());
return checkNullValue(index , jsonArray.getBigDecimal(index));
}
@Override
public JsonObject getJsonObject(int index) {
assertIndex(index , size());
Object opt = jsonArray.get(index);
if(opt instanceof com.alibaba.fastjson.JSONObject){
return new JSONObject((com.alibaba.fastjson.JSONObject)opt);
}
if(opt instanceof Map){
return new JSONObject((Map<String, Object>) opt);
}
return (JsonObject) opt;
}
@Override
public JsonArray getJsonArray(int index) {
assertIndex(index , size());
Object opt = jsonArray.get(index);
if(opt instanceof com.alibaba.fastjson.JSONArray){
return new JSONArray((com.alibaba.fastjson.JSONArray)opt);
}
if(opt instanceof List){
return new JSONArray((List)opt);
}
return (JsonArray) opt;
}
@Override
public JsonArray remove(int index) {
jsonArray.remove(index);
return this;
}
@Override
public JsonArray clear() {
jsonArray.clear();
return this;
}
@Override
public JsonArray put(Object o) {
jsonArray.add(o);
return this;
}
@Override
public JsonArray put(int index, Object o) {
jsonArray.remove(index);
jsonArray.add(index , o);
return this;
}
@Override
public JsonArray putAll(Collection<?> os) {
jsonArray.addAll(os);
return this;
}
@Override
public JsonArray parse(String jsonString) {
this.jsonArray = JSON.parseArray(jsonString);
return this;
//return new JSONArray(JSON.parseArray(jsonString));
}
@Override
public com.alibaba.fastjson.JSONArray unwrap() {
return jsonArray;
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<Integer , Json> map = new HashMap<>();
int size = size();
for (int i = 0; i < size; i++) {
Object o = jsonArray.get(i);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(i , (Json) o);
}
}
map.forEach((k,v)->{
jsonArray.remove((int)k);
jsonArray.add((int)k , v.unwrap());
});
return jsonArray.toString();
}
@Override
public int hashCode() {
return jsonArray.hashCode();
}
@Override
public boolean equals(Object obj) {
return jsonArray.equals(obj);
}
@Override
public JsonArray fromList(List<Object> list) {
jsonArray = new com.alibaba.fastjson.JSONArray(list);
return this;
}
}
通用测试方法:
package cn.zytx.common.json;
import cn.zytx.common.json.impl.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 使用的时候不会出现跟具体JSON框架耦合的代码
*/
public class JSONObjectTest {
JsonObject jsonObject = new JSONObject();
@Before
public void init(){
jsonObject.put("k1","v1");
jsonObject.put("k2","v2");
jsonObject.put("boolean1",true);
jsonObject.put("integer1",1);
jsonObject.put("long1",1L);
jsonObject.put("double1",1D);
jsonObject.put("float1",1F);
jsonObject.put("bigInteger1",new BigInteger("1"));
jsonObject.put("bigDecimal1",new BigDecimal("1"));
JSONObject object = new JSONObject();
object.put("k1" , "v1");
jsonObject.put("k3", new JSONObject(object.unwrap()));
}
@Test
public void get() throws Exception {
Assert.assertEquals("v1" , jsonObject.get("k1"));
jsonObject.setStrict(false);
Assert.assertNull(jsonObject.get("k4"));
}
@Test
public void getDefault() throws Exception {
Assert.assertEquals("v2" , jsonObject.get("k2" , "kv2"));
Assert.assertEquals("kv2" , jsonObject.get("k4" , "kv2"));
}
@Test
public void getJsonObject() throws Exception {
JsonObject k3 = jsonObject.getJsonObject("k3");
Assert.assertEquals("v1" , k3.getString("k1"));
}
@Test
public void getJsonArray() throws Exception {
}
@Test
public void getString() throws Exception {
String v1 = jsonObject.getString("k1");
Assert.assertEquals("v1" , v1);
}
@Test
public void getStringDefault() throws Exception {
String v1 = jsonObject.getString("k1" , "vv");
Assert.assertEquals("v1" , v1);
String v4 = jsonObject.getString("k4" , "vv");
Assert.assertEquals("vv" , v4);
}
@Test
public void getBoolean() throws Exception {
Boolean boolean1 = jsonObject.getBoolean("boolean1");
Assert.assertEquals(true , boolean1);
}
@Test
public void getBooleanDefault() throws Exception {
Boolean boolean1 = jsonObject.getBoolean("boolean1" , false);
Assert.assertEquals(true, boolean1);
Boolean v4 = jsonObject.getBoolean("k4" , false);
Assert.assertEquals(false , v4);
}
@Test
public void getInteger() throws Exception {
Integer integer1 = jsonObject.getInteger("integer1");
Assert.assertEquals((Integer) 1 , integer1);
}
@Test
public void getIntegerDefault() throws Exception {
Integer integer1 = jsonObject.getInteger("integer1" , 1);
Assert.assertEquals((Integer) 1, integer1);
Integer v4 = jsonObject.getInteger("k4" , 2);
Assert.assertEquals((Integer) 2 , v4);
}
@Test
public void getLong() throws Exception {
Long long1 = jsonObject.getLong("long1");
Assert.assertEquals(Long.valueOf(1) , long1);
}
@Test
public void getLongDefault() throws Exception {
Long long1 = jsonObject.getLong("long1" , 1L);
Assert.assertEquals((Long)1L, long1);
Long v4 = jsonObject.getLong("k4" , 2L);
Assert.assertEquals(Long.valueOf(2) , v4);
}
@Test
public void getFloat() throws Exception {
Float float1 = jsonObject.getFloat("float1");
Assert.assertEquals(Float.valueOf(1) , float1);
}
@Test
public void getFloatDefault() throws Exception {
Float float1 = jsonObject.getFloat("float1" , 1F);
Assert.assertEquals(Float.valueOf(1), float1);
Float v4 = jsonObject.getFloat("k4" , 2F);
Assert.assertEquals(Float.valueOf(2) , v4);
}
@Test
public void getDouble() throws Exception {
Double double1 = jsonObject.getDouble("double1");
Assert.assertEquals(Double.valueOf(1) , double1);
}
@Test
public void getDoubleDefault() throws Exception {
Double double1 = jsonObject.getDouble("double1" , 1D);
Assert.assertEquals((Double) 1D, double1);
Double v4 = jsonObject.getDouble("k4" , 2D);
Assert.assertEquals(Double.valueOf(2) , v4);
}
@Test
public void getBigInteger() throws Exception {
BigInteger bigInteger1 = jsonObject.getBigInteger("bigInteger1");
Assert.assertEquals(new BigInteger("1") , bigInteger1);
}
@Test
public void getBigIntegerDefault() throws Exception {
BigInteger bigInteger1 = jsonObject.getBigInteger("bigInteger1" , new BigInteger("2"));
Assert.assertEquals(new BigInteger("1") , bigInteger1);
BigInteger v4 = jsonObject.getBigInteger("k4" , new BigInteger("2"));
Assert.assertEquals(new BigInteger("2") , v4);
}
@Test
public void getBigDecimal() throws Exception {
BigDecimal bigDecimal1 = jsonObject.getBigDecimal("bigDecimal1");
Assert.assertEquals(new BigDecimal("1") , bigDecimal1);
}
@Test
public void getBigDecimalDefault() throws Exception {
BigDecimal bigDecimal1 = jsonObject.getBigDecimal("bigDecimal1" , new BigDecimal("2"));
Assert.assertEquals(new BigDecimal("1") , bigDecimal1);
BigDecimal v4 = jsonObject.getBigDecimal("k4" , new BigDecimal("2"));
Assert.assertEquals(new BigDecimal("2") , v4);
}
@Test
public void getClassObj() throws Exception {
String k1 = jsonObject.get("k1", String.class);
Assert.assertEquals("v1" , k1);
}
@Test
public void keySet() throws Exception {
Set<String> keySet = jsonObject.keySet();
System.out.println(keySet);
}
@Test
public void size() throws Exception {
Assert.assertEquals(10 , jsonObject.size());
}
@Test
public void isEmpty() throws Exception {
Assert.assertFalse(jsonObject.isEmpty());
}
@Test
public void containsKey() throws Exception {
Assert.assertTrue(jsonObject.containsKey("k1"));
Assert.assertFalse(jsonObject.containsKey("k5"));
}
@Test
public void containsValue() throws Exception {
Assert.assertTrue(jsonObject.containsValue("v1"));
Assert.assertFalse(jsonObject.containsValue("vv"));
}
@Test
public void put() throws Exception {
JsonObject put = jsonObject.put("k4", "v4");
Assert.assertEquals(11 , jsonObject.size());
Assert.assertEquals(11 , put.size());
Assert.assertEquals("v4" , jsonObject.getString("k4"));
Assert.assertEquals("v4" , put.getString("k4"));
}
@Test
public void putAll() throws Exception {
Map<String , Object> map = new HashMap<>(2);
map.put("k5" , "v5");
map.put("k6" , "v6");
JsonObject putAll = this.jsonObject.putAll(map);
Assert.assertEquals(12 , jsonObject.size());
Assert.assertEquals(12 , putAll.size());
Assert.assertEquals("v5" , jsonObject.getString("k5"));
Assert.assertEquals("v6" , jsonObject.getString("k6"));
Assert.assertEquals("v5" , putAll.getString("k5"));
Assert.assertEquals("v6" , putAll.getString("k6"));
}
@Test
public void clear() throws Exception {
jsonObject.clear();
Assert.assertEquals(0 , jsonObject.size());
}
@Test
public void remove() throws Exception {
Object k1 = jsonObject.remove("k1");
Assert.assertEquals(9 , jsonObject.size());
}
@Test
public void parse() throws Exception {
JsonObject parse = jsonObject.parse("{\"xx\":\"ss\" , \"ss\":{\"cc\":12}}");
Assert.assertEquals("{\"ss\":{\"cc\":12},\"xx\":\"ss\"}" , parse.toString());
Assert.assertEquals("{\"ss\":{\"cc\":12},\"xx\":\"ss\"}" , jsonObject.toString());
}
@Test
public void serialize() throws Exception {
FastJsonObjectBean javaBean = new FastJsonObjectBean();
javaBean.setK1("11");
String serialize = jsonObject.serialize(javaBean);
System.out.println(serialize);
}
@Test
public void deserialize() throws Exception {
FastJsonObjectBean deserialize = jsonObject.deserialize("{\"k1\":\"11\"}", FastJsonObjectBean.class);
System.out.println(deserialize);
FastJsonObjectBean deserialize1 = jsonObject.deserialize("{\"bigDecimal1\":1,\"bigInteger1\":1,\"boolean1\":true,\"double1\":1,\"float1\":1,\"integer1\":1,\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":{\"k1\":\"v1\"},\"long1\":1}", FastJsonObjectBean.class);
System.out.println(deserialize1);
}
@Test
public void testToString() throws Exception {
System.out.println(jsonObject.toString());
}
@Test
public void testConvert() throws Exception {
JsonObject parse = jsonObject.parse("{\"bigDecimal1\":1,\"bigInteger1\":1,\"boolean1\":true,\"double1\":1,\"float1\":1,\"integer1\":1,\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":{\"k1\":\"v1\"},\"long1\":1}");
System.out.println(parse.toString());
}
}
package cn.zytx.common.json;
import cn.zytx.common.json.impl.JSONArray;
import cn.zytx.common.json.impl.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
/**
* 使用的时候不会出现跟具体JSON框架耦合的代码
*/
public class JSONArrayTest {
private JsonArray jsonArray = new JSONArray();
@Test
public void unwrap() throws Exception {
Object unwrap = jsonArray.unwrap();
Assert.assertEquals(com.alibaba.fastjson.JSONArray.class , unwrap.getClass());
}
@Test
public void parse() throws Exception {
String src = "[\"12\",\"13\"]";
JsonArray array = jsonArray.parse(src);
Assert.assertEquals(2 , jsonArray.size());
Assert.assertEquals(2 , array.size());
}
@Test
public void size() throws Exception {
Assert.assertEquals(0 , jsonArray.size());
jsonArray.put("12");
Assert.assertEquals(1 , jsonArray.size());
}
@Test(expected = JsonException.class)
public void get() throws Exception {
jsonArray.setStrict(true);
jsonArray.put("12");
Assert.assertEquals("12" , jsonArray.get(0));
jsonArray.get(1);
}
@Test
public void getString() throws Exception {
jsonArray.put("12");
Assert.assertEquals("12" , jsonArray.getString(0));
}
@Test
public void getBoolean() throws Exception {
}
@Test
public void getInteger() throws Exception {
}
@Test
public void getLong() throws Exception {
}
@Test
public void getDouble() throws Exception {
}
@Test
public void getFloat() throws Exception {
}
@Test
public void getBigInteger() throws Exception {
}
@Test
public void getBigDecimal() throws Exception {
}
@Test
public void getJsonObject() throws Exception {
JsonObject jsonObject = new JSONObject();
jsonObject.put("k1" , "v1");
jsonObject.put("k2" , "v2");
jsonArray.put(jsonObject);
Assert.assertEquals("{\"k1\":\"v1\",\"k2\":\"v2\"}" , jsonArray.getJsonObject(0).toString());
}
@Test
public void getJsonArray() throws Exception {
JsonArray array = new JSONArray();
array.put("12");
array.put("13");
JsonArray array2 = new JSONArray();
array2.put("122");
array2.put("132");
jsonArray.put(array);
jsonArray.put(array2);
Assert.assertEquals("[\"12\",\"13\"]" , jsonArray.getJsonArray(0).toString());
}
@Test
public void remove() throws Exception {
JsonArray array = new JSONArray();
array.put("12");
array.put("13");
JsonArray array2 = new JSONArray();
array2.put("122");
array2.put("132");
jsonArray.put(array);
jsonArray.put(array2);
jsonArray.remove(0);
Assert.assertEquals("[[\"122\",\"132\"]]" , jsonArray.toString());
}
@Test
public void clear() throws Exception {
JsonArray array = new JSONArray();
array.put("12");
array.put("13");
JsonArray array2 = new JSONArray();
array2.put("122");
array2.put("132");
jsonArray.clear();
Assert.assertEquals("[]" , jsonArray.toString());
}
@Test
public void put() throws Exception {
jsonArray.put("12");
jsonArray.put("13");
Assert.assertEquals(2 , jsonArray.size());
}
@Test
public void putWithIndex() throws Exception {
jsonArray.put("12");
jsonArray.put(0 ,"13");
Assert.assertEquals(1 , jsonArray.size());
Assert.assertEquals("13" , jsonArray.getString(0));
}
@Test
public void putAll() throws Exception {
jsonArray.putAll(Arrays.asList("12","12","13","13"));
Assert.assertEquals(String.class , jsonArray.get(0).getClass());
Assert.assertEquals(4 , jsonArray.size());
}
@Test
public void testToString() throws Exception {
JsonObject jsonObject = new JSONObject();
jsonObject.put("k1" , "v1");
jsonObject.put("k2" , "v2");
jsonArray.put(jsonObject);
Assert.assertEquals("[{\"k1\":\"v1\",\"k2\":\"v2\"}]" , jsonArray.toString());
}
@Test
public void testFromList() throws Exception {
List<Object> list = Arrays.asList("12","21",12);
JsonArray array = jsonArray.fromList(list);
Assert.assertEquals("[\"12\",\"21\",12]" , array.toString());
}
}
2.orgJson实现
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonException;
import cn.zytx.common.json.JsonObject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author xiongshiyan at 2018/6/10
*/
public class JSONObject extends BaseJson<JSONObject> implements JsonObject {
private org.json.JSONObject jsonObject;
public JSONObject(org.json.JSONObject jsonObject){
this.jsonObject = jsonObject;
}
public JSONObject(Map<String , Object> map){
this.jsonObject = new org.json.JSONObject(map);
}
public JSONObject(){
this.jsonObject = new org.json.JSONObject();
}
public JSONObject(String jsonString){
this.jsonObject = new org.json.JSONObject(jsonString);
}
@Override
public org.json.JSONObject unwrap() {
return jsonObject;
}
@Override
public Object get(String key) {
assertKey(key);
return checkNullValue(key , jsonObject.opt(key));
}
@Override
public Object get(String key, Object defaultObject) {
assertKey(key);
Object temp = jsonObject.opt(key);
return null == temp ? defaultObject : temp;
}
@Override
public JsonObject getJsonObject(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.jsonObject.opt(key);
Object t = checkNullValue(key, temp);
if(t instanceof org.json.JSONObject){
return new JSONObject((org.json.JSONObject) t);
}
if(t instanceof Map){
return new JSONObject((Map<String, Object>) t);
}
return (JsonObject) t;
}
@Override
public JsonArray getJsonArray(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.jsonObject.opt(key);
Object t = checkNullValue(key, temp);
if(t instanceof org.json.JSONArray){
return new JSONArray((org.json.JSONArray)t);
}
if(t instanceof List){
return new JSONArray((List)t);
}
return (JsonArray) t;
}
@Override
public String getString(String key) {
return getString(key , null);
}
@Override
public String getString(String key, String defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
String temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , String.class);
}else {
temp = this.jsonObject.getString(key);
}
return temp;
}
@Override
public Boolean getBoolean(String key) {
return getBoolean(key , null);
}
@Override
public Boolean getBoolean(String key, Boolean defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Boolean temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , Boolean.class);
}else {
temp = this.jsonObject.getBoolean(key);
}
return temp;
}
@Override
public Integer getInteger(String key) {
return getInteger(key , null);
}
@Override
public Integer getInteger(String key, Integer defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Integer temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , Integer.class);
}else {
temp = this.jsonObject.getInt(key);
}
return temp;
}
@Override
public Long getLong(String key) {
return getLong(key , null);
}
@Override
public Long getLong(String key, Long defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Long temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , Long.class);
}else {
temp = this.jsonObject.getLong(key);
}
return temp;
}
@Override
public Float getFloat(String key) {
return getFloat(key , null);
}
@Override
public Float getFloat(String key, Float defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Float temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , Float.class);
}else {
temp = this.jsonObject.getFloat(key);
}
return temp;
}
@Override
public Double getDouble(String key) {
return getDouble(key , null);
}
@Override
public Double getDouble(String key, Double defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Double temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , Double.class);
}else {
temp = this.jsonObject.getDouble(key);
}
return temp;
}
@Override
public BigInteger getBigInteger(String key) {
return getBigInteger(key , null);
}
@Override
public BigInteger getBigInteger(String key, BigInteger defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
BigInteger temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , BigInteger.class);
}else {
temp = this.jsonObject.getBigInteger(key);
}
return temp;
}
@Override
public BigDecimal getBigDecimal(String key) {
return getBigDecimal(key , null);
}
@Override
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , defaultValue);
}
BigDecimal temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , BigDecimal.class);
}else {
temp = this.jsonObject.getBigDecimal(key);
}
return temp;
}
@Override
public <T> T get(String key, Class<T> clazz) {
assertKey(key);
boolean has = jsonObject.has(key);
if(!has){
return checkNullValue(key , null);
}
T temp = ValueCompatible.compatibleValue(this.jsonObject.get(key) , clazz);
return checkNullValue(key, temp);
}
@Override
public Set<String> keySet() {
return jsonObject.keySet();
}
@Override
public int size() {
return jsonObject.length();
}
@Override
public boolean isEmpty() {
return jsonObject.length() == 0;
}
@Override
public boolean containsKey(String key) {
return jsonObject.has(key);
}
@Override
public JsonObject put(String key, Object value) {
jsonObject.put(key, value);
return this;
}
@Override
public JsonObject putAll(Map<? extends String, ?> m) {
m.forEach(jsonObject::put);
return this;
}
@Override
public void clear() {
Set<String> keySet = jsonObject.keySet();
for (Object key : keySet.toArray()) {
jsonObject.remove(key.toString());
}
}
@Override
public Object remove(String key) {
return jsonObject.remove(key);
}
@Override
public JsonObject parse(String jsonString) {
jsonObject = new org.json.JSONObject(jsonString);
return this;
//return new JSONObject(jsonString);
}
@Override
public JsonObject fromMap(Map<String, Object> map) {
this.jsonObject = new org.json.JSONObject(map);
return new JSONObject(map);
}
@Override
public String serialize(Object javaBean) {
throw new JsonException(new UnsupportedOperationException());
}
@Override
public <T> T deserialize(String jsonString, Class<T> clazz) {
throw new JsonException(new UnsupportedOperationException());
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<String , Json> map = new HashMap<>();
for (String key : jsonObject.keySet()) {
Object o = jsonObject.get(key);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(key , (Json) o);
}
}
map.forEach((k , v)-> jsonObject.put(k , v.unwrap()));
return jsonObject.toString();
}
@Override
public int hashCode() {
return jsonObject.hashCode();
}
@Override
public boolean equals(Object obj) {
return jsonObject.equals(obj);
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* @author xiongshiyan at 2018/6/11
*/
public class JSONArray extends BaseJson<JSONArray> implements JsonArray {
private org.json.JSONArray jsonArray;
public JSONArray(org.json.JSONArray jsonArray){
this.jsonArray = jsonArray;
}
public JSONArray(List<Object> list){
this.jsonArray = new org.json.JSONArray(list);
}
public JSONArray(){
this.jsonArray = new org.json.JSONArray();
}
public JSONArray(String arrayString){
this.jsonArray = new org.json.JSONArray(arrayString);
}
@Override
public org.json.JSONArray unwrap() {
return jsonArray;
}
@Override
public JsonArray parse(String arrayString) {
jsonArray = new org.json.JSONArray(arrayString);
return this;
//return new JSONArray(arrayString);
}
@Override
public int size() {
return jsonArray.length();
}
@Override
public Object get(int index) {
assertIndex(index , size());
Object opt = jsonArray.opt(index);
return checkNullValue(index,opt);
}
@Override
public String getString(int index) {
assertIndex(index , size());
String temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), String.class);
}else {
temp = jsonArray.getString(index);
}
return checkNullValue(index , temp);
}
@Override
public Boolean getBoolean(int index) {
assertIndex(index , size());
Boolean temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), Boolean.class);
}else {
temp = jsonArray.getBoolean(index);
}
return checkNullValue(index , temp);
}
@Override
public Integer getInteger(int index) {
assertIndex(index , size());
Integer temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), Integer.class);
}else {
temp = jsonArray.getInt(index);
}
return checkNullValue(index , temp);
}
@Override
public Long getLong(int index) {
assertIndex(index , size());
Long temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), Long.class);
}else {
temp = jsonArray.getLong(index);
}
return checkNullValue(index , temp);
}
@Override
public Double getDouble(int index) {
assertIndex(index , size());
Double temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), Double.class);
}else {
temp = jsonArray.getDouble(index);
}
return checkNullValue(index , temp);
}
@Override
public Float getFloat(int index) {
assertIndex(index , size());
Float temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), Float.class);
}else {
temp = jsonArray.getFloat(index);
}
return checkNullValue(index , temp);
}
@Override
public BigInteger getBigInteger(int index) {
assertIndex(index , size());
BigInteger temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), BigInteger.class);
}else {
temp = jsonArray.getBigInteger(index);
}
return checkNullValue(index , temp);
}
@Override
public BigDecimal getBigDecimal(int index) {
assertIndex(index , size());
BigDecimal temp;
if(isTolerant()){
temp = ValueCompatible.compatibleValue(jsonArray.opt(index), BigDecimal.class);
}else {
temp = jsonArray.getBigDecimal(index);
}
return checkNullValue(index , temp);
}
@Override
public JsonObject getJsonObject(int index) {
assertIndex(index , size());
Object opt = jsonArray.opt(index);
if(opt instanceof org.json.JSONObject){
return new JSONObject((org.json.JSONObject)opt);
}
if(opt instanceof Map){
return new JSONObject((Map<String, Object>) opt);
}
return (JsonObject) opt;
}
@Override
public JsonArray getJsonArray(int index) {
assertIndex(index , size());
Object opt = jsonArray.opt(index);
if(opt instanceof org.json.JSONArray){
return new JSONArray((org.json.JSONArray)opt);
}
if(opt instanceof List){
return new JSONArray((List<Object>) opt);
}
return (JsonArray) opt;
}
@Override
public JsonArray remove(int index) {
assertIndex(index , size());
Object remove = jsonArray.remove(index);
return this;
}
@Override
public JsonArray clear() {
int length = jsonArray.length();
for(int i=0; i<length; i++){
jsonArray.remove(i);
}
return this;
}
@Override
public JsonArray put(Object o) {
jsonArray.put(o);
return this;
}
@Override
public JsonArray put(int index, Object o) {
jsonArray.put(index , o);
return this;
}
@Override
public JsonArray putAll(Collection<?> os) {
os.forEach(v->jsonArray.put(v));
return this;
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<Integer , Json> map = new HashMap<>();
int size = size();
for (int i = 0; i < size; i++) {
Object o = jsonArray.get(i);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(i , (Json) o);
}
}
map.forEach((k,v)->jsonArray.put(k , v.unwrap()));
return jsonArray.toString();
}
@Override
public int hashCode() {
return jsonArray.hashCode();
}
@Override
public boolean equals(Object obj) {
return jsonArray.equals(obj);
}
@Override
public JsonArray fromList(List<Object> list) {
this.jsonArray = new org.json.JSONArray(list);
return this;
}
}
需要一个兼容数据处理的工具类
package cn.zytx.common.json.impl;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* @author xiongshiyan at 2018/6/12
*/
public class ValueCompatible {
/**
* 将一个值转换为兼容类型的值
* @param value src
* @param toClazz 转换的类型
*/
public static <T> T compatibleValue(Object value , Class<T> toClazz){
if(value == null){return null;}
if(toClazz == String.class){
return (T)value.toString();
}
if(toClazz == Integer.class || toClazz == int.class){
if(value instanceof Number){
return (T)Integer.valueOf(((Number)value).intValue());
}else if(value instanceof String){
return (T)Integer.valueOf((String) value);
}
}
if(toClazz == Long.class || toClazz == long.class){
if(value instanceof Number){
return (T)Long.valueOf(((Number)value).longValue());
}else if(value instanceof String){
return (T)Long.valueOf((String) value);
}
}
if(toClazz == Double.class || toClazz == Double.class){
if(value instanceof Number){
return (T)Double.valueOf(((Number)value).doubleValue());
}else if(value instanceof String){
return (T)Double.valueOf((String) value);
}
}
if(toClazz == Float.class || toClazz == float.class){
if(value instanceof Number){
return (T)Float.valueOf(((Number)value).floatValue());
}else if(value instanceof String){
return (T)Float.valueOf((String) value);
}
}
if(toClazz == BigDecimal.class){
return (T)new BigDecimal(value.toString());
}
if(toClazz == BigInteger.class){
return (T)new BigInteger(value.toString());
}
if(toClazz == Boolean.class || toClazz == boolean.class){
return (T)Boolean.valueOf(value.toString());
}
if(toClazz == Character.class || toClazz == char.class){
return (T)value;
}
if(toClazz == Byte.class || toClazz == byte.class){
if(value instanceof Number){
return (T)Byte.valueOf(((Number)value).byteValue());
}else if(value instanceof String){
return (T)Byte.valueOf((String) value);
}
}
if(toClazz == Short.class || toClazz == short.class){
if(value instanceof Number){
return (T)Short.valueOf(((Number)value).shortValue());
}else if(value instanceof String){
return (T)Short.valueOf((String) value);
}
}
/*if(toClazz == java.util.Date.class){
if(value instanceof String){
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}else if(value instanceof Number){
return new Date(((Number) value).longValue());
}
}*/
return (T)value;
}
}
3.Gson实现、Jackson实现、jsonlib实现
这几个都基于map和list实现,只是对JavaBean的序列化和反序列化使用不同的方式。
package cn.zytx.common.json.impl;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 基于Map的JSONObject实现基类
* @author xiongshiyan at 2018/6/13
*/
public abstract class BaseMapJSONObject extends BaseJson<BaseMapJSONObject> implements JsonObject{
protected Map<String , Object> map;
public BaseMapJSONObject(Map<String , Object> map){
this.map = map;
}
public BaseMapJSONObject(){
this.map = new HashMap<>();
}
public BaseMapJSONObject(String jsonString){
this.map = str2Map(jsonString);
}
protected abstract Map<String , Object> str2Map(String jsonString);
@Override
public Map<String , Object> unwrap() {
return map;
}
@Override
public Object get(String key) {
assertKey(key);
return checkNullValue(key , map.get(key));
}
@Override
public Object get(String key, Object defaultObject) {
assertKey(key);
Object temp = map.get(key);
return null == temp ? defaultObject : temp;
}
@Override
public JsonObject getJsonObject(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof Map){
return new JSONObject((Map<String, Object>) t);
}
return (JsonObject) t;
}
@Override
public JsonArray getJsonArray(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof List){
return new JSONArray((List<Object>)t);
}
return (JsonArray) t;
}
@Override
public String getString(String key) {
return getString(key , null);
}
@Override
public String getString(String key, String defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
String temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, String.class);
}else {
temp = (String)value;
}
return temp;
}
@Override
public Boolean getBoolean(String key) {
return getBoolean(key , null);
}
@Override
public Boolean getBoolean(String key, Boolean defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Boolean temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Boolean.class);
}else {
temp = (Boolean)value;
}
return temp;
}
@Override
public Integer getInteger(String key) {
return getInteger(key , null);
}
@Override
public Integer getInteger(String key, Integer defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Integer temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Integer.class);
}else {
temp = (Integer)value;
}
return temp;
}
@Override
public Long getLong(String key) {
return getLong(key , null);
}
@Override
public Long getLong(String key, Long defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Long temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Long.class);
}else {
temp = (Long)value;
}
return temp;
}
@Override
public Float getFloat(String key) {
return getFloat(key , null);
}
@Override
public Float getFloat(String key, Float defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Float temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Float.class);
}else {
temp = (Float)value;
}
return temp;
}
@Override
public Double getDouble(String key) {
return getDouble(key , null);
}
@Override
public Double getDouble(String key, Double defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
Double temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Double.class);
}else {
temp = (Double)value;
}
return temp;
}
@Override
public BigInteger getBigInteger(String key) {
return getBigInteger(key , null);
}
@Override
public BigInteger getBigInteger(String key, BigInteger defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
BigInteger temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, BigInteger.class);
}else {
temp = (BigInteger)value;
}
return temp;
}
@Override
public BigDecimal getBigDecimal(String key) {
return getBigDecimal(key , null);
}
@Override
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
assertKey(key);
boolean has = map.containsKey(key);
if(!has){
return checkNullValue(key , defaultValue);
}
BigDecimal temp;
Object value = this.map.get(key);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, BigDecimal.class);
}else {
temp = (BigDecimal)value;
}
return temp;
}
@Override
public <T> T get(String key, Class<T> clazz) {
Object o = map.get(key);
return (T)o;
}
@Override
public Set<String> keySet() {
return map.keySet();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(String key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public JsonObject put(String key, Object value) {
map.put(key, value);
return this;
}
@Override
public JsonObject putAll(Map<? extends String, ?> m) {
map.putAll(m);
return this;
}
@Override
public void clear() {
map.clear();
}
@Override
public Object remove(String key) {
return map.remove(key);
}
@Override
public int hashCode() {
return map.hashCode();
}
@Override
public boolean equals(Object obj) {
return map.equals(obj);
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import com.google.gson.Gson;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* 基于List的JSONArray实现基类
* @author xiongshiyan at 2018/6/11
*/
public abstract class BaseListJSONArray extends BaseJson<BaseListJSONArray> implements JsonArray {
protected List<Object> list;
public BaseListJSONArray(List<Object> list){
this.list = list;
}
public BaseListJSONArray(){
this.list = new LinkedList<>();
}
public BaseListJSONArray(String arrayString){
this.list = str2List(arrayString);
}
public abstract List<Object> str2List(String arrayString);
@Override
public int size() {
return list.size();
}
@Override
public Object get(int index) {
assertIndex(index , size());
return checkNullValue(index , list.get(index));
}
@Override
public String getString(int index) {
assertIndex(index , size());
String temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, String.class);
}else {
temp = (String)value;
}
return checkNullValue(index , temp);
}
@Override
public Boolean getBoolean(int index) {
assertIndex(index , size());
Boolean temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Boolean.class);
}else {
temp = (Boolean)value;
}
return checkNullValue(index , temp);
}
@Override
public Integer getInteger(int index) {
assertIndex(index , size());
Integer temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Integer.class);
}else {
temp = (Integer)value;
}
return checkNullValue(index , temp);
}
@Override
public Long getLong(int index) {
assertIndex(index , size());
Long temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Long.class);
}else {
temp = (Long)value;
}
return checkNullValue(index , temp);
}
@Override
public Double getDouble(int index) {
assertIndex(index , size());
Double temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Double.class);
}else {
temp = (Double)value;
}
return checkNullValue(index , temp);
}
@Override
public Float getFloat(int index) {
assertIndex(index , size());
Float temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, Float.class);
}else {
temp = (Float)value;
}
return checkNullValue(index , temp);
}
@Override
public BigInteger getBigInteger(int index) {
assertIndex(index , size());
BigInteger temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, BigInteger.class);
}else {
temp = (BigInteger)value;
}
return checkNullValue(index , temp);
}
@Override
public BigDecimal getBigDecimal(int index) {
assertIndex(index , size());
BigDecimal temp;
Object value = list.get(index);
if(isTolerant()){
temp = ValueCompatible.compatibleValue(value, BigDecimal.class);
}else {
temp = (BigDecimal)value;
}
return checkNullValue(index , temp);
}
@Override
public JsonArray remove(int index) {
list.remove(index);
return this;
}
@Override
public JsonArray clear() {
list.clear();
return this;
}
@Override
public JsonArray put(Object o) {
list.add(o);
return this;
}
@Override
public JsonArray put(int index, Object o) {
list.remove(index);
list.add(index , o);
return this;
}
@Override
public JsonArray putAll(Collection<?> os) {
list.addAll(os);
return this;
}
@Override
public JsonArray parse(String jsonString) {
this.list = new Gson().fromJson(jsonString, List.class);
return this;
}
@Override
public List<Object> unwrap() {
return list;
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<Integer , Json> map = new HashMap<>();
int size = size();
for (int i = 0; i < size; i++) {
Object o = list.get(i);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(i , (Json) o);
}
}
map.forEach((k,v)->{
list.remove((int)k);
list.add(k , v.unwrap());
});
return new Gson().toJson(this.list , List.class);
}
@Override
public int hashCode() {
return list.hashCode();
}
@Override
public boolean equals(Object obj) {
return list.equals(obj);
}
}
Gson的实现
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
/**
* @author xiongshiyan at 2018/6/10
*/
public class JSONObject extends BaseMapJSONObject {
public JSONObject(Map<String , Object> map){
super(map);
}
public JSONObject(){
}
public JSONObject(String jsonString){
super(jsonString);
}
@Override
protected Map<String, Object> str2Map(String jsonString) {
return new Gson().fromJson(jsonString, Map.class);
}
@Override
public JsonObject parse(String jsonString) {
this.map = new Gson().fromJson(jsonString, Map.class);
return this;
}
@Override
public String serialize(Object javaBean) {
return new Gson().toJson(javaBean);
}
@Override
public <T> T deserialize(String jsonString, Class<T> clazz) {
return new Gson().fromJson(jsonString , clazz);
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<String , Json> map = new HashMap<>();
for (String key : map.keySet()) {
Object o = map.get(key);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(key , (Json) o);
}
}
map.forEach((k , v)-> this.map.put(k , v.unwrap()));
return new Gson().toJson(this.map , Map.class);
}
@Override
public JsonObject fromMap(Map<String, Object> map) {
return new JSONObject(map);
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import com.google.gson.Gson;
import java.util.*;
/**
* @author xiongshiyan at 2018/6/11
*/
public class JSONArray extends BaseListJSONArray {
public JSONArray(List<Object> list){
super(list);
}
public JSONArray(){
}
public JSONArray(String arrayString){
super(arrayString);
}
@Override
public List<Object> str2List(String arrayString) {
return new Gson().fromJson(arrayString , List.class);
}
@Override
public JsonObject getJsonObject(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof Map){
return new JSONObject((Map<String, Object>) opt);
}
return (JsonObject) opt;
}
@Override
public JsonArray getJsonArray(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof List){
return new JSONArray((List<Object>) opt);
}
return (JsonArray) opt;
}
@Override
public JsonArray fromList(List<Object> list) {
this.list = list;
return this;
}
}
Jackson实现
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonException;
import cn.zytx.common.json.JsonObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xiongshiyan at 2018/6/10
*/
public class JSONObject extends BaseMapJSONObject {
private static final ObjectMapper objectMapper = new ObjectMapper();
public JSONObject(Map<String , Object> map){
super(map);
}
public JSONObject(){
super();
}
public JSONObject(String jsonString){
super(jsonString);
}
@Override
protected Map<String, Object> str2Map(String jsonString) {
try {
return objectMapper.readValue(jsonString , Map.class);
} catch (IOException e) {
throw new JsonException(e);
}
}
@Override
public JsonObject getJsonObject(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof Map){
return new JSONObject((Map<String, Object>) t);
}
return (JsonObject) t;
}
@Override
public JsonArray getJsonArray(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof List){
return new JSONArray((List<Object>)t);
}
return (JsonArray) t;
}
@Override
public JsonObject parse(String jsonString) {
try {
Map map = objectMapper.readValue(jsonString, Map.class);
this.map = map;
return this;
} catch (IOException e) {
throw new JsonException(e);
}
}
@Override
public String serialize(Object javaBean) {
try {
return objectMapper.writeValueAsString(javaBean);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
@Override
public <T> T deserialize(String jsonString, Class<T> clazz) {
try {
return objectMapper.readValue(jsonString , clazz);
} catch (IOException e) {
throw new JsonException(e);
}
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<String , Json> map = new HashMap<>();
for (String key : map.keySet()) {
Object o = map.get(key);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(key , (Json) o);
}
}
map.forEach((k , v)-> this.map.put(k , v.unwrap()));
try {
return objectMapper.writeValueAsString(this.map);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
@Override
public JsonObject fromMap(Map<String, Object> map) {
return new JSONObject(map);
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonException;
import cn.zytx.common.json.JsonObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.*;
/**
* @author xiongshiyan at 2018/6/11
*/
public class JSONArray extends BaseListJSONArray {
private static final ObjectMapper objectMapper = new ObjectMapper();
public JSONArray(List<Object> list){
super(list);
}
public JSONArray(){
super();
}
public JSONArray(String arrayString){
super(arrayString);
}
@Override
public List<Object> str2List(String arrayString) {
try {
return objectMapper.readValue(arrayString , List.class);
} catch (IOException e) {
throw new JsonException(e);
}
}
@Override
public JsonObject getJsonObject(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof Map){
return new JSONObject((Map<String, Object>) opt);
}
return (JsonObject) opt;
}
@Override
public JsonArray getJsonArray(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof List){
return new JSONArray((List<Object>) opt);
}
return (JsonArray) opt;
}
@Override
public JsonArray parse(String jsonString) {
try {
this.list = objectMapper.readValue(jsonString, List.class);
return this;
} catch (IOException e) {
throw new JsonException(e);
}
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<Integer , Json> map = new HashMap<>();
int size = size();
for (int i = 0; i < size; i++) {
Object o = list.get(i);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(i , (Json) o);
}
}
map.forEach((k,v)->{
list.remove((int)k);
list.add(k , v.unwrap());
});
try {
return objectMapper.writeValueAsString(this.list);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
@Override
public JsonArray fromList(List<Object> list) {
/*String jsonString = objectMapper.writeValueAsString(list);
this.list = objectMapper.readValue(jsonString, List.class);*/
this.list = list;
return this;
}
}
Jsonlib实现
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import net.sf.ezmorph.bean.MorphDynaBean;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xiongshiyan at 2018/6/10
*/
public class JSONObject extends BaseMapJSONObject {
public JSONObject(Map<String , Object> map){
super(map);
}
public JSONObject(){
super();
}
public JSONObject(String jsonString){
super(jsonString);
}
@Override
protected Map<String, Object> str2Map(String jsonString) {
Map<String, Object> o = (Map<String, Object>) net.sf.json.JSONObject.toBean(
net.sf.json.JSONObject.fromObject(jsonString), Map.class);
Map<String , Object> temp = new HashMap<>();
o.forEach((k , v)->{
if(v instanceof MorphDynaBean){
Field[] declaredFields = MorphDynaBean.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
if("dynaValues".equals(declaredField.getName())){
try {
declaredField.setAccessible(true);
Object o1 = declaredField.get(v);
temp.put(k , o1);
} catch (IllegalAccessException e) { }
break;
}
}
}
});
o.putAll(temp);
return o;
}
@Override
public JsonObject getJsonObject(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof Map){
return new JSONObject((Map<String, Object>) t);
}
return (JsonObject) t;
}
@Override
public JsonArray getJsonArray(String key) {
assertKey(key);
//这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
//这里就是各种JsonObject不能混用的原因
Object temp = this.map.get(key);
Object t = checkNullValue(key, temp);
if(t instanceof List){
return new JSONArray((List<Object>)t);
}
return (JsonArray) t;
}
@Override
public JsonObject parse(String jsonString) {
this.map = str2Map(jsonString);
return this;
}
@Override
public String serialize(Object javaBean) {
return net.sf.json.JSONObject.fromObject(javaBean).toString();
}
@Override
public <T> T deserialize(String jsonString, Class<T> clazz) {
return (T)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(jsonString) , clazz);
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<String , Json> map = new HashMap<>();
for (String key : map.keySet()) {
Object o = map.get(key);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(key , (Json) o);
}
}
map.forEach((k , v)-> this.map.put(k , v.unwrap()));
return net.sf.json.JSONObject.fromObject(this.map).toString();
}
}
package cn.zytx.common.json.impl;
import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonArray;
import cn.zytx.common.json.JsonObject;
import java.util.*;
/**
* @author xiongshiyan at 2018/6/11
*/
public class JSONArray extends BaseListJSONArray {
public JSONArray(List<Object> list){
super(list);
}
public JSONArray(){
super();
}
public JSONArray(String arrayString){
super(arrayString);
}
@Override
public List<Object> str2List(String arrayString) {
return net.sf.json.JSONArray.toList(net.sf.json.JSONArray.fromObject(arrayString));
}
@Override
public JsonObject getJsonObject(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof Map){
return new JSONObject((Map<String, Object>) opt);
}
return (JsonObject) opt;
}
@Override
public JsonArray getJsonArray(int index) {
assertIndex(index , size());
Object opt = list.get(index);
if(opt instanceof List){
return new JSONArray((List<Object>) opt);
}
return (JsonArray) opt;
}
@Override
public JsonArray parse(String jsonString) {
this.list = str2List(jsonString);
return this;
}
@Override
public String toString() {
//需要针对JsonObject/JsonArray处理
Map<Integer , Json> map = new HashMap<>();
int size = size();
for (int i = 0; i < size; i++) {
Object o = list.get(i);
if(o instanceof JsonObject || o instanceof JsonArray){
map.put(i , (Json) o);
}
}
map.forEach((k,v)->{
list.remove((int)k);
list.add(k , v.unwrap());
});
return net.sf.json.JSONArray.fromObject(this.list).toString();
}
}