Commit 77f33c865478e0f2c19506f271e54dccd3380085

Authored by wangbin
1 parent c65275db
Exists in master

optimized jsonutil

@@ -59,7 +59,7 @@ uploadArchives { @@ -59,7 +59,7 @@ uploadArchives {
59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 } 60 }
61 pom.project { 61 pom.project {
62 - version '1.1.41' 62 + version '1.1.43'
63 artifactId ARTIFACT_Id 63 artifactId ARTIFACT_Id
64 groupId GROUP_ID 64 groupId GROUP_ID
65 packaging TYPE 65 packaging TYPE
src/main/java/com/taover/util/UtilJSON.java
@@ -6,6 +6,19 @@ import net.sf.json.JSONArray; @@ -6,6 +6,19 @@ import net.sf.json.JSONArray;
6 import net.sf.json.JSONObject; 6 import net.sf.json.JSONObject;
7 7
8 public class UtilJSON { 8 public class UtilJSON {
  9 + public static boolean isJsonNull(Object data){
  10 + if(data == null){
  11 + return true;
  12 + }
  13 + if(!(data instanceof String) && data.toString().equals("null")){
  14 + return true;
  15 + }
  16 + if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){
  17 + return true;
  18 + }
  19 + return false;
  20 + }
  21 +
9 public static void removeJsonNull(JSONObject data){ 22 public static void removeJsonNull(JSONObject data){
10 if(data == null || data.isNullObject()){ 23 if(data == null || data.isNullObject()){
11 return; 24 return;
@@ -16,44 +29,35 @@ public class UtilJSON { @@ -16,44 +29,35 @@ public class UtilJSON {
16 Object value = data.get(keyItem); 29 Object value = data.get(keyItem);
17 30
18 if(isJsonNull(value)) { 31 if(isJsonNull(value)) {
19 - data.put(keyItem, ""); 32 + data.remove(keyItem);
  33 + continue;
20 } 34 }
21 35
22 if(value instanceof JSONObject){ 36 if(value instanceof JSONObject){
23 removeJsonNull((JSONObject)value); 37 removeJsonNull((JSONObject)value);
24 - continue;  
25 - }  
26 -  
27 - if(value instanceof JSONArray){ 38 + }else if(value instanceof JSONArray){
28 removeJsonNull((JSONArray)value); 39 removeJsonNull((JSONArray)value);
29 } 40 }
30 } 41 }
31 } 42 }
32 -  
33 - public static boolean isJsonNull(Object data){  
34 - if(data == null){  
35 - return true;  
36 - }  
37 - if(!(data instanceof String) && data.toString().equals("null")){  
38 - return true;  
39 - }  
40 - if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){  
41 - return true;  
42 - }  
43 - return false;  
44 - }  
45 - 43 +
46 public static void removeJsonNull(JSONArray dataArr){ 44 public static void removeJsonNull(JSONArray dataArr){
47 if(dataArr == null){ 45 if(dataArr == null){
48 return; 46 return;
49 } 47 }
50 for(int i=0; i<dataArr.size(); ++i){ 48 for(int i=0; i<dataArr.size(); ++i){
51 - JSONObject dataItem = dataArr.optJSONObject(i); 49 + Object dataItem = dataArr.get(i);
52 if(isJsonNull(dataItem)){ 50 if(isJsonNull(dataItem)){
53 dataArr.remove(i); 51 dataArr.remove(i);
  52 + --i;
54 continue; 53 continue;
55 } 54 }
56 - removeJsonNull(dataItem); 55 +
  56 + if(dataItem instanceof JSONObject){
  57 + removeJsonNull((JSONObject)dataItem);
  58 + }else if(dataItem instanceof JSONArray){
  59 + removeJsonNull((JSONArray)dataItem);
  60 + }
57 } 61 }
58 } 62 }
59 63
@@ -68,14 +72,12 @@ public class UtilJSON { @@ -68,14 +72,12 @@ public class UtilJSON {
68 72
69 if(isJsonNull(value)) { 73 if(isJsonNull(value)) {
70 data.put(keyItem, target); 74 data.put(keyItem, target);
  75 + continue;
71 } 76 }
72 77
73 if(value instanceof JSONObject){ 78 if(value instanceof JSONObject){
74 replaceJsonNull((JSONObject)value, target); 79 replaceJsonNull((JSONObject)value, target);
75 - continue;  
76 - }  
77 -  
78 - if(value instanceof JSONArray){ 80 + }else if(value instanceof JSONArray){
79 replaceJsonNull((JSONArray)value, target); 81 replaceJsonNull((JSONArray)value, target);
80 } 82 }
81 } 83 }
@@ -86,12 +88,18 @@ public class UtilJSON { @@ -86,12 +88,18 @@ public class UtilJSON {
86 return; 88 return;
87 } 89 }
88 for(int i=0; i<dataArr.size(); ++i){ 90 for(int i=0; i<dataArr.size(); ++i){
89 - JSONObject dataItem = dataArr.optJSONObject(i); 91 + Object dataItem = dataArr.get(i);
90 if(isJsonNull(dataItem)){ 92 if(isJsonNull(dataItem)){
91 - dataArr.set(i, target); 93 + dataArr.remove(i);
  94 + --i;
92 continue; 95 continue;
93 } 96 }
94 - removeJsonNull(dataItem); 97 +
  98 + if(dataItem instanceof JSONObject){
  99 + replaceJsonNull((JSONObject)dataItem, target);
  100 + }else if(dataItem instanceof JSONArray){
  101 + replaceJsonNull((JSONArray)dataItem, target);
  102 + }
95 } 103 }
96 } 104 }
97 105