All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.centit.support.json.JSONOpt Maven / Gradle / Ivy

There is a newer version: 5.3.2302
Show newest version
package com.centit.support.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.support.algorithm.ReflectionOpt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
/**
 * 
 * 对JSON对象进行操作,目前只能对一维数组进行操作,
 * 但这不意味只能有一维数据,你可以给一维数组中的变量设置为任意值,包括数值,只是不能再次操作这个数值中的值
 * 
 * @author codefan
 * @version $Rev$ 
* $Id$ */ @SuppressWarnings("unused") public abstract class JSONOpt { private JSONOpt() { throw new IllegalAccessError("Utility class"); } protected static final Logger logger = LoggerFactory.getLogger(JSONOpt.class); static class JSONPath{ JSONObject objJson; String path; boolean found; int pathPos; }; static class JSONKey{ String skey; int ind; }; /* * 目前只支持一维数值 * @return */ private static JSONKey praseJosnKey(String skey){ JSONKey key = new JSONKey(); key.ind = -1; int nL = skey.indexOf('['); if(nL>0){ key.skey = skey.substring(0, nL); int nR = skey.indexOf(']'); if(nR > nL+1) key.ind = Integer.parseInt(skey.substring(nL+1,nR)); else key.ind = 0; }else key.skey = skey; return key; } private static JSONPath findJsonObject(JSONObject objJson,int depth,String [] skeys){ JSONPath p = new JSONPath(); //p.found = false; //p.pathPos = 0; int nLast = 0; JSONObject lastKeyJson=objJson; while(nLast=0) break; lastKeyJson =(JSONObject)obj; nLast++; }else if( obj instanceof JSONArray ){ if(key.ind<0) break; JSONArray jarray = (JSONArray)obj; if(key.ind>=jarray.size()) break; Object obj2 = jarray.get(key.ind); if(obj2 instanceof JSONObject){ lastKeyJson =(JSONObject)obj2; nLast++; }else break; }else break; } p.found = true; p.pathPos = nLast; p.objJson = lastKeyJson; return p; } /** * 获取一个json对象的属性,skeys是json属性数组,a.b 这样的属性对应的数组为{'a','b'} * @param objJson objJson * @param skeys json属性数组,a.b 这样的属性对应的数组为{'a','b'} * @return 获取一个json对象的属性 */ public static JSONObject findJsonObject(JSONObject objJson,String [] skeys){ int depth = skeys.length; if(depth<1) return null; JSONPath path = findJsonObject(objJson,depth,skeys); if(path.pathPos != depth ) return null; return path.objJson; } /** * 获取一个json对象的属性,path是json属性 可以有多层,用'.' 分隔 ,比如 a.b * @param objJson objJson * @param path json属性 可以有多层,用'.' 分隔 ,比如 a.b * @return 获取一个json对象的属性 */ public static JSONObject findJsonObject(JSONObject objJson,String path){ String [] skeys = path.split("\\x2E"); /* if(skeys==null) return null;*/ return findJsonObject(objJson,skeys); } private static JSONObject createJsonObject(String [] skeys , int beginPos ,Object value){ int depth = skeys.length; if(depth==0) return null; int nLast = beginPos; JSONKey key = praseJosnKey( skeys[depth-1]); JSONObject leafKey = new JSONObject(); if(key.ind<0){ leafKey.put(key.skey, value); }else{ JSONArray jarray = new JSONArray(); for(int i=0;i=nLast){ key = praseJosnKey( skeys[depth]); JSONObject tempKey = new JSONObject(); if(key.ind<0) tempKey.put(key.skey, leafKey); else{ JSONArray jarray = new JSONArray(); for(int i=0;i ent : ((Map)value).entrySet() ){ jpath.objJson.element(ent.getKey().toString(), ent.getValue()); } }else */ jpath.objJson.put(key.skey, jsonValue); }else{ //判断是否有节点,如果有设置对应的变量 if(jpath.objJson.containsKey(key.skey)){ Object leafJson = jpath.objJson.get(key.skey); //如果变量已经是数组,设置对应的值 if(leafJson instanceof JSONArray){ JSONArray jarray = ((JSONArray)leafJson); //对应位置的数值不存在 if(jarray.size() ent : ((Map)value).entrySet() ) ((JSONObject)obj).put(ent.getKey().toString(), ent.getValue()); else jarray.set(key.ind, jsonValue); }else jarray.set(key.ind, jsonValue); } }else{//不是数组,设置为数据 JSONArray jarray = new JSONArray(); for(int i=0;i values) { for(Object obj : values) appendData(objJson,path,obj); } public static String objectToJSONString(Object obj ){ return objectToJSONString(obj,false,false); } public static String objectToJSONString(Object obj,boolean methodOnly, boolean fieldOnly ){ return objectToJSONString(obj,methodOnly,fieldOnly,false); } public static String objectToJSONString(Object obj,boolean methodOnly, boolean fieldOnly, boolean includePrivateField ){ if(obj == null) return null; if(ReflectionOpt.isScalarType(obj.getClass())){ //StringBaseOpt.objectToString(obj); return obj.toString(); } if(obj instanceof JSON) return ((JSON)obj).toJSONString(); if( ReflectionOpt.isArray(obj )) return arrayToJSONArray(obj,methodOnly,fieldOnly,includePrivateField).toJSONString(); return objectToJSONObject(obj,methodOnly,fieldOnly,includePrivateField).toJSONString(); } /** * 将一个对象转换为JSON, 如果是 数值 或者 实现collect接口 则转换为 JSONArray否则转换为JSONObject * @param obj 将一个对象转换为JSON * @return 转换为 JSONArray否则转换为JSONObject */ public static JSON objectToJSON(Object obj ){ return objectToJSON(obj,false,false); } /* 将一个对象转换为JSON, 如果是 数值 或者 实现collect接口 则转换为 JSONArray否则转换为JSONObject * methodOnly 和 fieldOnly 不能同时为 true * @param obj * @param methodOnly * @param fieldOnly * @param includePrivateField 包括私有属性,methodOnly 为true是这个参数无效 * @return */ public static JSON objectToJSON(Object obj,boolean methodOnly, boolean fieldOnly, boolean includePrivateField ){ if(obj instanceof JSON) return (JSON)obj; if( ReflectionOpt.isArray(obj )) return arrayToJSONArray(obj,methodOnly,fieldOnly,includePrivateField); else return objectToJSONObject(obj,methodOnly,fieldOnly,includePrivateField); } /* 将一个对象转换为JSON, 如果是 数值 或者 实现collect接口 则转换为 JSONArray否则转换为JSONObject * methodOnly 和 fieldOnly 不能同时为 true * @param obj * @param methodOnly * @param fieldOnly * @return */ public static JSON objectToJSON(Object obj,boolean methodOnly, boolean fieldOnly ){ return objectToJSON(obj,methodOnly,fieldOnly,false); } /* * 将一个对象转换为JSON对象,method方法优先 * @param obj * @return */ public static JSONObject objectToJSONObject(Object obj){ return objectToJSONObject(obj,false,false,false); } /* * 将一个对象转换为 JSON 值, 如果是简单类型则转换为一个 String * @param value * @param methodOnly * @param fieldOnly * @return */ private static Object makeJSONValue(Object value,boolean methodOnly, boolean fieldOnly){ if(value==null) return null;//""; else if(ReflectionOpt.isScalarType(value.getClass())) return value;// String.valueof(value) else{ return objectToJSON(value,methodOnly, fieldOnly); } } /* 将一个对象转换为JSON对象 * methodOnly 和 fieldOnly 不能同时为 true * @param obj * @param methodOnly * @param fieldOnly * @param includePrivateField 包括私有属性,methodOnly 为true是这个参数无效 * @return */ public static JSONObject objectToJSONObject(Object obj,boolean methodOnly, boolean fieldOnly,boolean includePrivateField){ if(obj == null) return null; if(obj instanceof JSONObject) return (JSONObject)obj; JSONObject jObj = new JSONObject(); if(ReflectionOpt.isScalarType(obj.getClass())){ jObj.put("value",obj.toString()); return jObj; } if( obj instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map)obj; for(Map.Entry ent : map.entrySet() ){ jObj.put(ent.getKey().toString(), makeJSONValue( ent.getValue(),methodOnly, fieldOnly)); } return jObj; } List methodNames = new ArrayList(); List getMethods = ReflectionOpt.getAllGetterMethod(obj.getClass()); if(! fieldOnly && getMethods !=null){ for(Method m :getMethods ){ String fieldName = ReflectionOpt.methodNameToField(m.getName()); methodNames.add(fieldName); Object value = null; try { value = m.invoke(obj); } catch (Exception e) { logger.error(e.getMessage(),e);//e.printStackTrace(); } jObj.put(fieldName, makeJSONValue(value,methodOnly, fieldOnly)); } } //obj.getClass().getDeclaredFields(); Field[] fls =ReflectionOpt.getFields(obj); if(! methodOnly && fls !=null){ for (Field fl : fls) { if(methodNames.contains(fl.getName())) continue; if(includePrivateField || Modifier.isPublic(fl.getModifiers())){ //fl.isAccessible() Object value = ReflectionOpt.forceGetFieldValue(obj,fl); jObj.put(fl.getName(), makeJSONValue(value,methodOnly, fieldOnly)); } } } if((fls==null || fls.length<1) &&(getMethods==null||getMethods.size()<1) ){ jObj.put("value",obj.toString()); } return jObj; } public static JSONObject objectToJSONObject(Object obj,boolean methodOnly, boolean fieldOnly){ return objectToJSONObject( obj, methodOnly, fieldOnly,false); } /* * 将一个对象转换为JSON对象,method方法优先 * @param obj * @return */ public static JSONArray arrayToJSONArray(Object obj){ return arrayToJSONArray(obj,false,false,false); } /** 将一个对象转换为JSON对象 * methodOnly 和 fieldOnly 不能同时为 true * @param objArray objArray * @param methodOnly methodOnly * @param fieldOnly fieldOnly * @param includePrivateField 包括私有属性,methodOnly 为true是这个参数无效 * @return 将一个对象转换为JSON对象 */ public static JSONArray arrayToJSONArray(Object objArray,boolean methodOnly, boolean fieldOnly,boolean includePrivateField){ if(objArray == null) return null; if(objArray instanceof JSONArray) return (JSONArray)objArray; JSONArray jArray = new JSONArray(); if(objArray instanceof Object[]){ Object[] objList = (Object[])objArray; for(Object obj:objList ){ jArray.add(makeJSONValue(obj,methodOnly, fieldOnly)); } }else if(objArray instanceof Collection){ @SuppressWarnings("unchecked") Collection objList = (Collection)objArray; for(Object obj:objList ){ jArray.add(makeJSONValue(obj,methodOnly, fieldOnly)); } }else{ //System.out.println( objArray.getClass().getName()); if(int[].class.equals(objArray.getClass())){ int[] objList = (int[])objArray; for(int obj:objList ){ jArray.add(obj);//String.valueOf } }else if(long[].class.equals(objArray.getClass())){//是J不是L没有写错。 long[] objList = (long[])objArray; for(long obj:objList ){ jArray.add(obj); } }else if(float[].class.equals(objArray.getClass())){ float[] objList = (float[])objArray; for(float obj:objList ){ jArray.add(obj); } }else if(double[].class.equals(objArray.getClass())){ double[] objList = (double[])objArray; for(double obj:objList ){ jArray.add(obj); } }else if(char[].class.equals(objArray.getClass())){ char[] objList = (char[])objArray; for(char obj:objList ){ jArray.add(obj); } } } return jArray; } public static JSONArray arrayToJSONArray(Object objArray,boolean methodOnly, boolean fieldOnly){ return arrayToJSONArray( objArray, methodOnly, fieldOnly, false); } /* * 参数必须是 string object string object .... * @param objs * @return */ public static Map createHashMap(Object... objs){ if(objs==null || objs.length<2) return null; Map paramsMap = new HashMap<>(objs.length); for(int i=0;i