2121import java .util .ArrayList ;
2222import java .util .HashMap ;
2323import java .util .LinkedHashMap ;
24+ import java .util .LinkedHashSet ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .Set ;
@@ -110,6 +111,8 @@ public JSONObject parseResponse(String request) {
110111 * @return requestObject
111112 */
112113 public JSONObject parseResponse (JSONObject request ) {
114+ final String requestString = JSON .toJSONString (request );//request传进去解析后已经变了
115+
113116 relationMap = new HashMap <String , String >();
114117 parseRelation = false ;
115118
@@ -137,8 +140,8 @@ public JSONObject parseResponse(JSONObject request) {
137140 // }
138141
139142 System .out .println ("\n \n \n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
140- + TAG + requestMethod + "/parseResponse request = \n " + JSON . toJSONString ( request ) );
141-
143+ + TAG + requestMethod + "/parseResponse request = \n " + requestString );
144+
142145 return requestObject ;
143146 }
144147
@@ -156,7 +159,7 @@ public static JSONObject parseRequest(String request, RequestMethod method) {
156159 method = RequestMethod .GET ;
157160 }
158161 System .out .println ("\n \n \n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n " + TAG + method
159- + "/parseResponse request = \n " + request );
162+ + "/parseResponse request = \n " + request );
160163 return JSON .parseObject (request );
161164 }
162165
@@ -416,7 +419,7 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
416419 , final JSONObject request ) throws Exception {
417420 System .out .println (TAG + "\n getObject: parentPath = " + parentPath
418421 + ";\n name = " + name + "; request = " + JSON .toJSONString (request ));
419- if (request == null ) {//key-value条件
422+ if (request == null ) {// Moment:{} || request.isEmpty()) {// key-value条件
420423 return null ;
421424 }
422425 final String path = getPath (parentPath , name );
@@ -438,11 +441,16 @@ && isInRelationMap(path) == false) {
438441
439442 boolean containRelation = false ;
440443
441- Set <String > set = request .keySet ();
442- JSONObject transferredRequest = new JSONObject (true );
443- Map <String , String > functionMap = new LinkedHashMap <String , String >();
444- Map <String , Object > selfDefineKeyMap = new LinkedHashMap <String , Object >();
445- if (set != null ) {
444+ JSONObject transferredRequest = null ;
445+ Map <String , String > functionMap = null ;
446+ Map <String , Object > selfDefineKeyMap = null ;
447+
448+ Set <String > set = new LinkedHashSet <>(request .keySet ());
449+ if (set != null && set .size () > 0 ) {//判断换取少几个变量的初始化是否值得?
450+ transferredRequest = new JSONObject (true );
451+ functionMap = new LinkedHashMap <String , String >();
452+ selfDefineKeyMap = new LinkedHashMap <String , Object >();
453+
446454 Object value ;
447455 JSONObject result ;
448456 boolean isFirst = true ;
@@ -552,6 +560,7 @@ && isInRelationMap(path) == false) {
552560 }
553561 }
554562
563+
555564 if (containRelation == false && isTableKey (name )) {//提高性能
556565 if (parseRelation == false || isInRelationMap (path )) {//避免覆盖原来已经获取的
557566 // relationMap.remove(path);
@@ -563,16 +572,13 @@ && isInRelationMap(path) == false) {
563572 }
564573
565574 transferredRequest = getSQLObject (config2 );
566- //
567- // JSONObject result = getSQLObject(config2);
568- // if (result != null && result.isEmpty() == false) {//解决获取失败导致不能获取里面JSONObject
569- // transferredRequest = result;
570-
571575
572576 if (transferredRequest != null && transferredRequest .isEmpty () == false ) {//避免返回空的
573- transferredRequest .putAll (selfDefineKeyMap );
577+ if (selfDefineKeyMap != null ) {
578+ transferredRequest .putAll (selfDefineKeyMap );
579+ }
574580 //解析函数function
575- Set <String > functionSet = functionMap .keySet ();
581+ Set <String > functionSet = functionMap == null ? null : functionMap .keySet ();
576582 if (functionSet != null && functionSet .isEmpty () == false ) {
577583 for (String key : functionSet ) {
578584 transferredRequest .put (getRealKey (requestMethod , key , false )
@@ -581,12 +587,9 @@ && isInRelationMap(path) == false) {
581587 }
582588
583589 //可能客户端还有用,客户端自己解决,况且已知Android用fastjson解析不会崩溃 //移除自定义key
584- }
585-
586- if (parseRelation ) {
587590 putValueByPath (path , transferredRequest );//解决获取关联数据时requestObject里不存在需要的关联数据
588591 }
589- // }
592+
590593 }
591594 }
592595
@@ -607,7 +610,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
607610 , final JSONObject request ) throws Exception {
608611 System .out .println (TAG + "\n \n \n getArray parentPath = " + parentPath
609612 + "; name = " + name + "; request = " + JSON .toJSONString (request ));
610- if (request == null ) {//jsonKey-jsonValue条件
613+ if (request == null || request . isEmpty () ) {//jsonKey-jsonValue条件
611614 return null ;
612615 }
613616 String path = getPath (parentPath , name );
@@ -625,7 +628,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
625628 }
626629
627630 //最好先获取第一个table的所有项(where条件),填充一个列表?
628- Set <String > set = request .keySet ();
631+ Set <String > set = new LinkedHashSet <>( request .keySet () );
629632 if (count <= 0 || count > 10 ) {//10以下不优化长度
630633 if (parseRelation == false && set != null ) {
631634 Object object ;
@@ -646,6 +649,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
646649 if (count > total ) {
647650 count = total ;
648651 request .put (JSONRequest .KEY_COUNT , count );
652+ // set = request.keySet();
649653 }
650654 }
651655 }
@@ -656,55 +660,53 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
656660 QueryConfig config = new QueryConfig (requestMethod , count , page );
657661
658662 JSONObject transferredRequest = new JSONObject (true );
659- if (set != null ) {
660- JSONObject parent = null ;
661- Object value ;
662- JSONObject result ;
663- if (parseRelation == false ) {
664- //生成count个
665- for (int i = 0 ; i < count ; i ++) {
666- parent = new JSONObject (true );
667- for (String key : set ) {
668- value = request .get (key );
669- if (value instanceof JSONObject ) {//JSONObject
670- config .setPosition (i );
671- if (isArrayKey (key )) {//json array
672- result = getArray (getPath (path , "" + i ), config , key , (JSONObject ) value );
673- } else {//json object
674- result = getObject (getPath (path , "" + i ), config , key , (JSONObject ) value );
675- }
676- System .out .println (TAG + "getArray parseRelation == false"
677- + " >> i = " + i + "result = " + result );
678- if (result != null && result .isEmpty () == false ) {//只添加!=null的值,可能数据库返回数据不够count
679- parent .put (key , result );
680-
681- updateRelation (path , getPath (path , "" + i ));//request结构已改变,需要更新依赖关系
682- }
683- } else {//JSONArray或其它Object,直接填充
684- transferredRequest .put (key , value );//array里不允许关联,只能在object中关联
685- }
686- }
687- if (parent .isEmpty () == false ) {//可能数据库返回数据不够count
688- transferredRequest .put ("" + i , parent );
689- }
690- }
691- } else {
663+ JSONObject parent = null ;
664+ Object value ;
665+ JSONObject result ;
666+ if (parseRelation == false ) {
667+ //生成count个
668+ for (int i = 0 ; i < count ; i ++) {
669+ parent = new JSONObject (true );
692670 for (String key : set ) {
693671 value = request .get (key );
694- if (value instanceof JSONObject ) {//JSONObject,往下一级提取
695- config .setPosition (Integer . valueOf ( 0 + StringUtil . getNumber ( key , true )) );
672+ if (value instanceof JSONObject ) {//JSONObject
673+ config .setPosition (i );
696674 if (isArrayKey (key )) {//json array
697- result = getArray (path , config , key , (JSONObject ) value );
675+ result = getArray (getPath ( path , "" + i ) , config , key , (JSONObject ) value );
698676 } else {//json object
699- result = getObject (path , config , key , (JSONObject ) value );
677+ result = getObject (getPath ( path , "" + i ) , config , key , (JSONObject ) value );
700678 }
679+ System .out .println (TAG + "getArray parseRelation == false"
680+ + " >> i = " + i + "result = " + result );
701681 if (result != null && result .isEmpty () == false ) {//只添加!=null的值,可能数据库返回数据不够count
702- transferredRequest .put (key , result );
682+ parent .put (key , result );
683+
684+ updateRelation (path , getPath (path , "" + i ));//request结构已改变,需要更新依赖关系
703685 }
704- } else {//JSONArray或其它Object
705- //array里不允许关联,只能在object中关联
686+ } else {//JSONArray或其它Object,直接填充
687+ transferredRequest . put ( key , value ); //array里不允许关联,只能在object中关联
706688 }
707689 }
690+ if (parent .isEmpty () == false ) {//可能数据库返回数据不够count
691+ transferredRequest .put ("" + i , parent );
692+ }
693+ }
694+ } else {
695+ for (String key : set ) {
696+ value = request .get (key );
697+ if (value instanceof JSONObject ) {//JSONObject,往下一级提取
698+ config .setPosition (Integer .valueOf (0 + StringUtil .getNumber (key , true )));
699+ if (isArrayKey (key )) {//json array
700+ result = getArray (path , config , key , (JSONObject ) value );
701+ } else {//json object
702+ result = getObject (path , config , key , (JSONObject ) value );
703+ }
704+ if (result != null && result .isEmpty () == false ) {//只添加!=null的值,可能数据库返回数据不够count
705+ transferredRequest .put (key , result );
706+ }
707+ } else {//JSONArray或其它Object
708+ //array里不允许关联,只能在object中关联
709+ }
708710 }
709711 }
710712
@@ -795,9 +797,6 @@ private synchronized void putValueByPath(String path, Object value) {
795797 if (keys == null ) {
796798 return ;
797799 }
798- if (requestObject == null ) {
799- requestObject = new JSONObject (true );
800- }
801800 JSONObject parent = requestObject ;
802801 JSONObject child = null ;
803802 String key ;
@@ -820,42 +819,48 @@ private synchronized void putValueByPath(String path, Object value) {
820819 }
821820 System .out .println (TAG + "putValueByPath requestObject" + JSON .toJSONString (requestObject ) + "\n >>>>>>>>>>>>>>" );
822821 }
822+
823823 /**根据路径获取值
824824 * @param path
825825 * @return
826826 */
827827 private Object getValueByPath (String path ) {
828+ return getValueByPath (path , false );
829+ }
830+ /**根据路径获取值
831+ * @param path
832+ * @param containKey
833+ * @return containKey && parent.containsKey(targetKey) == false ? path : parent.get(targetKey);
834+ */
835+ private Object getValueByPath (String path , boolean containKey ) {
828836 System .out .println (TAG + "<<<<<<<<<<<<<<< \n getValueByPath path = " + path + "\n <<<<<<<<<<<<<<<<<<" );
829837 String [] keys = StringUtil .splitPath (path );
838+ if (keys == null || keys .length <= 0 ) {
839+ return null ;
840+ }
830841
831- if (keys != null ) {
832- JSONObject parent = requestObject ;
833- JSONObject child = null ;
834- String key ;
835- for (int i = 0 ; i < keys .length - 1 ; i ++) {//一步一步到达指定位置parentPath
836- key = keys [i ];
837- child = getJSONObject (parent , key );
838- if (child == null ) {//不存在
839- return path ;
840- }
841- parent = child ;
842- }
843- try {
844- System .out .println ("getValueByPath return parent.get(keys[keys.length - 1]); >> " );
845- return parent .get (keys [keys .length - 1 ]);
846- } catch (Exception e ) {
847- System .out .println ("getValueByPath try { return parent.get(keys[keys.length - 1]); " +
848- "} catch (Exception e) {\n " + e .getMessage () + "\n >> return '';" );
849- return path ;
842+ JSONObject parent = requestObject ;
843+ JSONObject child = null ;
844+ String key ;
845+ for (int i = 0 ; i < keys .length - 1 ; i ++) {//一步一步到达指定位置parentPath
846+ key = keys [i ];
847+ child = getJSONObject (parent , key );
848+ if (child == null ) {//不存在
849+ return containKey ? path : null ;
850850 }
851+ parent = child ;
851852 }
852853
853- System .out .println (TAG + "getValueByPath return requestObject"
854- + JSON . toJSONString ( requestObject ) + " \n >>>>>>>>>>>>>>>>>>" ) ;
855- return requestObject ;
854+ System .out .println ("getValueByPath return parent.get(keys[keys.length - 1]); >> " );
855+ String targetKey = keys [ keys . length - 1 ] ;
856+ return containKey && parent . containsKey ( targetKey ) == false ? path : parent . get ( targetKey ) ;
856857 }
857858
858859
860+
861+
862+
863+
859864 public static JSONObject getJSONObject (JSONObject object , String key ) {
860865 try {
861866 return object .getJSONObject (key );
0 commit comments