Skip to content

Commit 2ceb6f9

Browse files
committed
migrate json-simple to json
1 parent 109ec8b commit 2ceb6f9

File tree

8 files changed

+78
-81
lines changed

8 files changed

+78
-81
lines changed

build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Copyright 2019-2023 VMware, Inc.
1+
//Copyright 2019-2025 VMware, Inc.
22
//SPDX-License-Identifier: EPL-2.0
33
apply plugin: 'java'
44
apply plugin: "jacoco"
@@ -54,9 +54,7 @@ repositories {
5454

5555
dependencies {
5656
implementation("commons-collections:commons-collections:3.2.2")
57-
implementation("com.googlecode.json-simple:json-simple:1.1.1"){
58-
exclude group: 'junit'
59-
}
57+
implementation("org.json:json:20250107")
6058

6159
implementation("com.fasterxml.jackson.core:jackson-databind:2.16.0")
6260
implementation("commons-io:commons-io:2.13.0")

src/main/java/com/vmware/i18n/utils/CLDRUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils;
@@ -31,8 +31,8 @@
3131
import com.vmware.i18n.common.OfficialStatusEnum;
3232
import com.vmware.i18n.utils.timezone.CldrTimeZoneUtils;
3333

34-
import org.json.simple.JSONArray;
35-
import org.json.simple.JSONObject;
34+
import org.json.JSONArray;
35+
import org.json.JSONObject;
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838

@@ -1007,8 +1007,13 @@ public static void regionDataExtract() {
10071007
String locale = null;
10081008
if (!CommonUtil.isEmpty(itemMap.get(Constants.LANGUAGE_POPULATION))) {
10091009
Map<String, String> maxPopulationLanguage = new HashMap<>();
1010-
Map<String, Object> map = JSONUtil
1011-
.string2SortMap(itemMap.get(Constants.LANGUAGE_POPULATION).toString());
1010+
Map<String, Object> map = null;
1011+
try {
1012+
map = JSONUtil
1013+
.string2SortMap(new ObjectMapper().writeValueAsString(itemMap.get(Constants.LANGUAGE_POPULATION)));
1014+
} catch (Exception e) {
1015+
e.printStackTrace();
1016+
}
10121017
for (Map.Entry<String, Object> item : map.entrySet()) {
10131018
Map<String, String> data = (Map<String, String>) item.getValue();
10141019
boolean isMaxLanguage = getMaxLanguage(maxPopulationLanguage, data);

src/main/java/com/vmware/i18n/utils/JSONUtil.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils;
@@ -9,13 +9,12 @@
99
import java.util.LinkedList;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.HashMap;
1213
import java.util.TreeMap;
1314

14-
import org.json.simple.JSONObject;
15-
import org.json.simple.JSONValue;
16-
import org.json.simple.parser.ContainerFactory;
17-
import org.json.simple.parser.JSONParser;
18-
import org.json.simple.parser.ParseException;
15+
import org.json.JSONObject;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import org.json.JSONException;
1918

2019
public class JSONUtil {
2120

@@ -25,29 +24,16 @@ public class JSONUtil {
2524
* @return Map<String, Object> obj
2625
*/
2726
@SuppressWarnings("unchecked")
28-
public static Map<String, Object> getMapFromJson(String json) {
29-
JSONParser parser = new JSONParser();
30-
ContainerFactory containerFactory = getContainerFactory();
31-
Map<String, Object> result = null;
32-
try {
33-
result = (Map<String, Object>) parser.parse(json, containerFactory);
34-
} catch (ParseException e) {
35-
e.printStackTrace();
36-
}
37-
return result;
38-
}
39-
40-
private static ContainerFactory getContainerFactory() {
41-
ContainerFactory containerFactory = new ContainerFactory() {
42-
public List<Object> creatArrayContainer() {
43-
return new LinkedList<Object>();
44-
}
45-
46-
public Map<String, Object> createObjectContainer() {
47-
return new LinkedHashMap<String, Object>();
48-
}
49-
};
50-
return containerFactory;
27+
public static Map<String, Object> getMapFromJson(String json) throws JSONException {
28+
Map<String, Object> mapping = null;
29+
try {
30+
mapping = new ObjectMapper().readValue(json, HashMap.class);
31+
} catch (Exception e) {
32+
System.out.println("json=" + json);
33+
e.printStackTrace();
34+
throw new JSONException(e.getMessage(), e);
35+
}
36+
return mapping;
5137
}
5238

5339
/**
@@ -65,7 +51,12 @@ public static Object select(JSONObject jsonObj, String keyPath) {
6551
Object retvalue = null;
6652
for (int i = 0; i < patharr.length; i++) {
6753
String key = patharr[i];
68-
retvalue = current.get(key);
54+
try {
55+
retvalue = current.get(key);
56+
} catch (JSONException e) {
57+
retvalue = null;
58+
break;
59+
}
6960
if (i < (patharr.length - 1)) {
7061
current = (JSONObject) retvalue;
7162
}
@@ -81,8 +72,8 @@ public static Object select(JSONObject jsonObj, String keyPath) {
8172
public static JSONObject string2JSON(String jsonStr) {
8273
JSONObject genreJsonObject = null;
8374
try {
84-
genreJsonObject = (JSONObject) JSONValue.parseWithException(jsonStr);
85-
} catch (ParseException e) {
75+
genreJsonObject = new JSONObject(jsonStr);
76+
} catch (JSONException e) {
8677
e.printStackTrace();
8778
}
8879
return genreJsonObject;
@@ -100,9 +91,9 @@ public int compare(String o1, String o2) {
10091
}
10192
});
10293
try {
103-
Map<String, Object> genreJsonObject = (Map<String, Object>) JSONValue.parseWithException(jsonStr);
94+
Map<String, Object> genreJsonObject = (Map<String, Object>) getMapFromJson(jsonStr);
10495
sortMap.putAll(genreJsonObject);
105-
} catch (ParseException e) {
96+
} catch (JSONException e) {
10697
e.printStackTrace();
10798
}
10899
return sortMap;

src/main/java/com/vmware/i18n/utils/LocaleDataUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
2-
* Copyright 2019-2022 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils;
66

77
import java.io.File;
88
import java.util.*;
99

10-
import org.json.simple.JSONArray;
11-
import org.json.simple.JSONObject;
10+
import org.json.JSONArray;
11+
import org.json.JSONObject;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

src/main/java/com/vmware/i18n/utils/MiscUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils;
@@ -9,7 +9,7 @@
99
import java.util.LinkedHashMap;
1010
import java.util.Map;
1111

12-
import org.json.simple.JSONObject;
12+
import org.json.JSONObject;
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
1515

src/main/java/com/vmware/i18n/utils/SupplementUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
2-
* Copyright 2019-2023 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils;
66

77
import java.io.File;
88
import java.text.MessageFormat;
99
import java.util.LinkedHashMap;
10-
import java.util.LinkedList;
10+
import java.util.List;
1111
import java.util.Map;
1212

1313
import com.vmware.i18n.common.Constants;
14-
import org.json.simple.JSONObject;
14+
import org.json.JSONObject;
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

@@ -46,7 +46,7 @@ public static void supplementalCurrencyExtract() {
4646
Map<String, Object> regionMap = (Map<String, Object>) supplementalMap.get(Constants.REGION);
4747
Map<String, String> resMap = new LinkedHashMap<String, String>();
4848
for (Map.Entry<String, Object> entry : regionMap.entrySet()) {
49-
LinkedList<Object> list = (LinkedList<Object>) entry.getValue();
49+
List<Object> list = (List<Object>) entry.getValue();
5050
for (int i = 0; i < list.size(); i++) {
5151
Map<String, Object> itemMap = (Map<String, Object>) list.get(i);
5252
for (Map.Entry<String, Object> item : itemMap.entrySet()) {

src/main/java/com/vmware/i18n/utils/timezone/CldrTimeZoneUtils.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.i18n.utils.timezone;
@@ -17,8 +17,8 @@
1717
import java.util.TimeZone;
1818
import java.util.TreeMap;
1919

20-
import org.json.simple.JSONArray;
21-
import org.json.simple.JSONObject;
20+
import org.json.JSONArray;
21+
import org.json.JSONObject;
2222

2323
import com.fasterxml.jackson.annotation.JsonInclude;
2424
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -33,10 +33,11 @@ public static Map<String, JSONArray> findTimezoneKeys(JSONObject metaZonesJson)
3333
JSONObject timzone = (JSONObject) select(metaZonesJson, "supplemental.metaZones.metazoneInfo.timezone");
3434
Map<String, JSONArray> result = new TreeMap<String, JSONArray>();
3535
if (timzone != null) {
36-
for (Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) timzone.entrySet()) {
37-
String zoneKeystr1 = entry.getKey();
38-
Object obj1 = entry.getValue();
39-
if (obj1 instanceof List) {
36+
for (String key : (Set<String>) timzone.keySet()) {
37+
// for (Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) timzone.entrySet()) {
38+
String zoneKeystr1 = key;
39+
Object obj1 = timzone.get(key);
40+
if (obj1 instanceof JSONArray) {
4041
JSONArray objArry = (JSONArray) obj1;
4142
JSONObject usesMetazones = (JSONObject) objArry.get(0);
4243
JSONObject usesMetazoneObj = (JSONObject) usesMetazones.get(Constants.TIMEZONENAME_USES_METAZONE);
@@ -46,10 +47,11 @@ public static Map<String, JSONArray> findTimezoneKeys(JSONObject metaZonesJson)
4647
}
4748
}
4849
JSONObject jsonObj1 = (JSONObject) obj1;
49-
for (Entry<String, Object> entry1 : (Set<Map.Entry<String, Object>>) jsonObj1.entrySet()) {
50-
String zoneKeystr2 = entry1.getKey();
51-
Object obj2 = entry1.getValue();
52-
if (obj2 instanceof List) {
50+
for (String key1 : (Set<String>) jsonObj1.keySet()) {
51+
// for (Entry<String, Object> entry1 : (Set<Map.Entry<String, Object>>) jsonObj1.entrySet()) {
52+
String zoneKeystr2 = key1;
53+
Object obj2 = jsonObj1.get(key1);
54+
if (obj2 instanceof JSONArray) {
5355
JSONArray objArry2 = (JSONArray) obj2;
5456
JSONObject usesMetazones2 = (JSONObject) objArry2.get(0);
5557
JSONObject usesMetazoneObj2 = (JSONObject) usesMetazones2
@@ -61,10 +63,11 @@ public static Map<String, JSONArray> findTimezoneKeys(JSONObject metaZonesJson)
6163
}
6264
}
6365
JSONObject jsonObj2 = (JSONObject) obj2;
64-
for (Entry<String, Object> entry2 : (Set<Map.Entry<String, Object>>) jsonObj2.entrySet()) {
65-
String zoneKeystr3 = entry2.getKey();
66-
Object obj3 = entry2.getValue();
67-
if (obj3 instanceof List) {
66+
for (String key2 : (Set<String>) jsonObj2.keySet()) {
67+
// for (Entry<String, Object> entry2 : (Set<Map.Entry<String, Object>>) jsonObj2.entrySet()) {
68+
String zoneKeystr3 = key2;
69+
Object obj3 = jsonObj2.get(key2);
70+
if (obj3 instanceof JSONArray) {
6871
JSONArray objArry3 = (JSONArray) obj3;
6972
JSONObject usesMetazones3 = (JSONObject) objArry3.get(0);
7073
JSONObject usesMetazoneObj3 = (JSONObject) usesMetazones3
@@ -76,10 +79,11 @@ public static Map<String, JSONArray> findTimezoneKeys(JSONObject metaZonesJson)
7679
}
7780
}
7881
JSONObject jsonObj3 = (JSONObject) obj3;
79-
for (Entry<String, Object> entry3 : (Set<Map.Entry<String, Object>>) jsonObj3.entrySet()) {
80-
String zoneKeystr4 = entry3.getKey();
81-
Object obj4 = entry3.getValue();
82-
if (obj4 instanceof List) {
82+
for (String key3 : (Set<String>) jsonObj3.keySet()) {
83+
// for (Entry<String, Object> entry3 : (Set<Map.Entry<String, Object>>) jsonObj3.entrySet()) {
84+
String zoneKeystr4 = key3;
85+
Object obj4 = jsonObj3.get(key3);
86+
if (obj4 instanceof JSONArray) {
8387
JSONArray objArry4 = (JSONArray) obj4;
8488
JSONObject usesMetazones4 = (JSONObject) objArry4.get(0);
8589
JSONObject usesMetazoneObj4 = (JSONObject) usesMetazones4
@@ -116,9 +120,9 @@ public static String createTimeZoneNameJson(JSONObject metaZonesJson, JSONObject
116120
Map<String, JSONArray> timezoneKeysProps = findTimezoneKeys(metaZonesJson);
117121
Map<String, List<JSONObject>> mapZonesMap = new TreeMap<String, List<JSONObject>>();
118122
if (arry != null) {
119-
Iterator<JSONObject> iterator = arry.iterator();
123+
Iterator<Object> iterator = arry.iterator();
120124
while (iterator.hasNext()) {
121-
JSONObject objZone = iterator.next();
125+
JSONObject objZone = (JSONObject) iterator.next();
122126
String timezoneKey = (String) select(objZone, "mapZone._type");
123127
if (mapZonesMap.get(timezoneKey) != null) {
124128
mapZonesMap.get(timezoneKey).add(objZone);
@@ -139,10 +143,10 @@ public static String createTimeZoneNameJson(JSONObject metaZonesJson, JSONObject
139143
JSONArray mataZoneP = entry.getValue();
140144
cldrMetaZone.put(Constants.TIMEZONENAME_METAZONE_EXEMPLARCITY, exemplarCity);
141145
cldrMetaZone.put(Constants.TIMEZONENAME_METAZONE_TIMEZONE, timeZone);
142-
Iterator<JSONObject> metaiterator = mataZoneP.iterator();
146+
Iterator<Object> metaiterator = mataZoneP.iterator();
143147
List<Map<String, Object>> usesMetazones = new ArrayList<Map<String, Object>>();
144148
while (metaiterator.hasNext()) {
145-
JSONObject objZone = metaiterator.next();
149+
JSONObject objZone = (JSONObject) metaiterator.next();
146150
Map<String, Object> usesMetazoneMap = new TreeMap<String, Object>();
147151
String metazoneKey = (String) select(objZone, "usesMetazone._mzone");
148152
String _fromVal = (String) select(objZone, "usesMetazone._from");

src/test/java/com/vmware/test/i18n/I18nUtilTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 VMware, Inc.
2+
* Copyright 2019-2025 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.test.i18n;
@@ -10,8 +10,7 @@
1010
import java.util.LinkedHashMap;
1111
import java.util.Map;
1212

13-
import org.json.simple.JSONValue;
14-
import org.json.simple.parser.ParseException;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
1514
import org.junit.Assert;
1615
import org.junit.Before;
1716
import org.junit.Test;
@@ -60,8 +59,8 @@ public void testLocalesExtract() {
6059
for(int i=0;i<languageArray.length;i++) {
6160
String regions = PatternUtil.getRegionFromLib(languageArray[i]);
6261
Map<String, Object> genreJsonObject = null;
63-
genreJsonObject = (Map<String, Object>) JSONValue.parseWithException(regions);
64-
Map<String, Object> territoriesObject = (Map<String, Object>) JSONValue.parseWithException(genreJsonObject.get("territories").toString());
62+
genreJsonObject = (Map<String, Object>) new ObjectMapper().readValue(regions, HashMap.class);
63+
Map<String, Object> territoriesObject = (Map<String, Object>) new ObjectMapper().readValue(genreJsonObject.get("territories").toString(), HashMap.class);
6564
if (languageArray[i].equals("zh")) {
6665
Assert.assertEquals("zh", genreJsonObject.get("language"));
6766
Assert.assertNotNull(territoriesObject.get("TW"));
@@ -101,7 +100,7 @@ public void testLocalesExtract() {
101100
}
102101

103102
}
104-
} catch (ParseException e) {
103+
} catch (Exception e) {
105104
e.printStackTrace();
106105
}
107106
}

0 commit comments

Comments
 (0)