Skip to content

Commit da8b295

Browse files
committed
Fix an issue with parsing null values. Null objects are now stored as empty objects, rather than as true null values
1 parent 786160f commit da8b295

File tree

7 files changed

+119
-4
lines changed

7 files changed

+119
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.stevenlagoy</groupId>
66
<artifactId>json-java-objectifier</artifactId>
7-
<version>1.0.3</version>
7+
<version>1.0.4</version>
88
<packaging>jar</packaging>
99
<name>JSON Java Objectifier</name>
1010
<description>Java tools for converting JSON into typed object structures</description>

src/main/java/core/JSONObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static boolean isValidJsonType(Object value) {
6262
value instanceof JSONObject ||
6363
value instanceof List<?> ||
6464
value instanceof Boolean ||
65-
value == null
65+
value instanceof Object
6666
);
6767
}
6868

src/main/java/core/JSONStringifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static String stringifyValue(Object value, Class<?> type) {
5656
public static String stringifyValue(Object value) {
5757
if (value instanceof String) return "\"" + stringifyEscape((String) value) + "\"";
5858
if (value instanceof Number || value instanceof Boolean) return value.toString();
59-
if (value == null) return "null";
59+
if (value instanceof Object) return "null";
6060
else throw new IllegalArgumentException("Unsupported JSON value: " + value);
6161
}
6262

src/tests/java/StringifierTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void stringifyValue() {
2323
assertEquals("\"hello\"", JSONStringifier.stringifyValue("hello"));
2424
assertEquals("42", JSONStringifier.stringifyValue(42));
2525
assertEquals("true", JSONStringifier.stringifyValue(true));
26-
assertEquals("null", JSONStringifier.stringifyValue(null));
26+
assertEquals("null", JSONStringifier.stringifyValue(new Object()));
2727

2828
// Test with type information
2929
List<Integer> numbers = new ArrayList<>();

src/tests/java/Tests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010

1111
public class Tests {
1212

13+
@Test
14+
public void testFileWithNulls() {
15+
Path expectedPath = Path.of("src", "tests", "java", "data", "test7", "expected7.out");
16+
Path testPath = Path.of("src", "tests", "java", "data", "test7", "test7.json");
17+
18+
String expected = String.join("\n", FileOperations.readFile(expectedPath));
19+
String actual = JSONProcessor.processJson(testPath).toString();
20+
21+
assertEquals(expected, actual);
22+
}
23+
1324
@Test
1425
public void testVeryLargeFile() {
1526

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"test7" : {
2+
"FIPS" : "02016",
3+
"name" : "Aleutians West Census Area",
4+
"state" : "Alaska",
5+
"county_seat" : null,
6+
"population" : 5619,
7+
"population_desity" : 1.5,
8+
"pop_change_rate" : 5.8,
9+
"square_mileage" : 3682.18,
10+
"white_population" : 2004,
11+
"black_population" : 332,
12+
"native_indian_population" : 857,
13+
"asian_population" : 1606,
14+
"pacific_islander_population" : 103,
15+
"hispanic_population" : 726,
16+
"other_race_ethnicity_population" : 348,
17+
"multiple_race_population" : 311,
18+
"pop_male" : 3723,
19+
"pop_female" : 1838,
20+
"pop_0to4" : 205,
21+
"pop_5to9" : 227,
22+
"pop_10to14" : 226,
23+
"pop_15to19" : 249,
24+
"pop_20to24" : 334,
25+
"pop_25to34" : 972,
26+
"pop_35to44" : 1137,
27+
"pop_45to54" : 1232,
28+
"pop_55to64" : 786,
29+
"pop_65to74" : 157,
30+
"pop_75to84" : 31,
31+
"pop_85to120" : 5,
32+
"median_age" : 40.7,
33+
"median_age_male" : 41.0,
34+
"median_age_female" : 39.9,
35+
"number_households" : 1212,
36+
"average_household_size" : 2.49,
37+
"number_households_singleM_wo_child" : 288,
38+
"number_households_singleF_wo_child" : 105,
39+
"number_households_married_w_child" : 288,
40+
"number_households_married_wo_child" : 220,
41+
"number_households_singleM_w_child" : 59,
42+
"number_households_singleF_w_child" : 86,
43+
"number_families" : 711,
44+
"average_family_size" : 3.18,
45+
"housing_units" : 1212,
46+
"housing_vacant" : 717,
47+
"owner_occupied_housing" : 391,
48+
"renter_occupied_housing" : 821,
49+
"number_farms" : -99,
50+
"average_farm_acreage" : -99,
51+
"total_crop_acreage" : -99
52+
}

src/tests/java/data/test7/test7.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"FIPS" : "02016",
3+
"name" : "Aleutians West Census Area",
4+
"state" : "Alaska",
5+
"county_seat" : null,
6+
"population" : 5619,
7+
"population_desity" : 1.5,
8+
"pop_change_rate" : 5.8,
9+
"square_mileage" : 3682.18,
10+
"white_population" : 2004,
11+
"black_population" : 332,
12+
"native_indian_population" : 857,
13+
"asian_population" : 1606,
14+
"pacific_islander_population" : 103,
15+
"hispanic_population" : 726,
16+
"other_race_ethnicity_population" : 348,
17+
"multiple_race_population" : 311,
18+
"pop_male" : 3723,
19+
"pop_female" : 1838,
20+
"pop_0to4" : 205,
21+
"pop_5to9" : 227,
22+
"pop_10to14" : 226,
23+
"pop_15to19" : 249,
24+
"pop_20to24" : 334,
25+
"pop_25to34" : 972,
26+
"pop_35to44" : 1137,
27+
"pop_45to54" : 1232,
28+
"pop_55to64" : 786,
29+
"pop_65to74" : 157,
30+
"pop_75to84" : 31,
31+
"pop_85to120" : 5,
32+
"median_age" : 40.7,
33+
"median_age_male" : 41.0,
34+
"median_age_female" : 39.9,
35+
"number_households" : 1212,
36+
"average_household_size" : 2.49,
37+
"number_households_singleM_wo_child" : 288,
38+
"number_households_singleF_wo_child" : 105,
39+
"number_households_married_w_child" : 288,
40+
"number_households_married_wo_child" : 220,
41+
"number_households_singleM_w_child" : 59,
42+
"number_households_singleF_w_child" : 86,
43+
"number_families" : 711,
44+
"average_family_size" : 3.18,
45+
"housing_units" : 1212,
46+
"housing_vacant" : 717,
47+
"owner_occupied_housing" : 391,
48+
"renter_occupied_housing" : 821,
49+
"number_farms" : -99,
50+
"average_farm_acreage" : -99,
51+
"total_crop_acreage" : -99
52+
}

0 commit comments

Comments
 (0)