17 |
18 | true
19 | true
20 | false
21 | false
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/array-minify-to-prettify.writer.json5:
--------------------------------------------------------------------------------
1 | // Multi-line comment on root array element
2 | [
3 | // Comment on truly primitive boolean
4 | true,
5 | // Comment on falsy primitive boolean
6 | false,
7 | // Double quoted string with escaped quote
8 | "Test \" 123",
9 | // Single quoted string with escaped quote
10 | "Test ' 123",
11 | // String with mixed quotes
12 | "Test ' 123",
13 | // Escapes
14 | "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
15 | // Multi-line comment on primitive value
16 | true,
17 | // Neutral hex number
18 | 0x303030,
19 | // Positive hex number
20 | 0x303030,
21 | // Negative hex number
22 | -0x303030,
23 | NaN,
24 | NaN,
25 | NaN,
26 | Infinity,
27 | Infinity,
28 | -Infinity,
29 | 123,
30 | 123,
31 | -123,
32 | // Object within array
33 | {
34 | // Sample member
35 | key: "value",
36 | // Nested array
37 | array: [
38 | // any true
39 | true,
40 | // any false
41 | false,
42 | ],
43 | },
44 | ]
45 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/array.writer.json5:
--------------------------------------------------------------------------------
1 | /*
2 | * Multi-line comment
3 | * on root array element
4 | */
5 | [
6 | // Comment on truly primitive boolean
7 | true,
8 | // Comment on falsy primitive boolean
9 | false,
10 | // Double quoted string with escaped quote
11 | "Test \" 123",
12 | // Single quoted string with escaped quote
13 | "Test ' 123",
14 | // String with mixed quotes
15 | "Test ' 123",
16 | // Escapes
17 | "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
18 | /*
19 | * Multi-line comment
20 | * on primitive value
21 | */
22 | true,
23 | // Neutral hex number
24 | 0x303030,
25 | // Positive hex number
26 | 0x303030,
27 | // Negative hex number
28 | -0x303030,
29 | NaN,
30 | NaN,
31 | NaN,
32 | Infinity,
33 | Infinity,
34 | -Infinity,
35 | 123,
36 | 123,
37 | -123,
38 | // Object within array
39 | {
40 | // Sample member
41 | key: "value",
42 | // Nested array
43 | array: [
44 | // any true
45 | true,
46 | // any false
47 | false,
48 | ],
49 | },
50 | ]
51 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/array.parser.json5:
--------------------------------------------------------------------------------
1 | /*
2 | * Multi-line comment
3 | * on root array element
4 | */
5 | [
6 | // Comment on truly primitive boolean
7 | true,
8 | // Comment on falsy primitive boolean
9 | false,
10 | // Double quoted string with escaped quote
11 | "Test \" 123",
12 | // Single quoted string with escaped quote
13 | 'Test \' 123',
14 | // String with mixed quotes
15 | "Test ' 123",
16 | // Escapes
17 | "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
18 | /*
19 | * Multi-line comment
20 | * on primitive value
21 | */
22 | true,
23 | // Neutral hex number
24 | 0x303030,
25 | // Positive hex number
26 | +0x303030,
27 | // Negative hex number
28 | -0x303030,
29 | NaN,
30 | +NaN,
31 | -NaN,
32 | Infinity,
33 | +Infinity,
34 | -Infinity,
35 | 123,
36 | +123,
37 | -123,
38 | // Object within array
39 | {
40 | // Sample member
41 | key: "value",
42 | // Nested array
43 | array: [
44 | // any true
45 | true,
46 | // any false
47 | false
48 | ]
49 | }
50 | ]
51 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/object-minify-to-prettify.writer.json5:
--------------------------------------------------------------------------------
1 | // Object comment
2 | {
3 | // Quotes tests
4 | quotes: {
5 | // Test double-quoted member name and string value
6 | doubleQuoted: "Test \" 123",
7 | // Test single-quoted member name and string value
8 | singleQuoted: "Test ' 123",
9 | // Test mixed quotes with double- and single-quotes
10 | testMixedQuoted: "Test ' 123",
11 | },
12 | // Falsy boolean,
13 | falsy: false,
14 | // Truly boolean
15 | truly: true,
16 | // Test escape characters
17 | escapes: "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
18 | // Test member names
19 | memberNames: {
20 | // Test member names
21 | $LoremA_Ipsum123指事字: 0,
22 | },
23 | // Some multi-line comment
24 | multiLineComment: false,
25 | // Hex numbers
26 | hexNumbers: [
27 | // Neutral hex number
28 | 0x303030,
29 | // Positive hex number
30 | 0x303030,
31 | // Negative hex number
32 | -0x303030,
33 | ],
34 | // Special numbers
35 | specialNumbers: [
36 | NaN,
37 | NaN,
38 | NaN,
39 | Infinity,
40 | Infinity,
41 | -Infinity,
42 | ],
43 | // Literals
44 | literals: [
45 | 123,
46 | 123,
47 | -123,
48 | ],
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/object.writer.json5:
--------------------------------------------------------------------------------
1 | // Object comment
2 | {
3 | // Quotes tests
4 | quotes: {
5 | // Test double-quoted member name and string value
6 | doubleQuoted: "Test \" 123",
7 | // Test single-quoted member name and string value
8 | singleQuoted: "Test ' 123",
9 | // Test mixed quotes with double- and single-quotes
10 | testMixedQuoted: "Test ' 123",
11 | },
12 | // Falsy boolean,
13 | falsy: false,
14 | // Truly boolean
15 | truly: true,
16 | // Test escape characters
17 | escapes: "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
18 | // Test member names
19 | memberNames: {
20 | // Test member names
21 | $LoremA_Ipsum123指事字: 0,
22 | },
23 | /*
24 | * Some multi-line
25 | * comment
26 | */
27 | multiLineComment: false,
28 | // Hex numbers
29 | hexNumbers: [
30 | // Neutral hex number
31 | 0x303030,
32 | // Positive hex number
33 | 0x303030,
34 | // Negative hex number
35 | -0x303030,
36 | ],
37 | // Special numbers
38 | specialNumbers: [
39 | NaN,
40 | NaN,
41 | NaN,
42 | Infinity,
43 | Infinity,
44 | -Infinity,
45 | ],
46 | // Literals
47 | literals: [
48 | 123,
49 | 123,
50 | -123,
51 | ],
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/resources/e2e/roundtrips/object.parser.json5:
--------------------------------------------------------------------------------
1 | // Object comment
2 | {
3 | // Quotes tests
4 | quotes: {
5 | // Test double-quoted member name and string value
6 | "doubleQuoted": "Test \" 123",
7 | // Test single-quoted member name and string value
8 | 'singleQuoted': 'Test \' 123',
9 | // Test mixed quotes with double- and single-quotes
10 | testMixedQuoted: "Test ' 123"
11 | },
12 | // Falsy boolean,
13 | falsy: false,
14 | // Truly boolean
15 | truly: true,
16 | // Test escape characters
17 | "escapes": "\\n\\r\\f\\b\\t\\u000B\\0\\u12Fa\\u007F",
18 | // Test member names
19 | memberNames: {
20 | // Test member names
21 | "$Lorem\u0041_Ipsum123指事字": 0
22 | },
23 | /*
24 | * Some multi-line
25 | * comment
26 | */
27 | multiLineComment: false,
28 | // Hex numbers
29 | hexNumbers: [
30 | // Neutral hex number
31 | 0x303030,
32 | // Positive hex number
33 | +0x303030,
34 | // Negative hex number
35 | -0x303030
36 | ],
37 | // Special numbers
38 | specialNumbers: [
39 | NaN,
40 | +NaN,
41 | -NaN,
42 | Infinity,
43 | +Infinity,
44 | -Infinity
45 | ],
46 | // Literals
47 | literals: [
48 | 123,
49 | +123,
50 | -123
51 | ]
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/config/DigitSeparatorStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2022 - 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.config;
18 |
19 | /**
20 | * An enum containing all supported behaviors for handling digit separators.
21 | *
22 | * @author Marcel Haßlinger
23 | */
24 | public enum DigitSeparatorStrategy {
25 |
26 | /**
27 | * Expect no digit separators
28 | */
29 | NONE,
30 |
31 | /**
32 | * Uses Java-style digit separators (e.g. {@code 123_456}).
33 | */
34 | JAVA_STYLE,
35 |
36 | /**
37 | * Uses C-style digit separators (e.g. {@code 123'456}).
38 | */
39 | C_STYLE,
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/Json5Null.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008 Google Inc.
3 | * Copyright (C) 2025 Marcel Haßlinger
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.marhali.json5;
19 |
20 | /**
21 | * A class representing a Json {@code null} literal value.
22 | *
23 | * @author Inderjeet Singh
24 | * @author Joel Leitch
25 | * @author Marcel Haßlinger
26 | */
27 | public final class Json5Null extends Json5Element {
28 | public Json5Null() {
29 | }
30 |
31 | @Override
32 | public Json5Element deepCopy() {
33 | Json5Null copy = new Json5Null();
34 | copy.setComment(comment);
35 | return copy;
36 | }
37 |
38 | @Override
39 | public String getAsString() {
40 | return "null";
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/internal/RadixNumberTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import static org.junit.jupiter.api.Assertions.*;
22 |
23 | /**
24 | * @author Marcel Haßlinger
25 | */
26 | public class RadixNumberTest {
27 |
28 | @Test
29 | void test_getNumber() {
30 | assertEquals(187, new RadixNumber(187, 10).getNumber());
31 | }
32 |
33 | @Test
34 | void test_getRadix() {
35 | assertEquals(10, new RadixNumber(187, 10).getRadix());
36 | }
37 |
38 | @Test
39 | void test_equals() {
40 | assertEquals(new RadixNumber(187, 10), new RadixNumber(187, 10));
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/internal/NumberLimits.java:
--------------------------------------------------------------------------------
1 | package de.marhali.json5.internal;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.BigInteger;
5 |
6 | /**
7 | * This class enforces limits on numbers parsed from Json5 to avoid potential performance problems
8 | * when extremely large numbers are used.
9 | */
10 | public class NumberLimits {
11 | private NumberLimits() {
12 | }
13 |
14 | private static final int MAX_NUMBER_STRING_LENGTH = 10_000;
15 |
16 | private static void checkNumberStringLength(String s) {
17 | if (s.length() > MAX_NUMBER_STRING_LENGTH) {
18 | throw new NumberFormatException("Number string too large: " + s.substring(0, 30) + "...");
19 | }
20 | }
21 |
22 | public static BigDecimal parseBigDecimal(String s) throws NumberFormatException {
23 | checkNumberStringLength(s);
24 | BigDecimal decimal = new BigDecimal(s);
25 |
26 | // Cast to long to avoid issues with abs when value is Integer.MIN_VALUE
27 | if (Math.abs((long) decimal.scale()) >= 10_000) {
28 | throw new NumberFormatException("Number has unsupported scale: " + s);
29 | }
30 | return decimal;
31 | }
32 |
33 | public static BigInteger parseBigInteger(String s) throws NumberFormatException {
34 | checkNumberStringLength(s);
35 | return new BigInteger(s);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/fixtures/ToStringFixtures.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.fixtures;
18 |
19 | import de.marhali.json5.config.DigitSeparatorStrategy;
20 | import de.marhali.json5.config.DuplicateKeyStrategy;
21 | import de.marhali.json5.config.Json5Options;
22 |
23 | /**
24 | * @author Marcel Haßlinger
25 | */
26 | public class ToStringFixtures {
27 | /**
28 | * Options to use for testing {@link de.marhali.json5.Json5Element#toString(Json5Options)} methods.
29 | */
30 | public static Json5Options OPTIONS = Json5Options.builder()
31 | .allowNaN()
32 | .allowInfinity()
33 | .allowInvalidSurrogates()
34 | .parseComments()
35 | .writeComments()
36 | .trailingComma()
37 | .quoteSingle()
38 | .digitSeparatorStrategy(DigitSeparatorStrategy.NONE)
39 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
40 | .prettyPrinting()
41 | .build();
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowNaNTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.*;
27 |
28 | /**
29 | * @author Marcel Haßlinger
30 | */
31 | public class DisallowNaNTest {
32 | @Test
33 | @DisplayName("Parse: disallowed NaN throws exception")
34 | void disallowNaN() {
35 | var json5 = Json5.builder(Json5Options.Builder::build);
36 |
37 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-NaN.json5")));
38 |
39 | assertEquals("NaN is not allowed at index 22 [character 1 in line 3]", ex.getMessage());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowInfinityTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowInfinityTest {
33 | @Test
34 | @DisplayName("Parse: disallowed Infinity throws exception")
35 | void disallowInfinity() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-Infinity.json5")));
39 |
40 | assertEquals("Infinity is not allowed at index 27 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowOctalNumberTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowOctalNumberTest {
33 | @Test
34 | @DisplayName("Parse: disallowed octal number throws exception")
35 | void disallowOctalNumber() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-octal-number.json5")));
39 |
40 | assertEquals("Octal literals are not allowed at index 24 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowBinaryNumberTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowBinaryNumberTest {
33 | @Test
34 | @DisplayName("Parse: disallowed binary number throws exception")
35 | void disallowBinaryNumber() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-binary-number.json5")));
39 |
40 | assertEquals("Binary literals are not allowed at index 25 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowTrailingArrayTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowTrailingArrayTest {
33 | @Test
34 | @DisplayName("Parse: disallowed trailing data on array throws exception")
35 | void disallowTrailingArray() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-trailing-array.json5")));
39 |
40 | assertEquals("Trailing data after Json5Array at index 2 [character 3 in line 1]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowTrailingObjectTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowTrailingObjectTest {
33 | @Test
34 | @DisplayName("Parse: disallowed trailing data on object throws exception")
35 | void disallowTrailingObject() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-trailing-object.json5")));
39 |
40 | assertEquals("Trailing data after Json5Object at index 2 [character 3 in line 1]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowHexFloatingTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowHexFloatingTest {
33 | @Test
34 | @DisplayName("Parse: disallowed hex floating-point throws exception")
35 | void disallowHexFloating() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-hex-floating.json5")));
39 |
40 | assertEquals("Hexadecimal floating-point literals are not allowed at index 28 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowCDigitSeparatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowCDigitSeparatorTest {
33 | @Test
34 | @DisplayName("Parse: disallowed C-style digit separators throws exception")
35 | void disallowCCDigitSeparator() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-c-digit-separator.json5")));
39 |
40 | assertEquals("C-style digit separators are not allowed at index 28 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DisallowJavaDigitSeparatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.Json5Options;
21 | import de.marhali.json5.e2e.TestResourceHelper;
22 | import de.marhali.json5.exception.Json5Exception;
23 | import org.junit.jupiter.api.DisplayName;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertThrows;
28 |
29 | /**
30 | * @author Marcel Haßlinger
31 | */
32 | public class DisallowJavaDigitSeparatorTest {
33 | @Test
34 | @DisplayName("Parse: disallowed Java-style digit separators throws exception")
35 | void disallowJavaDigitSeparator() {
36 | var json5 = Json5.builder(Json5Options.Builder::build);
37 |
38 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/disallow-java-digit-separator.json5")));
39 |
40 | assertEquals("Java-style digit separators are not allowed at index 28 [character 1 in line 3]", ex.getMessage());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/TestResourceHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e;
18 |
19 | import java.io.BufferedInputStream;
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import java.nio.charset.StandardCharsets;
24 | import java.util.Optional;
25 |
26 | /**
27 | * @author Marcel Haßlinger
28 | */
29 | public class TestResourceHelper {
30 | public static InputStream getTestResource(String fileName) {
31 | return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
32 | }
33 |
34 | public static String getTestResourceContent(String fileName) throws IOException {
35 | try (BufferedInputStream bis = new BufferedInputStream(getTestResource(fileName))) {
36 | ByteArrayOutputStream buf = new ByteArrayOutputStream();
37 |
38 | for (int result = bis.read(); result != -1; result = bis.read()) {
39 | buf.write((byte) result);
40 | }
41 |
42 | return Optional.ofNullable(buf.toString(StandardCharsets.UTF_8))
43 | .map(s -> s.replace("\r\n", "\n"))
44 | .map(s -> s.replace("\r", "\n"))
45 | .orElse(null);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/Json5NullTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5;
18 |
19 | import de.marhali.json5.fixtures.ToStringFixtures;
20 | import org.junit.jupiter.api.DisplayName;
21 | import org.junit.jupiter.api.Test;
22 |
23 | import static org.junit.jupiter.api.Assertions.*;
24 |
25 | /**
26 | * @author Marcel Haßlinger
27 | */
28 | public class Json5NullTest {
29 | @Test
30 | @DisplayName("deepCopy(): it should just copy comment")
31 | void test_deepCopy() {
32 | Json5Null source = new Json5Null();
33 | String sourceComment = "my comment";
34 | source.setComment(sourceComment);
35 |
36 | Json5Element copy = source.deepCopy();
37 | String newComment = "new comment";
38 | source.setComment(newComment);
39 |
40 | assertTrue(copy.isJson5Null());
41 | assertEquals(sourceComment, copy.getComment());
42 | assertEquals(newComment, source.getComment());
43 | }
44 |
45 | @Test
46 | @DisplayName("getAsString(): it should return null value")
47 | void test_getAsString() {
48 | var element = new Json5Null();
49 | assertEquals("null", element.getAsString());
50 | }
51 |
52 | @Test
53 | void test_toString() {
54 | var element = new Json5Null();
55 | assertEquals("null", element.toString(ToStringFixtures.OPTIONS));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/config/DuplicateKeyStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (C) 2021 SyntaxError404
5 | * Copyright (C) 2025 Marcel Haßlinger
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in all
15 | * copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | * SOFTWARE.
24 | */
25 |
26 | package de.marhali.json5.config;
27 |
28 | /**
29 | * An enum containing all supported behaviors for duplicate keys
30 | *
31 | * @author SyntaxError404
32 | * @author Marcel Haßlinger
33 | */
34 | public enum DuplicateKeyStrategy {
35 |
36 | /**
37 | * Throws an {@link de.marhali.json5.exception.Json5Exception exception} when a key
38 | * is encountered multiple times within the same object
39 | */
40 | UNIQUE,
41 |
42 | /**
43 | * Only the last encountered value is significant,
44 | * all previous occurrences are silently discarded
45 | */
46 | LAST_WINS,
47 |
48 | /**
49 | * Wraps duplicate values inside an {@link de.marhali.json5.Json5Array array},
50 | * effectively treating them as if they were declared as one
51 | */
52 | DUPLICATE
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file.
4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6 |
7 | ## Unreleased
8 |
9 | ### Added
10 |
11 | ### Changed
12 |
13 | ### Deprecated
14 |
15 | ### Removed
16 |
17 | ### Fixed
18 |
19 | ### Security
20 |
21 | ## 3.0.0 - 2025-09-23
22 |
23 | ### Added
24 |
25 | - Option: `stringifyUnixInstants`
26 | - Option: `stringifyAscii`
27 | - Option: `allowNaN`
28 | - Option: `allowInfinity`
29 | - Option: `quoteSingle`
30 | - Option: `quoteless`
31 | - Option: `allowBinaryLiterals`
32 | - Option: `allowOctalLiterals`
33 | - Option: `allowHexFloatingLiterals`
34 | - Option: `allowLongUnicodeEscapes`
35 | - Option: `allowTrailingData`
36 | - Option: `parseComments`
37 | - Option: `writeComments`
38 | - Option: `insertFinalNewline`
39 | - Option: `digitSeparatorStrategy`
40 | - Option: `duplicateBehaviour`
41 |
42 | ### Changed
43 |
44 | - `Json5Null` is no longer a singleton as it allows individual comments
45 | - `Json5Primitive` holds any primitive value besides `Json5Null`
46 | - `Json5Options` with more advanced builder
47 |
48 | ### Removed
49 |
50 | - `Json5Boolean`, `Json5Hexadecimal`, `Json5Number` and `Json5String` in favor of `Json5Primitive`.
51 |
52 | ## 2.0.1 - 2025-09-03
53 |
54 | ### Changed
55 |
56 | - Update dependencies
57 |
58 | ### Fixed
59 |
60 | - Fix unit tests on Windows (#18) - thanks to @Zim-Inn
61 |
62 | ## 2.0.0 - 2022-02-02
63 |
64 | ### Changed
65 |
66 | - For consistency, all methods that return a Json5 data type have been refactored to use the same naming convention
67 |
68 | ## 1.0.1 - 2022-02-22
69 |
70 | ### Changed
71 |
72 | - Json5Parser#parse will return null if provided Json5Lexer does not contain any data
73 |
74 | ## 1.0.0 - 2022-02-21
75 |
76 | ### Added
77 |
78 | - Object-oriented access to all Json5 types
79 | - Parse json5 data by InputStream / Reader / String
80 | - Serialize json5 data to OutputStream / Writer / String
81 | - Json5Options with builder pattern (Json5OptionsBuilder) to configure Json5
82 | - options: allowInvalidSurrogates, quoteSingle, trailingComma, indentFactor
83 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/internal/RadixNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import java.util.Objects;
20 |
21 | /**
22 | * Simple wrapper around {@link Number} that tracks the used radix base.
23 | *
24 | * @author Marcel Haßlinger
25 | */
26 | public class RadixNumber {
27 |
28 | /**
29 | * Referenced number.
30 | */
31 | private final Number number;
32 |
33 | /**
34 | * Radix base to use.
35 | *
36 | * Supported values are:
37 | *
38 | *
Binary: 2
39 | *
Octal: 8
40 | *
Decimal: 10
41 | *
Hex: 16
42 | *
43 | */
44 | private final int radix;
45 |
46 | public RadixNumber(Number number, int radix) {
47 | this.number = number;
48 | this.radix = radix;
49 | }
50 |
51 | public Number getNumber() {
52 | return number;
53 | }
54 |
55 | public int getRadix() {
56 | return radix;
57 | }
58 |
59 | @Override
60 | public String toString() {
61 | return "RadixNumber{" +
62 | "number=" + number +
63 | ", radix=" + radix +
64 | '}';
65 | }
66 |
67 | @Override
68 | public boolean equals(Object o) {
69 | if (o == null || getClass() != o.getClass()) return false;
70 | RadixNumber that = (RadixNumber) o;
71 | return radix == that.radix && Objects.equals(number, that.number);
72 | }
73 |
74 | @Override
75 | public int hashCode() {
76 | return Objects.hash(number, radix);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/exception/Json5Exception.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2021 SyntaxError404
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package de.marhali.json5.exception;
25 |
26 | /**
27 | * Describes exceptions which occur during json5 parsing and serialization.
28 | *
29 | * @author SyntaxError404
30 | */
31 | public class Json5Exception extends RuntimeException {
32 |
33 | /**
34 | * Constructs a new JSONException
35 | */
36 | public Json5Exception() {
37 | super();
38 | }
39 |
40 | /**
41 | * Constructs a new JSONException with a detail message
42 | *
43 | * @param message the detail message
44 | */
45 | public Json5Exception(String message) {
46 | super(message);
47 | }
48 |
49 | /**
50 | * Constructs a new JSONException with a causing exception
51 | *
52 | * @param cause the causing exception
53 | */
54 | public Json5Exception(Throwable cause) {
55 | super(cause);
56 | }
57 |
58 | /**
59 | * Constructs a new JSONException with a detail message and a causing exception
60 | *
61 | * @param message the detail message
62 | * @param cause the causing exception
63 | */
64 | public Json5Exception(String message, Throwable cause) {
65 | super(message, cause);
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/InvalidArrayParserTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.DuplicateKeyStrategy;
21 | import de.marhali.json5.config.Json5Options;
22 | import de.marhali.json5.e2e.TestResourceHelper;
23 | import de.marhali.json5.exception.Json5Exception;
24 | import de.marhali.json5.stream.Json5Lexer;
25 | import de.marhali.json5.stream.Json5Parser;
26 | import org.junit.jupiter.api.Test;
27 |
28 | import java.io.StringReader;
29 |
30 | import static org.junit.jupiter.api.Assertions.assertEquals;
31 | import static org.junit.jupiter.api.Assertions.assertThrows;
32 |
33 | /**
34 | * @author Marcel Haßlinger
35 | */
36 | public class InvalidArrayParserTest {
37 | @Test
38 | void invalid_array_opening_tags_throws() {
39 | var reader = new StringReader("{}");
40 | var lexer = new Json5Lexer(reader, Json5Options.DEFAULT);
41 |
42 | var ex = assertThrows(Json5Exception.class, () -> Json5Parser.parseArray(lexer));
43 |
44 | assertEquals("A Json5Array must begin with '[' at index 0 [character 1 in line 1]", ex.getMessage());
45 | }
46 |
47 | @Test
48 | void invalid_array_closing_tags_throws() {
49 | var json5 = new Json5();
50 |
51 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/invalid-array-closing-tags.json5")));
52 |
53 | assertEquals("A Json5Array must end with ']' at index 17 [character 0 in line 3]", ex.getMessage());
54 | }
55 |
56 | @Test
57 | void invalid_array_missing_comma_throws() {
58 | var json5 = new Json5();
59 |
60 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/invalid-array-missing-comma.json5")));
61 |
62 | assertEquals("Expected ',' or ']' after value, got '[' instead at index 9 [character 3 in line 3]", ex.getMessage());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/InvalidObjectParserTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.config.DuplicateKeyStrategy;
21 | import de.marhali.json5.config.Json5Options;
22 | import de.marhali.json5.e2e.TestResourceHelper;
23 | import de.marhali.json5.exception.Json5Exception;
24 | import de.marhali.json5.stream.Json5Lexer;
25 | import de.marhali.json5.stream.Json5Parser;
26 | import org.junit.jupiter.api.Test;
27 |
28 | import java.io.StringReader;
29 |
30 | import static org.junit.jupiter.api.Assertions.*;
31 |
32 | /**
33 | * @author Marcel Haßlinger
34 | */
35 | public class InvalidObjectParserTest {
36 | @Test
37 | void invalid_object_opening_tags_throws() {
38 | var reader = new StringReader("[]");
39 | var lexer = new Json5Lexer(reader, Json5Options.DEFAULT);
40 |
41 | var ex = assertThrows(Json5Exception.class, () -> Json5Parser.parseObject(lexer));
42 |
43 | assertEquals("A Json5Object must begin with '{' at index 0 [character 1 in line 1]", ex.getMessage());
44 | }
45 |
46 | @Test
47 | void invalid_object_closing_tags_throws() {
48 | var json5 = new Json5();
49 |
50 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/invalid-object-closing-tags.json5")));
51 |
52 | assertEquals("A Json5Object must end with '}' at index 42 [character 0 in line 4]", ex.getMessage());
53 | }
54 |
55 | @Test
56 | void invalid_object_missing_key_suffix_throws() {
57 | var json5 = new Json5();
58 |
59 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/invalid-object-missing-key-suffix.json5")));
60 |
61 | assertEquals("Expected ':' after a key, got 'f' instead at index 12 [character 11 in line 2]", ex.getMessage());
62 | }
63 |
64 | @Test
65 | void invalid_object_missing_comma_throws() {
66 | var json5 = new Json5();
67 |
68 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/invalid-object-missing-comma.json5")));
69 |
70 | assertEquals("Expected ',' or '}' after value, got 's' instead at index 22 [character 3 in line 3]", ex.getMessage());
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 | @rem SPDX-License-Identifier: Apache-2.0
17 | @rem
18 |
19 | @if "%DEBUG%"=="" @echo off
20 | @rem ##########################################################################
21 | @rem
22 | @rem Gradle startup script for Windows
23 | @rem
24 | @rem ##########################################################################
25 |
26 | @rem Set local scope for the variables with windows NT shell
27 | if "%OS%"=="Windows_NT" setlocal
28 |
29 | set DIRNAME=%~dp0
30 | if "%DIRNAME%"=="" set DIRNAME=.
31 | @rem This is normally unused
32 | set APP_BASE_NAME=%~n0
33 | set APP_HOME=%DIRNAME%
34 |
35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
37 |
38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
40 |
41 | @rem Find java.exe
42 | if defined JAVA_HOME goto findJavaFromJavaHome
43 |
44 | set JAVA_EXE=java.exe
45 | %JAVA_EXE% -version >NUL 2>&1
46 | if %ERRORLEVEL% equ 0 goto execute
47 |
48 | echo. 1>&2
49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50 | echo. 1>&2
51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52 | echo location of your Java installation. 1>&2
53 |
54 | goto fail
55 |
56 | :findJavaFromJavaHome
57 | set JAVA_HOME=%JAVA_HOME:"=%
58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
59 |
60 | if exist "%JAVA_EXE%" goto execute
61 |
62 | echo. 1>&2
63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64 | echo. 1>&2
65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66 | echo location of your Java installation. 1>&2
67 |
68 | goto fail
69 |
70 | :execute
71 | @rem Setup the command line
72 |
73 | set CLASSPATH=
74 |
75 |
76 | @rem Execute Gradle
77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
78 |
79 | :end
80 | @rem End local scope for the variables with windows NT shell
81 | if %ERRORLEVEL% equ 0 goto mainEnd
82 |
83 | :fail
84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
85 | rem the _cmd.exe /c_ return code!
86 | set EXIT_CODE=%ERRORLEVEL%
87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
89 | exit /b %EXIT_CODE%
90 |
91 | :mainEnd
92 | if "%OS%"=="Windows_NT" endlocal
93 |
94 | :omega
95 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | # GitHub Actions Workflow created for handling the release process based on the draft release prepared with the Build workflow.
2 |
3 | name: Release
4 | on:
5 | release:
6 | types: [prereleased, released]
7 |
8 | jobs:
9 |
10 | # Prepare and publish the library to Maven Central
11 | release:
12 | name: Publish Library
13 | runs-on: ubuntu-latest
14 | permissions:
15 | contents: write
16 | pull-requests: write
17 | steps:
18 |
19 | # Check out the current repository
20 | - name: Fetch Sources
21 | uses: actions/checkout@v5
22 | with:
23 | ref: ${{ github.event.release.tag_name }}
24 |
25 | # Set up the Java environment for the next steps
26 | - name: Setup Java
27 | uses: actions/setup-java@v5
28 | with:
29 | distribution: adopt
30 | java-version: 11
31 |
32 | # Setup Gradle
33 | - name: Setup Gradle
34 | uses: gradle/actions/setup-gradle@v4
35 | with:
36 | cache-read-only: true
37 |
38 | # Update the Unreleased section with the current release note
39 | - name: Patch Changelog
40 | if: ${{ github.event.release.body != '' }}
41 | env:
42 | CHANGELOG: ${{ github.event.release.body }}
43 | run: |
44 | RELEASE_NOTE="./build/tmp/release_note.txt"
45 | mkdir -p "$(dirname "$RELEASE_NOTE")"
46 | echo "$CHANGELOG" > $RELEASE_NOTE
47 |
48 | ./gradlew patchChangelog --release-note-file=$RELEASE_NOTE
49 |
50 | # Publish the library to Maven Central
51 | - name: Publish Library
52 | env:
53 | ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.ORG_GRADLE_PROJECT_mavenCentralUsername }}
54 | ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.ORG_GRADLE_PROJECT_mavenCentralPassword }}
55 | ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.ORG_GRADLE_PROJECT_signingInMemoryKey }}
56 | ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.ORG_GRADLE_PROJECT_signingInMemoryKeyPassword }}
57 | run: ./gradlew publishToMavenCentral
58 |
59 | # Upload an artifact as a release asset
60 | - name: Upload Release Asset
61 | env:
62 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63 | run: gh release upload ${{ github.event.release.tag_name }} ./build/libs/*
64 |
65 | # Create a pull request
66 | - name: Create Pull Request
67 | if: ${{ github.event.release.body != '' }}
68 | env:
69 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70 | run: |
71 | VERSION="${{ github.event.release.tag_name }}"
72 | BRANCH="changelog-update-$VERSION"
73 | LABEL="release changelog"
74 |
75 | git config user.email "action@github.com"
76 | git config user.name "GitHub Action"
77 |
78 | git checkout -b $BRANCH
79 | git commit -am "Changelog update - $VERSION"
80 | git push --set-upstream origin $BRANCH
81 |
82 | gh label create "$LABEL" \
83 | --description "Pull requests with release changelog update" \
84 | --force \
85 | || true
86 |
87 | gh pr create \
88 | --title "Changelog update - \`$VERSION\`" \
89 | --body "Current pull request contains patched \`CHANGELOG.md\` file for the \`$VERSION\` version." \
90 | --label "$LABEL" \
91 | --head $BRANCH
92 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/internal/LazilyParsedNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package de.marhali.json5.internal;
17 |
18 | import java.io.IOException;
19 | import java.io.InvalidObjectException;
20 | import java.io.ObjectInputStream;
21 | import java.io.ObjectStreamException;
22 | import java.math.BigDecimal;
23 |
24 | /**
25 | * This class holds a number value that is lazily converted to a specific number type
26 | *
27 | * @author Inderjeet Singh
28 | */
29 | public final class LazilyParsedNumber extends Number {
30 | private final String value;
31 |
32 | /**
33 | * @param value must not be null
34 | */
35 | public LazilyParsedNumber(String value) {
36 | this.value = value;
37 | }
38 |
39 | @Override
40 | public int intValue() {
41 | try {
42 | return Integer.parseInt(value);
43 | } catch (NumberFormatException e) {
44 | try {
45 | return (int) Long.parseLong(value);
46 | } catch (NumberFormatException nfe) {
47 | return new BigDecimal(value).intValue();
48 | }
49 | }
50 | }
51 |
52 | @Override
53 | public long longValue() {
54 | try {
55 | return Long.parseLong(value);
56 | } catch (NumberFormatException e) {
57 | return new BigDecimal(value).longValue();
58 | }
59 | }
60 |
61 | @Override
62 | public float floatValue() {
63 | return Float.parseFloat(value);
64 | }
65 |
66 | @Override
67 | public double doubleValue() {
68 | return Double.parseDouble(value);
69 | }
70 |
71 | @Override
72 | public String toString() {
73 | return value;
74 | }
75 |
76 | /**
77 | * If somebody is unlucky enough to have to serialize one of these, serialize
78 | * it as a BigDecimal so that they won't need Gson on the other side to
79 | * deserialize it.
80 | *
81 | * @return Value as {@link BigDecimal}
82 | * @throws ObjectStreamException Stream exception
83 | */
84 | private Object writeReplace() throws ObjectStreamException {
85 | return new BigDecimal(value);
86 | }
87 |
88 | private void readObject(ObjectInputStream in) throws IOException {
89 | // Don't permit directly deserializing this class; writeReplace() should have written a replacement
90 | throw new InvalidObjectException("Deserialization is unsupported");
91 | }
92 |
93 | @Override
94 | public int hashCode() {
95 | return value.hashCode();
96 | }
97 |
98 | @Override
99 | public boolean equals(Object obj) {
100 | if (this == obj) {
101 | return true;
102 | }
103 | if (obj instanceof LazilyParsedNumber) {
104 | LazilyParsedNumber other = (LazilyParsedNumber) obj;
105 | return value == other.value || value.equals(other.value);
106 | }
107 | return false;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/e2e/failures/DuplicateObjectKeyTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.e2e.failures;
18 |
19 | import de.marhali.json5.Json5;
20 | import de.marhali.json5.Json5Element;
21 | import de.marhali.json5.Json5Primitive;
22 | import de.marhali.json5.config.DuplicateKeyStrategy;
23 | import de.marhali.json5.e2e.TestResourceHelper;
24 | import de.marhali.json5.exception.Json5Exception;
25 | import org.junit.jupiter.api.DisplayName;
26 | import org.junit.jupiter.api.Test;
27 |
28 | import static org.junit.jupiter.api.Assertions.*;
29 |
30 | /**
31 | * @author Marcel Haßlinger
32 | */
33 | public class DuplicateObjectKeyTest {
34 | @Test
35 | @DisplayName("Parse: duplicate key on object throws with DuplicateStrategy.UNIQUE")
36 | void disallowDuplicateObjectKey() {
37 | var json5 = Json5.builder(builder -> builder.duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE).build());
38 |
39 | var ex = assertThrows(Json5Exception.class, () -> json5.parse(TestResourceHelper.getTestResourceContent("e2e/failures/duplicate-object-key.json5")));
40 |
41 | assertEquals("Duplicate key \"alpha\" at index 37 [character 8 in line 3]", ex.getMessage());
42 | }
43 |
44 | @Test
45 | @DisplayName("Parse: duplicate key on object, but last one wins with DuplicateStrategy.LAST_WINS")
46 | void duplicateObjectKeyLastWins() {
47 | var json5 = Json5.builder(builder -> builder.duplicateKeyStrategy(DuplicateKeyStrategy.LAST_WINS).build());
48 |
49 | Json5Element element = json5.parse(TestResourceHelper.getTestResource("e2e/failures/duplicate-object-key.json5"));
50 |
51 | assertTrue(element.isJson5Object());
52 | var object = element.getAsJson5Object();
53 |
54 | assertEquals(3, object.size());
55 | assertEquals("secondAlphaValue", object.get("alpha").getAsString());
56 | assertEquals("bravoValue", object.get("bravo").getAsString());
57 | assertEquals("secondCharlieValue", object.get("charlie").getAsString());
58 | }
59 |
60 | @Test
61 | @DisplayName("Parse: duplicate key on object, but all entries are converted to array with DuplicateStrategy.DUPLICATE")
62 | void duplicateObjectKeyAsArray() {
63 | var json5 = Json5.builder(builder -> builder.duplicateKeyStrategy(DuplicateKeyStrategy.DUPLICATE).build());
64 |
65 | Json5Element element = json5.parse(TestResourceHelper.getTestResource("e2e/failures/duplicate-object-key.json5"));
66 |
67 | assertTrue(element.isJson5Object());
68 | var object = element.getAsJson5Object();
69 |
70 | assertEquals(3, object.size());
71 | assertTrue(object.get("alpha").isJson5Array());
72 | assertEquals(Json5Primitive.fromString("firstAlphaValue"), object.get("alpha").getAsJson5Array().get(0));
73 | assertEquals(Json5Primitive.fromString("secondAlphaValue"), object.get("alpha").getAsJson5Array().get(1));
74 | assertEquals("bravoValue", object.get("bravo").getAsString());
75 | assertEquals(Json5Primitive.fromString("firstCharlieValue"), object.get("charlie").getAsJson5Array().get(0));
76 | assertEquals(Json5Primitive.fromString("secondCharlieValue"), object.get("charlie").getAsJson5Array().get(1));
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/Json5Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5;
18 |
19 | import de.marhali.json5.config.Json5Options;
20 | import org.junit.jupiter.api.Test;
21 |
22 | import java.io.*;
23 | import java.util.function.Function;
24 |
25 | import static org.junit.jupiter.api.Assertions.*;
26 |
27 | /**
28 | * @author Marcel Haßlinger
29 | */
30 | public class Json5Test {
31 |
32 | // --- helpers ---
33 | private static Json5Object sampleObject() {
34 | Json5Object o = new Json5Object();
35 | o.addProperty("n", 42);
36 | o.addProperty("s", "hi");
37 | o.addProperty("b", true);
38 | Json5Array a = new Json5Array();
39 | a.add(1);
40 | a.add("x");
41 | o.add("arr", a);
42 | return o;
43 | }
44 |
45 | @Test
46 | void builder_applies_function_and_returns_instance() {
47 | Function fn = b -> Json5Options.DEFAULT;
48 | Json5 j = Json5.builder(fn);
49 | assertNotNull(j);
50 | assertInstanceOf(Json5Object.class, j.parse("{}"));
51 | }
52 |
53 | @Test
54 | void constructor_with_options_and_default_and_null_checks() {
55 | assertDoesNotThrow(() -> new Json5(Json5Options.DEFAULT));
56 | assertDoesNotThrow(() -> new Json5());
57 |
58 | assertThrows(NullPointerException.class, () -> new Json5(null));
59 | }
60 |
61 | @Test
62 | void parse_from_string_object_array_and_empty() {
63 | Json5 json5 = new Json5();
64 |
65 | assertInstanceOf(Json5Object.class, json5.parse("{ }"));
66 | assertInstanceOf(Json5Array.class, json5.parse("[ ]"));
67 |
68 | assertNull(json5.parse(""));
69 | }
70 |
71 | @Test
72 | void parse_from_reader_and_stream() {
73 | Json5 json5 = new Json5();
74 |
75 | // Reader
76 | Reader r = new StringReader("{a:1}");
77 | Json5Element e1 = json5.parse(r);
78 | assertInstanceOf(Json5Object.class, e1);
79 |
80 | // InputStream
81 | InputStream in = new ByteArrayInputStream("[1,2]".getBytes());
82 | Json5Element e2 = json5.parse(in);
83 | assertInstanceOf(Json5Array.class, e2);
84 | }
85 |
86 | @Test
87 | void parse_null_arguments_throw_npe() {
88 | Json5 json5 = new Json5();
89 | assertThrows(NullPointerException.class, () -> json5.parse((String) null));
90 | assertThrows(NullPointerException.class, () -> json5.parse((Reader) null));
91 | assertThrows(NullPointerException.class, () -> json5.parse((InputStream) null));
92 | }
93 |
94 | @Test
95 | void serialize_null_arguments_throw_npe() {
96 | Json5 json5 = new Json5();
97 | Json5Element elem = sampleObject();
98 |
99 | assertThrows(NullPointerException.class, () -> json5.serialize(null));
100 | assertThrows(NullPointerException.class, () -> json5.serialize(null, new StringWriter()));
101 | assertThrows(NullPointerException.class, () -> json5.serialize(elem, (Writer) null));
102 | assertThrows(NullPointerException.class, () -> json5.serialize(null, new ByteArrayOutputStream()));
103 | assertThrows(NullPointerException.class, () -> json5.serialize(elem, (OutputStream) null));
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/internal/NonNullElementWrapperList.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import java.util.AbstractList;
20 | import java.util.ArrayList;
21 | import java.util.Collection;
22 | import java.util.List;
23 | import java.util.Objects;
24 | import java.util.RandomAccess;
25 |
26 | /**
27 | * {@link List} which wraps another {@code List} but prevents insertion of {@code null} elements.
28 | * Methods which only perform checks with the element argument (e.g. {@link #contains(Object)}) do
29 | * not throw exceptions for {@code null} arguments.
30 | */
31 | public class NonNullElementWrapperList extends AbstractList implements RandomAccess {
32 | // Explicitly specify ArrayList as type to guarantee that delegate implements RandomAccess
33 | private final ArrayList delegate;
34 |
35 | @SuppressWarnings("NonApiType")
36 | public NonNullElementWrapperList(ArrayList delegate) {
37 | this.delegate = Objects.requireNonNull(delegate);
38 | }
39 |
40 | @Override
41 | public E get(int index) {
42 | return delegate.get(index);
43 | }
44 |
45 | @Override
46 | public int size() {
47 | return delegate.size();
48 | }
49 |
50 | private E nonNull(E element) {
51 | if (element == null) {
52 | throw new NullPointerException("Element must be non-null");
53 | }
54 | return element;
55 | }
56 |
57 | @Override
58 | public E set(int index, E element) {
59 | return delegate.set(index, nonNull(element));
60 | }
61 |
62 | @Override
63 | public void add(int index, E element) {
64 | delegate.add(index, nonNull(element));
65 | }
66 |
67 | @Override
68 | public E remove(int index) {
69 | return delegate.remove(index);
70 | }
71 |
72 | /* The following methods are overridden because their default implementation is inefficient */
73 |
74 | @Override
75 | public void clear() {
76 | delegate.clear();
77 | }
78 |
79 | @SuppressWarnings("UngroupedOverloads") // this is intentionally ungrouped, see comment above
80 | @Override
81 | public boolean remove(Object o) {
82 | return delegate.remove(o);
83 | }
84 |
85 | @Override
86 | public boolean removeAll(Collection> c) {
87 | return delegate.removeAll(c);
88 | }
89 |
90 | @Override
91 | public boolean retainAll(Collection> c) {
92 | return delegate.retainAll(c);
93 | }
94 |
95 | @Override
96 | public boolean contains(Object o) {
97 | return delegate.contains(o);
98 | }
99 |
100 | @Override
101 | public int indexOf(Object o) {
102 | return delegate.indexOf(o);
103 | }
104 |
105 | @Override
106 | public int lastIndexOf(Object o) {
107 | return delegate.lastIndexOf(o);
108 | }
109 |
110 | @Override
111 | public Object[] toArray() {
112 | return delegate.toArray();
113 | }
114 |
115 | @Override
116 | public T[] toArray(T[] a) {
117 | return delegate.toArray(a);
118 | }
119 |
120 | @Override
121 | public boolean equals(Object o) {
122 | return delegate.equals(o);
123 | }
124 |
125 | @Override
126 | public int hashCode() {
127 | return delegate.hashCode();
128 | }
129 |
130 | // Maybe also delegate List#sort and List#spliterator in the future, but that
131 | // requires Android API level 24
132 | }
133 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/fixtures/Json5OptionsFixtures.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.fixtures;
18 |
19 | import de.marhali.json5.config.DigitSeparatorStrategy;
20 | import de.marhali.json5.config.DuplicateKeyStrategy;
21 | import de.marhali.json5.config.Json5Options;
22 |
23 | /**
24 | * @author Marcel Haßlinger
25 | */
26 | public class Json5OptionsFixtures {
27 | public static Json5Options PRETTY = Json5Options.builder()
28 | .allowNaN()
29 | .allowInfinity()
30 | .allowInvalidSurrogates()
31 | .quoteless()
32 | .parseComments()
33 | .writeComments()
34 | .trailingComma()
35 | .insertFinalNewline()
36 | .digitSeparatorStrategy(DigitSeparatorStrategy.NONE)
37 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
38 | .prettyPrinting()
39 | .build();
40 |
41 | public static Json5Options PRETTY_NO_COMMENTS = Json5Options.builder()
42 | .allowNaN()
43 | .allowInfinity()
44 | .allowInvalidSurrogates()
45 | .quoteless()
46 | .trailingComma()
47 | .insertFinalNewline()
48 | .digitSeparatorStrategy(DigitSeparatorStrategy.NONE)
49 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
50 | .prettyPrinting()
51 | .build();
52 |
53 | public static Json5Options MINIFY = Json5Options.builder()
54 | .allowNaN()
55 | .allowInfinity()
56 | .allowInvalidSurrogates()
57 | .quoteless()
58 | .parseComments()
59 | .writeComments()
60 | .insertFinalNewline()
61 | .digitSeparatorStrategy(DigitSeparatorStrategy.NONE)
62 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
63 | .indentFactor(0)
64 | .build();
65 |
66 | public static Json5Options MINIFY_NO_COMMENTS = Json5Options.builder()
67 | .allowNaN()
68 | .allowInfinity()
69 | .allowInvalidSurrogates()
70 | .quoteless()
71 | .insertFinalNewline()
72 | .digitSeparatorStrategy(DigitSeparatorStrategy.NONE)
73 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
74 | .indentFactor(0)
75 | .build();
76 |
77 | public static Json5Options EXTENSIONS_C_STYLE = Json5Options.builder()
78 | .allowNaN()
79 | .allowInfinity()
80 | .allowInvalidSurrogates()
81 | .allowBinaryLiterals()
82 | .allowOctalLiterals()
83 | .allowHexFloatingLiterals()
84 | .allowLongUnicodeEscapes()
85 | .quoteless()
86 | .parseComments()
87 | .writeComments()
88 | .trailingComma()
89 | .insertFinalNewline()
90 | .digitSeparatorStrategy(DigitSeparatorStrategy.C_STYLE)
91 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
92 | .prettyPrinting()
93 | .build();
94 |
95 | public static Json5Options EXTENSIONS_JAVA_STYLE = Json5Options.builder()
96 | .allowNaN()
97 | .allowInfinity()
98 | .allowInvalidSurrogates()
99 | .allowBinaryLiterals()
100 | .allowOctalLiterals()
101 | .allowHexFloatingLiterals()
102 | .allowLongUnicodeEscapes()
103 | .quoteless()
104 | .parseComments()
105 | .writeComments()
106 | .trailingComma()
107 | .insertFinalNewline()
108 | .digitSeparatorStrategy(DigitSeparatorStrategy.JAVA_STYLE)
109 | .duplicateKeyStrategy(DuplicateKeyStrategy.UNIQUE)
110 | .prettyPrinting()
111 | .build();
112 | }
113 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/internal/NumberLimitsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import java.math.BigDecimal;
22 | import java.math.BigInteger;
23 |
24 | import static org.junit.jupiter.api.Assertions.*;
25 |
26 | /**
27 | * @author Marcel Haßlinger
28 | */
29 | public class NumberLimitsTest {
30 | private static String repeat(char c, int n) {
31 | StringBuilder sb = new StringBuilder(n);
32 | for (int i = 0; i < n; i++) sb.append(c);
33 | return sb.toString();
34 | }
35 |
36 | @Test
37 | void parseBigInteger_allows_exactly_10000_chars() {
38 | String s = repeat('7', 10_000);
39 | BigInteger bi = assertDoesNotThrow(() -> NumberLimits.parseBigInteger(s));
40 | assertEquals(new BigInteger(s), bi);
41 | }
42 |
43 | @Test
44 | void parseBigInteger_rejects_length_over_10000_and_includes_prefix_in_message() {
45 | String s = repeat('9', 10_001);
46 | NumberFormatException ex =
47 | assertThrows(NumberFormatException.class, () -> NumberLimits.parseBigInteger(s));
48 | String expectedPrefix = "Number string too large: " + s.substring(0, 30) + "...";
49 | assertTrue(ex.getMessage().startsWith(expectedPrefix),
50 | () -> "Msg mismatch.\nExpected prefix: " + expectedPrefix + "\nActual: " + ex.getMessage());
51 | }
52 |
53 | @Test
54 | void parseBigDecimal_allows_exactly_10000_chars() {
55 | String s = repeat('1', 10_000);
56 | BigDecimal bd = assertDoesNotThrow(() -> NumberLimits.parseBigDecimal(s));
57 | assertEquals(new BigDecimal(s), bd);
58 | }
59 |
60 | @Test
61 | void parseBigDecimal_rejects_length_over_10000_and_includes_prefix_in_message() {
62 | String s = repeat('3', 10_001);
63 | NumberFormatException ex =
64 | assertThrows(NumberFormatException.class, () -> NumberLimits.parseBigDecimal(s));
65 | String expectedPrefix = "Number string too large: " + s.substring(0, 30) + "...";
66 | assertTrue(ex.getMessage().startsWith(expectedPrefix));
67 | }
68 |
69 | @Test
70 | void parseBigDecimal_allows_scale_abs_9999_both_signs() {
71 | BigDecimal bdNegExp = assertDoesNotThrow(() -> NumberLimits.parseBigDecimal("1E-9999"));
72 | assertEquals(9_999, bdNegExp.scale());
73 |
74 | BigDecimal bdPosExp = assertDoesNotThrow(() -> NumberLimits.parseBigDecimal("1E+9999"));
75 | assertEquals(-9_999, bdPosExp.scale());
76 | }
77 |
78 | @Test
79 | void parseBigDecimal_rejects_scale_10000_or_more_negative() {
80 | NumberFormatException ex =
81 | assertThrows(NumberFormatException.class, () -> NumberLimits.parseBigDecimal("1E-10000"));
82 | assertTrue(ex.getMessage().contains("unsupported scale"));
83 | }
84 |
85 | @Test
86 | void parseBigDecimal_rejects_scale_10000_or_more_positive() {
87 | NumberFormatException ex =
88 | assertThrows(NumberFormatException.class, () -> NumberLimits.parseBigDecimal("1E+10000"));
89 | assertTrue(ex.getMessage().contains("unsupported scale"));
90 | }
91 |
92 | @Test
93 | void parseBigDecimal_parses_normal_numbers() {
94 | BigDecimal bd = assertDoesNotThrow(() -> NumberLimits.parseBigDecimal("12345.6789"));
95 | assertEquals(new BigDecimal("12345.6789"), bd);
96 | }
97 |
98 | @Test
99 | void parseBigInteger_parses_normal_numbers() {
100 | BigInteger bi = assertDoesNotThrow(() -> NumberLimits.parseBigInteger("-9007199254740993"));
101 | assertEquals(new BigInteger("-9007199254740993"), bi);
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/internal/LazilyParsedNumberTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import java.io.*;
22 | import java.math.BigDecimal;
23 |
24 | import static org.junit.jupiter.api.Assertions.*;
25 |
26 | /**
27 | * @author Marcel Haßlinger
28 | */
29 | class LazilyParsedNumberTest {
30 |
31 | @Test
32 | void toString_returnsOriginal() {
33 | assertEquals("123", new LazilyParsedNumber("123").toString());
34 | assertEquals("1.50", new LazilyParsedNumber("1.50").toString());
35 | }
36 |
37 | @Test
38 | void int_long_parse_directInteger() {
39 | LazilyParsedNumber n = new LazilyParsedNumber("123");
40 | assertEquals(123, n.intValue());
41 | assertEquals(123L, n.longValue());
42 | }
43 |
44 | @Test
45 | void float_double_parse_decimal() {
46 | LazilyParsedNumber n = new LazilyParsedNumber("1.5");
47 | assertEquals(1.5f, n.floatValue(), 0.000001f);
48 | assertEquals(1.5, n.doubleValue(), 0.0000000001);
49 | // Integer/Long parse fails -> BigDecimal.intValue()
50 | assertEquals(new BigDecimal("1.5").intValue(), n.intValue());
51 | }
52 |
53 | @Test
54 | void intValue_fallsBackToLongAndNarrows() {
55 | // 2147483648 = Integer.MAX_VALUE + 1
56 | String s = "2147483648";
57 | LazilyParsedNumber n = new LazilyParsedNumber(s);
58 |
59 | // Long.parseLong works, intValue uses (int) long → Overflows to MIN_VALUE
60 | long expectedLong = Long.parseLong(s);
61 | int expectedInt = (int) expectedLong;
62 |
63 | assertEquals(expectedLong, n.longValue());
64 | assertEquals(expectedInt, n.intValue());
65 | }
66 |
67 | @Test
68 | void longAndInt_fallBackToBigDecimalWhenTooLargeForLong() {
69 | // Long.MAX_VALUE + 1 -> Long.parseLong causes NFE, BigDecimal will be used
70 | String s = "9223372036854775808";
71 | LazilyParsedNumber n = new LazilyParsedNumber(s);
72 |
73 | long expectedLong = new BigDecimal(s).longValue();
74 | int expectedInt = new BigDecimal(s).intValue();
75 |
76 | assertEquals(expectedLong, n.longValue());
77 | assertEquals(expectedInt, n.intValue());
78 | assertEquals(Double.parseDouble(s), n.doubleValue(), 0.0);
79 | }
80 |
81 | @Test
82 | void equals_and_hashCode_behave() {
83 | LazilyParsedNumber a1 = new LazilyParsedNumber("42");
84 | LazilyParsedNumber a2 = new LazilyParsedNumber("42");
85 | LazilyParsedNumber b = new LazilyParsedNumber("043");
86 |
87 | assertEquals(a1, a1); // reflexive
88 | assertEquals(a1, a2); // same string content
89 | assertNotEquals(a1, b); // other content
90 | assertNotEquals(a1, new Object()); // other type
91 |
92 | assertEquals("42".hashCode(), a1.hashCode());
93 | }
94 |
95 | @Test
96 | void serialization_usesWriteReplace_returnsBigDecimalOnRead() throws Exception {
97 | LazilyParsedNumber n = new LazilyParsedNumber("123.45");
98 | Object roundTripped = roundTrip(n);
99 |
100 | assertTrue(roundTripped instanceof BigDecimal, "Deserialize should return BigDecimal");
101 | assertEquals(new BigDecimal("123.45"), roundTripped);
102 | }
103 |
104 | private static Object roundTrip(Object o) throws IOException, ClassNotFoundException {
105 | byte[] bytes;
106 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
107 | ObjectOutputStream oos = new ObjectOutputStream(baos)) {
108 | oos.writeObject(o);
109 | oos.flush();
110 | bytes = baos.toByteArray();
111 | }
112 | try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
113 | return ois.readObject();
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/internal/EcmaScriptIdentifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | /**
20 | * @author Marcel Haßlinger
21 | */
22 | public class EcmaScriptIdentifier {
23 | // Zero Width Non-Joiner / Joiner
24 | private static final int ZWNJ = 0x200C;
25 | private static final int ZWJ = 0x200D;
26 |
27 | private EcmaScriptIdentifier() {
28 | }
29 |
30 | /**
31 | * Checks whether the provided {@link String} is a valid ES5.1 IdentifierName.
32 | *
33 | * @param raw Input to check
34 | * @return true if valid identifier, otherwise false
35 | * @see https://262.ecma-international.org/5.1/#sec-7.6
36 | */
37 | public static boolean isValid(String raw) {
38 | if (raw == null || raw.isEmpty()) return false;
39 |
40 | // Transform \\uXXXX-Escapes into real codepoints (ES5.1 allows escape in IdentifierName
41 | String unescaped = decodeEs5UnicodeEscapes(raw);
42 | if (unescaped == null || unescaped.isEmpty()) return false;
43 |
44 | int i = 0;
45 | int cp = unescaped.codePointAt(i);
46 | if (!isIdentifierStartES5(cp)) return false;
47 | i += Character.charCount(cp);
48 |
49 | while (i < unescaped.length()) {
50 | cp = unescaped.codePointAt(i);
51 | if (!isIdentifierPartES5(cp)) return false;
52 | i += Character.charCount(cp);
53 | }
54 | return true;
55 | }
56 |
57 | private static boolean isIdentifierStartES5(int cp) {
58 | // '$' and '_' explicit
59 | if (cp == '$' || cp == '_') return true;
60 |
61 | int t = Character.getType(cp);
62 | // Unicode categories: Lu, Ll, Lt, Lm, Lo, Nl
63 | switch (t) {
64 | case Character.UPPERCASE_LETTER: // Lu
65 | case Character.LOWERCASE_LETTER: // Ll
66 | case Character.TITLECASE_LETTER: // Lt
67 | case Character.MODIFIER_LETTER: // Lm
68 | case Character.OTHER_LETTER: // Lo
69 | case Character.LETTER_NUMBER: // Nl
70 | return true;
71 | default:
72 | return false;
73 | }
74 | }
75 |
76 | private static boolean isIdentifierPartES5(int cp) {
77 | if (isIdentifierStartES5(cp)) return true;
78 | if (cp == ZWNJ || cp == ZWJ) return true; // U+200C/U+200D are alowed
79 |
80 | int t = Character.getType(cp);
81 | // Additional categories: Mn, Mc, Nd, Pc
82 | switch (t) {
83 | case Character.NON_SPACING_MARK: // Mn
84 | case Character.COMBINING_SPACING_MARK: // Mc
85 | case Character.DECIMAL_DIGIT_NUMBER: // Nd
86 | case Character.CONNECTOR_PUNCTUATION: // Pc (e.g. underline, but already covered)
87 | return true;
88 | default:
89 | return false;
90 | }
91 | }
92 |
93 | /**
94 | * Decodes ES5-style Unicode-Escapes \\uXXXX inside a Identifier.
95 | *
96 | * @return {@code null}, if an escape is syntactically invalid
97 | */
98 | private static String decodeEs5UnicodeEscapes(String s) {
99 | StringBuilder out = new StringBuilder(s.length());
100 | for (int i = 0; i < s.length(); ) {
101 | char ch = s.charAt(i);
102 | if (ch == '\\') {
103 | if (i + 1 < s.length() && s.charAt(i + 1) == 'u') {
104 | // Expect 4 hex chars (ES5.1; no \\u{...} syntax)
105 | if (i + 6 > s.length()) return null;
106 | String hex = s.substring(i + 2, i + 6);
107 | int codeUnit = parse4Hex(hex);
108 | if (codeUnit < 0) return null;
109 | out.append((char) codeUnit);
110 | i += 6;
111 | } else {
112 | // Other backslashes are not allowed
113 | return null;
114 | }
115 | } else {
116 | out.append(ch);
117 | i++;
118 | }
119 | }
120 | // Maybe Surrogates...
121 | return out.toString();
122 | }
123 |
124 | private static int parse4Hex(String hex4) {
125 | if (hex4.length() != 4) return -1;
126 | int val = 0;
127 | for (int i = 0; i < 4; i++) {
128 | char c = hex4.charAt(i);
129 | int d = Character.digit(c, 16);
130 | if (d < 0) return -1;
131 | val = (val << 4) | d;
132 | }
133 | return val;
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/internal/NonNullElementWrapperListTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import java.util.*;
22 |
23 | import static org.junit.jupiter.api.Assertions.*;
24 |
25 | /**
26 | * @author Marcel Haßlinger
27 | */
28 | class NonNullElementWrapperListTest {
29 |
30 | private static NonNullElementWrapperList wrap(ArrayList delegate) {
31 | return new NonNullElementWrapperList<>(delegate);
32 | }
33 |
34 | @Test
35 | void constructor_nullDelegate_throwsNPE() {
36 | assertThrows(NullPointerException.class, () -> new NonNullElementWrapperList<>(null));
37 | }
38 |
39 | @Test
40 | void isRandomAccess() {
41 | NonNullElementWrapperList list = wrap(new ArrayList<>());
42 | assertTrue(list instanceof RandomAccess, "Wrapper should implement RandomAccess");
43 | }
44 |
45 | @Test
46 | void size_get_add_nonNull_ok() {
47 | NonNullElementWrapperList list = wrap(new ArrayList<>());
48 | assertEquals(0, list.size());
49 | list.add(0, "a");
50 | list.add(1, "b");
51 | assertEquals(2, list.size());
52 | assertEquals("a", list.get(0));
53 | assertEquals("b", list.get(1));
54 | }
55 |
56 | @Test
57 | void add_null_throwsNPE() {
58 | NonNullElementWrapperList list = wrap(new ArrayList<>());
59 | NullPointerException ex = assertThrows(NullPointerException.class, () -> list.add(0, null));
60 | assertTrue(ex.getMessage() == null || ex.getMessage().toLowerCase().contains("non-null"));
61 | }
62 |
63 | @Test
64 | void set_replacesAndReturnsOld_nonNull_ok() {
65 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("x", "y")));
66 | String old = list.set(1, "z");
67 | assertEquals("y", old);
68 | assertEquals(Arrays.asList("x", "z"), new ArrayList<>(list));
69 | }
70 |
71 | @Test
72 | void set_null_throwsNPE() {
73 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("x")));
74 | assertThrows(NullPointerException.class, () -> list.set(0, null));
75 | assertEquals(Collections.singletonList("x"), new ArrayList<>(list));
76 | }
77 |
78 | @Test
79 | void contains_and_indexOf_withNull_doNotThrow() {
80 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("a", "b")));
81 | assertFalse(list.contains(null));
82 | assertEquals(-1, list.indexOf(null));
83 | assertEquals(-1, list.lastIndexOf(null));
84 | }
85 |
86 | @Test
87 | void remove_byIndex_and_byObject_behave() {
88 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("a", "b", "c")));
89 | assertEquals("b", list.remove(1));
90 | assertEquals(Arrays.asList("a", "c"), new ArrayList<>(list));
91 |
92 | // remove(Object) with null must not throw and should return false
93 | assertDoesNotThrow(() -> {
94 | boolean removed = list.remove((Object) null);
95 | assertFalse(removed);
96 | });
97 |
98 | // remove existing object
99 | assertTrue(list.remove("a"));
100 | assertEquals(Collections.singletonList("c"), new ArrayList<>(list));
101 | }
102 |
103 | @Test
104 | void clear_removeAll_retainAll_delegateCorrectly() {
105 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("a", "b", "c", "d")));
106 | assertTrue(list.removeAll(Arrays.asList("b", "x"))); // removes b
107 | assertEquals(Arrays.asList("a", "c", "d"), new ArrayList<>(list));
108 |
109 | assertTrue(list.retainAll(new HashSet<>(Arrays.asList("a", "d")))); // keeps a,d
110 | assertEquals(Arrays.asList("a", "d"), new ArrayList<>(list));
111 |
112 | list.clear();
113 | assertTrue(list.isEmpty());
114 | }
115 |
116 | @Test
117 | void toArray_variants_matchDelegate() {
118 | NonNullElementWrapperList list = wrap(new ArrayList<>(Arrays.asList("a", "b")));
119 | Object[] arr = list.toArray();
120 | assertArrayEquals(new Object[]{"a", "b"}, arr);
121 |
122 | String[] into = new String[0];
123 | String[] arr2 = list.toArray(into);
124 | assertArrayEquals(new String[]{"a", "b"}, arr2);
125 | }
126 |
127 | @Test
128 | void equals_and_hashCode_delegateBehavior_and_symmetry() {
129 | ArrayList base = new ArrayList<>(Arrays.asList("a", "b"));
130 | NonNullElementWrapperList w1 = wrap(new ArrayList<>(base));
131 | NonNullElementWrapperList w2 = wrap(new ArrayList<>(base));
132 | ArrayList otherList = new ArrayList<>(base);
133 |
134 | // equals compares by elements via delegate.equals(o)
135 | assertEquals(w1, w2); // wrapper vs wrapper
136 | assertEquals(w1, otherList); // wrapper vs plain List
137 | assertEquals(otherList, w1); // symmetry from the other side
138 |
139 | assertEquals(otherList.hashCode(), w1.hashCode());
140 | // change elements -> not equal
141 | w2.add(2, "c");
142 | assertNotEquals(w1, w2);
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | # GitHub Actions Workflow is created for testing and preparing the library release in the following steps:
2 | # - Validate Gradle Wrapper.
3 | # - Run 'test' task.
4 | # - Run Qodana inspections.
5 | # - Run the 'buildLibrary' task and prepare artifact for further tests.
6 | # - Create a draft release.
7 | #
8 | # The workflow is triggered on push and pull_request events.
9 | #
10 | # GitHub Actions reference: https://help.github.com/en/actions
11 |
12 | name: Build
13 | on:
14 | # Trigger the workflow on pushes to only the 'main' branch (this avoids duplicate checks being run e.g., for dependabot pull requests)
15 | push:
16 | branches: [ main ]
17 | # Trigger the workflow on any pull request
18 | pull_request:
19 |
20 | concurrency:
21 | group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
22 | cancel-in-progress: true
23 |
24 | jobs:
25 |
26 | # Prepare the environment and build the library
27 | build:
28 | name: Build
29 | runs-on: ubuntu-latest
30 | steps:
31 |
32 | # Check out the current repository
33 | - name: Fetch Sources
34 | uses: actions/checkout@v5
35 |
36 | # Set up the Java environment for the next steps
37 | - name: Setup Java
38 | uses: actions/setup-java@v5
39 | with:
40 | distribution: adopt
41 | java-version: 11
42 |
43 | # Setup Gradle
44 | - name: Setup Gradle
45 | uses: gradle/actions/setup-gradle@v4
46 |
47 | # Build the library
48 | - name: Build Library
49 | run: ./gradlew build
50 |
51 | # Store an already-built library as an artifact for downloading
52 | - name: Upload artifact
53 | uses: actions/upload-artifact@v4
54 | with:
55 | path: ./build/libs/*
56 |
57 | # Run tests and upload a code coverage report
58 | test:
59 | name: Test
60 | needs: [ build ]
61 | runs-on: ubuntu-latest
62 | steps:
63 |
64 | # Check out the current repository
65 | - name: Fetch Sources
66 | uses: actions/checkout@v5
67 |
68 | # Set up the Java environment for the next steps
69 | - name: Setup Java
70 | uses: actions/setup-java@v5
71 | with:
72 | distribution: adopt
73 | java-version: 11
74 |
75 | # Setup Gradle
76 | - name: Setup Gradle
77 | uses: gradle/actions/setup-gradle@v4
78 | with:
79 | cache-read-only: true
80 |
81 | # Run tests
82 | - name: Run Tests
83 | run: ./gradlew check
84 |
85 | # Collect Tests Result of failed tests
86 | - name: Collect Tests Result
87 | if: ${{ failure() }}
88 | uses: actions/upload-artifact@v4
89 | with:
90 | name: tests-result
91 | path: ${{ github.workspace }}/build/reports/tests
92 |
93 | # Upload the jacoco report to CodeCov
94 | - name: Upload Code Coverage Report
95 | uses: codecov/codecov-action@v5
96 | with:
97 | files: ${{ github.workspace }}/build/reports/jacoco/jacocoTestReport.xml
98 | token: ${{ secrets.CODECOV_TOKEN }}
99 |
100 | # Upload the test results to CodeCov
101 | - name: Upload test results to Codecov
102 | if: ${{ !cancelled() }}
103 | uses: codecov/test-results-action@v1
104 | with:
105 | token: ${{ secrets.CODECOV_TOKEN }}
106 |
107 | # Run Qodana inspections and provide a report
108 | inspectCode:
109 | name: Inspect code
110 | needs: [ build ]
111 | runs-on: ubuntu-latest
112 | permissions:
113 | contents: write
114 | checks: write
115 | pull-requests: write
116 | steps:
117 |
118 | # Check out the current repository
119 | - name: Fetch Sources
120 | uses: actions/checkout@v5
121 | with:
122 | ref: ${{ github.event.pull_request.head.sha }} # to check out the actual pull request commit, not the merge commit
123 | fetch-depth: 0 # a full history is required for pull request analysis
124 |
125 | # Run Qodana inspections
126 | - name: 'Qodana Scan'
127 | uses: JetBrains/qodana-action@v2025.2
128 | with:
129 | pr-mode: false
130 | env:
131 | QODANA_TOKEN: ${{ secrets.QODANA_TOKEN_2073039781 }}
132 | QODANA_ENDPOINT: 'https://qodana.cloud'
133 |
134 | # Prepare a draft release for GitHub Releases page for the manual verification
135 | # If accepted and published, the release workflow would be triggered
136 | releaseDraft:
137 | name: Release draft
138 | if: github.event_name != 'pull_request'
139 | needs: [ build, test, inspectCode ]
140 | runs-on: ubuntu-latest
141 | permissions:
142 | contents: write
143 | steps:
144 |
145 | # Check out the current repository
146 | - name: Fetch Sources
147 | uses: actions/checkout@v5
148 |
149 | # Remove old release drafts by using the curl request for the available releases with a draft flag
150 | - name: Remove Old Release Drafts
151 | env:
152 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
153 | run: |
154 | gh api repos/{owner}/{repo}/releases \
155 | --jq '.[] | select(.draft == true) | .id' \
156 | | xargs -I '{}' gh api -X DELETE repos/{owner}/{repo}/releases/{}
157 |
158 | # Create a new release draft which is not publicly visible and requires manual acceptance
159 | - name: Create Release Draft
160 | env:
161 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
162 | run: |
163 | VERSION=v$(./gradlew properties --property version --quiet --console=plain | tail -n 1 | cut -f2- -d ' ')
164 | RELEASE_NOTE="./build/tmp/release_note.txt"
165 | ./gradlew getChangelog --unreleased --no-header --quiet --console=plain --output-file=$RELEASE_NOTE
166 |
167 | gh release create $VERSION \
168 | --draft \
169 | --title $VERSION \
170 | --notes-file $RELEASE_NOTE
171 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/Json5ElementTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2022 - 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import static org.junit.jupiter.api.Assertions.*;
22 |
23 | /**
24 | * @author Marcel Haßlinger
25 | */
26 | public class Json5ElementTest {
27 | @Test
28 | void getAsJson5Array_throws_on_non_object() {
29 | var ex = assertThrows(IllegalStateException.class, () -> Json5Primitive.fromString("anyString").getAsJson5Object());
30 | assertEquals("Not a Json5Object: \"anyString\"", ex.getMessage());
31 | }
32 |
33 | @Test
34 | void getAsJson5Object_throws_on_non_array() {
35 | var ex = assertThrows(IllegalStateException.class, () -> Json5Primitive.fromString("anyString").getAsJson5Array());
36 | assertEquals("Not a Json5Array: \"anyString\"", ex.getMessage());
37 | }
38 |
39 | @Test
40 | void getAsJson5Primitive_throws_on_non_primitive() {
41 | var ex = assertThrows(IllegalStateException.class, () -> new Json5Object().getAsJson5Primitive());
42 | assertEquals("Not a Json5Primitive: {\n}", ex.getMessage());
43 | }
44 |
45 | @Test
46 | void getAsJson5Null_throws_on_null() {
47 | var ex = assertThrows(IllegalStateException.class, () -> Json5Primitive.fromString("anyString").getAsJson5Null());
48 | assertEquals("Not a Json5Null: \"anyString\"", ex.getMessage());
49 | }
50 |
51 | @Test
52 | void getAsBoolean_throws_on_non_boolean() {
53 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsBoolean());
54 | assertEquals("Json5Object", ex.getMessage());
55 | }
56 |
57 | @Test
58 | void getAsInstant_throws_on_non_boolean() {
59 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsInstant());
60 | assertEquals("Json5Object", ex.getMessage());
61 | }
62 |
63 | @Test
64 | void getAsNumber_throws_on_non_boolean() {
65 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsNumber());
66 | assertEquals("Json5Object", ex.getMessage());
67 | }
68 |
69 | @Test
70 | void getAsRadixNumber_throws_on_non_boolean() {
71 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsRadixNumber());
72 | assertEquals("Json5Object", ex.getMessage());
73 | }
74 |
75 | @Test
76 | void getAsString_throws_on_non_boolean() {
77 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsString());
78 | assertEquals("Json5Object", ex.getMessage());
79 | }
80 |
81 | @Test
82 | void getAsDouble_throws_on_non_boolean() {
83 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsDouble());
84 | assertEquals("Json5Object", ex.getMessage());
85 | }
86 |
87 | @Test
88 | void getAsFloat_throws_on_non_boolean() {
89 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsFloat());
90 | assertEquals("Json5Object", ex.getMessage());
91 | }
92 |
93 | @Test
94 | void getAsLong_throws_on_non_boolean() {
95 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsLong());
96 | assertEquals("Json5Object", ex.getMessage());
97 | }
98 |
99 | @Test
100 | void getAsInt_throws_on_non_boolean() {
101 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsInt());
102 | assertEquals("Json5Object", ex.getMessage());
103 | }
104 |
105 | @Test
106 | void getAsByte_throws_on_non_boolean() {
107 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsByte());
108 | assertEquals("Json5Object", ex.getMessage());
109 | }
110 |
111 | @Test
112 | void getAsBigDecimal_throws_on_non_boolean() {
113 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsBigDecimal());
114 | assertEquals("Json5Object", ex.getMessage());
115 | }
116 |
117 | @Test
118 | void getAsBigInteger_throws_on_non_boolean() {
119 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsBigInteger());
120 | assertEquals("Json5Object", ex.getMessage());
121 | }
122 |
123 | @Test
124 | void getAsShort_throws_on_non_boolean() {
125 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsShort());
126 | assertEquals("Json5Object", ex.getMessage());
127 | }
128 |
129 | @Test
130 | void getAsBinaryString_throws_on_non_boolean() {
131 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsBinaryString());
132 | assertEquals("Json5Object", ex.getMessage());
133 | }
134 |
135 | @Test
136 | void getAsOctalString_throws_on_non_boolean() {
137 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsOctalString());
138 | assertEquals("Json5Object", ex.getMessage());
139 | }
140 |
141 | @Test
142 | void getAsHexString_throws_on_non_boolean() {
143 | var ex = assertThrows(UnsupportedOperationException.class, () -> new Json5Object().getAsHexString());
144 | assertEquals("Json5Object", ex.getMessage());
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/internal/EcmaScriptIdentifierTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5.internal;
18 |
19 | import org.junit.jupiter.api.DisplayName;
20 | import org.junit.jupiter.api.Test;
21 |
22 | import static org.junit.jupiter.api.Assertions.*;
23 |
24 | /**
25 | * @author Marcel Haßlinger
26 | */
27 | class EcmaScriptIdentifierTest {
28 |
29 | @Test
30 | @DisplayName("ASCII: valid identifiers")
31 | void asciiValid() {
32 | assertTrue(EcmaScriptIdentifier.isValid("foo"));
33 | assertTrue(EcmaScriptIdentifier.isValid("_bar"));
34 | assertTrue(EcmaScriptIdentifier.isValid("$baz"));
35 | assertTrue(EcmaScriptIdentifier.isValid("a1"));
36 | assertTrue(EcmaScriptIdentifier.isValid("A9_$_x"));
37 | }
38 |
39 | @Test
40 | @DisplayName("ASCII: invalid forms")
41 | void asciiInvalid() {
42 | assertFalse(EcmaScriptIdentifier.isValid("")); // empty identifier
43 | assertFalse(EcmaScriptIdentifier.isValid("1abc")); // starts with digit
44 | assertFalse(EcmaScriptIdentifier.isValid("with space")); // space
45 | assertFalse(EcmaScriptIdentifier.isValid("some-key")); // hyphen
46 | assertFalse(EcmaScriptIdentifier.isValid("foo.bar")); // dot
47 | assertFalse(EcmaScriptIdentifier.isValid("a,b")); // comma
48 | assertFalse(EcmaScriptIdentifier.isValid("😀face")); // symbol
49 | }
50 |
51 | @Test
52 | @DisplayName("Reserved words are allowed")
53 | void reservedWordsAllowed() {
54 | assertTrue(EcmaScriptIdentifier.isValid("class"));
55 | assertTrue(EcmaScriptIdentifier.isValid("default"));
56 | assertTrue(EcmaScriptIdentifier.isValid("function"));
57 | assertTrue(EcmaScriptIdentifier.isValid("if"));
58 | assertTrue(EcmaScriptIdentifier.isValid("true"));
59 | assertTrue(EcmaScriptIdentifier.isValid("false"));
60 | assertTrue(EcmaScriptIdentifier.isValid("null"));
61 | assertTrue(EcmaScriptIdentifier.isValid("NaN"));
62 | assertTrue(EcmaScriptIdentifier.isValid("Infinity"));
63 | }
64 |
65 | @Test
66 | @DisplayName("Unicode letters as start are allowed")
67 | void unicodeLettersStart() {
68 | assertTrue(EcmaScriptIdentifier.isValid("café")); // Lo
69 | assertTrue(EcmaScriptIdentifier.isValid("naïve")); // Lo + combining
70 | assertTrue(EcmaScriptIdentifier.isValid("äpfel")); // Ll
71 | assertTrue(EcmaScriptIdentifier.isValid("Русский")); // Cyrillic
72 | assertTrue(EcmaScriptIdentifier.isValid("你好")); // CJK
73 | assertTrue(EcmaScriptIdentifier.isValid("ʰello")); // Lm (U+02B0)
74 | assertTrue(EcmaScriptIdentifier.isValid("Ⅻwert")); // Nl (U+216B)
75 | }
76 |
77 | @Test
78 | @DisplayName("Digit at start is invalid (even Unicode Nd)")
79 | void unicodeDigitStartInvalid() {
80 | assertFalse(EcmaScriptIdentifier.isValid("١abc")); // U+0661 ARABIC-INDIC DIGIT ONE (Nd) at start
81 | }
82 |
83 | @Test
84 | @DisplayName("IdentifierPart categories")
85 | void identifierPartCategories() {
86 | // digits (Nd) in part
87 | assertTrue(EcmaScriptIdentifier.isValid("foo1"));
88 | assertTrue(EcmaScriptIdentifier.isValid("a\u0661")); // arabic-indic digit one in part
89 |
90 | // combining marks (Mn/Mc) in part
91 | assertTrue(EcmaScriptIdentifier.isValid("e\u0301")); // 'e' + COMBINING ACUTE ACCENT (Mn)
92 | assertFalse(EcmaScriptIdentifier.isValid("\u0301e")); // mark at start -> invalid
93 |
94 | // connector punctuation (Pc) in part
95 | assertTrue(EcmaScriptIdentifier.isValid("a_b")); // U+005F LOW LINE
96 | assertTrue(EcmaScriptIdentifier.isValid("a\u203Fbc")); // U+203F UNDERTIE (Pc)
97 | assertTrue(EcmaScriptIdentifier.isValid("a\u2054bc")); // U+2054 INVERTED UNDERTIE (Pc)
98 |
99 | // Pc at start (not '_') is not allowed
100 | assertFalse(EcmaScriptIdentifier.isValid("\u203Fabc"));
101 | }
102 |
103 | @Test
104 | @DisplayName("ZWNJ/ZWJ allowed in part, not at start")
105 | void zwnjZwjRules() {
106 | assertTrue(EcmaScriptIdentifier.isValid("a\u200Cbc")); // ZWNJ
107 | assertTrue(EcmaScriptIdentifier.isValid("a\u200Dbc")); // ZWJ
108 | assertFalse(EcmaScriptIdentifier.isValid("\u200Cabc"));
109 | assertFalse(EcmaScriptIdentifier.isValid("\u200Dabc"));
110 | }
111 |
112 | @Test
113 | @DisplayName("Other Cf (format) not allowed")
114 | void otherCfNotAllowed() {
115 | assertFalse(EcmaScriptIdentifier.isValid("a\u00ADb")); // SOFT HYPHEN (Cf)
116 | }
117 |
118 | @Test
119 | @DisplayName("\\uXXXX escapes: valid")
120 | void unicodeEscapesValid() {
121 | assertTrue(EcmaScriptIdentifier.isValid("\\u0061bc")); // 'a'bc
122 | assertTrue(EcmaScriptIdentifier.isValid("\\u00E4pfel")); // 'ä'pfel
123 | assertTrue(EcmaScriptIdentifier.isValid("a\\u0301")); // combining accent in part
124 | assertTrue(EcmaScriptIdentifier.isValid("a\\u200C")); // ZWNJ in part
125 | assertTrue(EcmaScriptIdentifier.isValid("a\\u200D")); // ZWJ in part
126 | assertTrue(EcmaScriptIdentifier.isValid("a\\u005Fb")); // '_' in part
127 | assertTrue(EcmaScriptIdentifier.isValid("\\u0041\\u0030x")); // 'A''0'x
128 | // valid surrogate pair literal (astral letter) as UTF-16, not via \\u{...}
129 | assertTrue(EcmaScriptIdentifier.isValid("\uD801\uDC00abc")); // U+10400
130 | }
131 |
132 | @Test
133 | @DisplayName("\\uXXXX escapes: invalid or disallowed")
134 | void unicodeEscapesInvalid() {
135 | assertFalse(EcmaScriptIdentifier.isValid("\\u00G1")); // not hex
136 | assertFalse(EcmaScriptIdentifier.isValid("\\u12")); // too short
137 | assertFalse(EcmaScriptIdentifier.isValid("a\\x61")); // \x not allowed
138 | assertFalse(EcmaScriptIdentifier.isValid("abc\\")); // bare backslash
139 | assertFalse(EcmaScriptIdentifier.isValid("\\u200Cabc")); // ZWNJ at start
140 | assertFalse(EcmaScriptIdentifier.isValid("\\uD800abc")); // lone high surrogate
141 | }
142 |
143 | @Test
144 | @DisplayName("$ behaves like in JS")
145 | void dollarRules() {
146 | assertTrue(EcmaScriptIdentifier.isValid("$"));
147 | assertTrue(EcmaScriptIdentifier.isValid("$x"));
148 | assertTrue(EcmaScriptIdentifier.isValid("a$1"));
149 | assertTrue(EcmaScriptIdentifier.isValid("ä$"));
150 | }
151 |
152 | @Test
153 | @DisplayName("Null/empty")
154 | void nullAndEmpty() {
155 | assertFalse(EcmaScriptIdentifier.isValid(null));
156 | assertFalse(EcmaScriptIdentifier.isValid(""));
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # json5-java
2 |
3 | [](https://github.com/marhali/json5-java/actions)
4 | [](https://github.com/marhali/json5-java/releases)
5 | [](https://javadoc.io/doc/de.marhali/json5-java)
6 | [](https://codecov.io/gh/marhali/json5-java)
7 | [](https://paypal.me/marhalide)
8 |
9 | This is a reference implementation of the [JSON5 standard](https://json5.org/) in Java 11+,
10 | capable of parsing and serialization of JSON5 data.
11 |
12 | This library is an enhanced version of [Synt4xErr0r4 / json5](https://github.com/Synt4xErr0r4/json5),
13 | which provides a better full-fledged API inspired by Google's [Gson](https://github.com/google/gson) library.
14 |
15 | ## Features
16 |
17 | - Fully supports JSON5 according to the [specification](https://spec.json5.org/)
18 | - Extensive API for interacting with elements, inspired by Google's [Gson](https://github.com/google/gson) library
19 | - Supports comment parsing and writing (if they can be associated with an [Json5Element](src/main/java/de/marhali/json5/Json5Element.java))
20 | - Fine-grained [configuration options](#configuration-options)
21 | - No runtime dependencies – ensures a clean supply chain
22 |
23 | ## Installation
24 |
25 | Download the [latest release](https://github.com/marhali/json5-java/releases/latest) manually or add it as a Maven dependency.
26 | Don't worry the project is already in the [Maven Central Repository](https://central.sonatype.com/artifact/de.marhali/json5-java). See the configuration below for your favorite build system.
27 |
28 | ### Add via Maven
29 |
30 | ```xml
31 |
32 |
33 | de.marhali
34 | json5-java
35 | 3.0.0
36 |
37 |
38 | ```
39 |
40 | ### Add via Gradle
41 |
42 | ```kotlin
43 | repositories {
44 | mavenCentral()
45 | }
46 |
47 | dependencies {
48 | implementation("de.marhali:json5-java:3.0.0")
49 | }
50 | ```
51 |
52 | ## Usage
53 |
54 | This library can be used by either configuring a [Json5](src/main/java/de/marhali/json5/Json5.java)
55 | instance or by using the underlying [Json5Parser](src/main/java/de/marhali/json5/stream/Json5Parser.java)
56 | and [Json5Writer](src/main/java/de/marhali/json5/stream/Json5Writer.java).
57 |
58 | The following section describes how to use this library with the
59 | [Json5](src/main/java/de/marhali/json5/Json5.java) core class.
60 |
61 | ### Configure Json5 instance
62 |
63 | See [Configuration Options](#configuration-options) for a full overview of possible options.
64 |
65 | ```java
66 | import de.marhali.json5.config.Json5Options;
67 | import de.marhali.json5.Json5;
68 |
69 | // Create Json5 instance using builder pattern to configure desired options
70 | Json5 json5 = Json5.builder(builder -> builder
71 | .quoteless()
72 | .quoteSingle()
73 | .parseComments()
74 | .writeComments()
75 | .prettyPrinting()
76 | .build()
77 | );
78 |
79 | // Using configuration object
80 | Json5Options options = Json5Options.builder()
81 | // ...
82 | .build();
83 | Json5 json5 = new Json5(options);
84 | ```
85 |
86 | ### Parsing
87 |
88 | During parsing, a JSON5 file or string is converted into the corresponding [Json5Element's](src/main/java/de/marhali/json5/Json5Element.java).
89 |
90 | ```java
91 | import de.marhali.json5.Json5;
92 | import de.marhali.json5.Json5Element;
93 |
94 | Json5 json5 = ...
95 |
96 | // Parse from a String
97 | Json5Element element =
98 | json5.parse("{ 'key': 'value', 'array': ['first val','second val'] }");
99 |
100 | // ...
101 |
102 | // Parse from a Reader or InputStream
103 | try (InputStream stream = ...) {
104 | Json5Element element = json5.parse(stream);
105 | // ...
106 | } catch (IOException e) {
107 | // ...
108 | }
109 | ```
110 |
111 | ### Serialization
112 |
113 | During serialization, [Json5Element's](src/main/java/de/marhali/json5/Json5Element.java) are converted to their string representation so that they can be written to a file, for example.
114 |
115 | ```java
116 | import de.marhali.json5.Json5;
117 | import de.marhali.json5.Json5Element;
118 |
119 | Json5Element element = ...
120 |
121 | // Serialize to a String literal
122 | String jsonString = json5.serialize(element);
123 |
124 | // ...
125 |
126 | // Serialize to a Writer or OutputStream
127 | try (OutputStream stream = ...) {
128 | json5.serialize(element, stream);
129 | // ...
130 | } catch (IOException e) {
131 | // ...
132 | }
133 | ```
134 |
135 | ## Documentation
136 | Detailed javadoc documentation can be found at [javadoc.io](https://javadoc.io/doc/de.marhali/json5-java).
137 |
138 | ### API
139 |
140 | This library provides a few core classes to interact with JSON5 elements.
141 |
142 | - [Json5](src/main/java/de/marhali/json5/Json5.java): Core class for parsing and serialization
143 | - [Json5Options](src/main/java/de/marhali/json5/config/Json5Options.java): Library configuration and options builder
144 | - [Json5Element](src/main/java/de/marhali/json5/Json5Element.java): Root class for every JSON5 element
145 | - [Json5Null](src/main/java/de/marhali/json5/Json5Null.java): Class representing the JSON5 `null` value
146 | - [Json5Object](src/main/java/de/marhali/json5/Json5Object.java): Represents a JSON5 object
147 | - [Json5Array](src/main/java/de/marhali/json5/Json5Array.java): Represents a JSON5 array
148 | - [Json5Primitive](src/main/java/de/marhali/json5/Json5Primitive.java): Holds any primitive value (`Boolean`, `Number` or `String`)
149 |
150 | > For a better understanding of how to use the API, take a look at the [unit tests](src/test/java/de/marhali/json5).
151 |
152 | ### Configuration Options
153 | This library supports a few customizations to adjust the behaviour of parsing and serialization.
154 | For a detailed explanation see the [Json5Options](src/main/java/de/marhali/json5/config/Json5Options.java) class.
155 |
156 | - stringifyUnixInstants
157 | - stringifyAscii
158 | - allowNaN
159 | - allowInfinity
160 | - allowInvalidSurrogates
161 | - quoteSingle
162 | - quoteless
163 | - allowBinaryLiterals
164 | - allowOctalLiterals
165 | - allowHexFloatingLiterals
166 | - allowLongUnicodeEscapes
167 | - allowTrailingData
168 | - parseComments
169 | - writeComments
170 | - trailingComma
171 | - insertFinalNewline
172 | - digitSeparatorStrategy
173 | - duplicateBehaviour
174 | - indentFactor
175 |
176 | > To get started using this library, `Json5Options.DEFAULT` may be a good starting point, as these are the recommended options.
177 |
178 | ## License
179 | This library is released under the [Apache 2.0 license](LICENSE).
180 |
181 | Partial parts of the project are based on [Gson](https://github.com/google/gson) and [Synt4xErr0r4 / json5](https://github.com/Synt4xErr0r4/json5). The affected classes contain the respective license notice.
182 |
183 | ## Contact
184 |
185 | Marcel Haßlinger - [@marhali_de](https://twitter.com/marhali_de) - [Portfolio Website](https://marhali.de)
186 |
187 | Project Link: [https://github.com/marhali/json5-java](https://github.com/marhali/json5-java)
188 |
189 | ## Donation
190 |
191 | If this library helps you to reduce development time, you can give me a [cup of coffee](https://paypal.me/marhalide) :)
192 |
--------------------------------------------------------------------------------
/src/main/java/de/marhali/json5/Json5.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2022 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5;
18 |
19 | import de.marhali.json5.config.Json5Options;
20 | import de.marhali.json5.stream.Json5Lexer;
21 | import de.marhali.json5.stream.Json5Parser;
22 | import de.marhali.json5.stream.Json5Writer;
23 |
24 | import java.io.*;
25 | import java.util.Objects;
26 | import java.util.function.Function;
27 |
28 | /**
29 | * This is the main class for using Json5. This class provides methods to parse and
30 | * serialize Json5 data according to the specification and the configured {@link Json5Options options}.
31 | *
32 | *
33 | * You can create a Json5 instance by invoking {@link #Json5(Json5Options)}
34 | * or by using {@link #builder(Function)}.
35 | *
36 | *
37 | *
38 | * This class contains several utility methods to parse and serialize json5 data by passing
39 | * {@link Reader}, {@link Writer} or simple {@link String} instances.
40 | *
79 | *
80 | * @author Marcel Haßlinger
81 | * @see Json5 Specification
82 | * @see Json5Parser
83 | * @see Json5Writer
84 | */
85 | public final class Json5 {
86 |
87 | /**
88 | * Constructs a new json5 instance by using the {@link Json5Options#builder()}.
89 | *
90 | * @param builder Options builder
91 | * @return Built options
92 | */
93 | public static Json5 builder(Function builder) {
94 | return new Json5(builder.apply(Json5Options.builder()));
95 | }
96 |
97 | private final Json5Options options;
98 |
99 | /**
100 | * Constructs a new json5 instance with custom configuration for parsing and serialization.
101 | *
102 | * @param options Configuration options
103 | * @see #builder(Function)
104 | */
105 | public Json5(Json5Options options) {
106 | this.options = Objects.requireNonNull(options);
107 | }
108 |
109 | /**
110 | * Constructs a json5 instance by using {@link Json5Options#DEFAULT} as configuration.
111 | *
112 | * @see #Json5(Json5Options)
113 | */
114 | public Json5() {
115 | this(Json5Options.DEFAULT);
116 | }
117 |
118 | /**
119 | * Parses the data from the {@link InputStream} into a tree of {@link Json5Element}'s.
120 | *
Note: The stream must be closed after operation
121 | *
122 | * @param in Can be any applicable {@link InputStream}
123 | * @return Parsed json5 tree. Can be {@code null} if the provided stream does not contain any data
124 | * @see #parse(Reader)
125 | */
126 | public Json5Element parse(InputStream in) {
127 | Objects.requireNonNull(in);
128 | return parse(new InputStreamReader(in));
129 | }
130 |
131 | /**
132 | * Parses the provided read-stream into a tree of {@link Json5Element}'s.
133 | *
Note: The reader must be closed after operation
134 | *
135 | * @param reader Can be any applicable {@link Reader}
136 | * @return Parsed json5 tree. Can be {@code null} if the provided stream does not contain any data
137 | * @see Json5Parser#parse(Json5Lexer)
138 | */
139 | public Json5Element parse(Reader reader) {
140 | Objects.requireNonNull(reader);
141 |
142 | Json5Lexer lexer = new Json5Lexer(reader, this.options);
143 | return Json5Parser.parse(lexer);
144 | }
145 |
146 | /**
147 | * Parses the provided json5-encoded {@link String} into a parse tree of {@link Json5Element}'s.
148 | *
149 | * @param string Json5 encoded {@link String}
150 | * @return Parsed json5 tree. Can be {@code null} if the provided {@link String} is empty
151 | * @see #parse(Reader)
152 | */
153 | public Json5Element parse(String string) {
154 | Objects.requireNonNull(string);
155 |
156 | StringReader reader = new StringReader(string);
157 | Json5Element element = this.parse(reader);
158 | reader.close();
159 | return element;
160 | }
161 |
162 | /**
163 | * Encodes the provided element into its character literal representation by using an output-stream.
164 | *
Note: The stream must be closed after operation ({@link OutputStream#close()})!
165 | *
166 | * @param element {@link Json5Element} to serialize
167 | * @param out Can be any applicable {@link OutputStream}
168 | * @throws IOException If an I/O error occurs
169 | * @see #serialize(Json5Element, Writer)
170 | */
171 | public void serialize(Json5Element element, OutputStream out) throws IOException {
172 | Objects.requireNonNull(element);
173 | Objects.requireNonNull(out);
174 |
175 | serialize(element, new OutputStreamWriter(out));
176 | }
177 |
178 | /**
179 | * Encodes the provided element into its character literal representation by using a write-stream.
180 | *
Note: The writer must be closed after operation ({@link Writer#close()})!
{@code Json5Object} does not implement the {@link Map} interface, but a {@code Map} view of it
35 | * can be obtained with {@link #asMap()}.
36 | *
37 | *
See the {@link Json5} documentation for details on how to convert {@code Json5Object} and
38 | * generally any {@code Json5Element} from and to Json5.
39 | *
40 | * @author Inderjeet Singh
41 | * @author Joel Leitch
42 | * @author Marcel Haßlinger
43 | */
44 | public final class Json5Object extends Json5Element {
45 | private final LinkedTreeMap members = new LinkedTreeMap<>(false);
46 |
47 | /**
48 | * Creates a new instance of a {@link Json5Object}.
49 | */
50 | public Json5Object() {
51 | }
52 |
53 | /**
54 | * Creates a deep copy of this element and all its children.
55 | */
56 | @Override
57 | public Json5Object deepCopy() {
58 | Json5Object result = new Json5Object();
59 | for (Map.Entry entry : members.entrySet()) {
60 | result.add(entry.getKey(), entry.getValue().deepCopy());
61 | }
62 | result.setComment(comment);
63 | return result;
64 | }
65 |
66 | /**
67 | * Adds a member, which is a name-value pair, to self. The name must be a String, but the value
68 | * can be an arbitrary {@link Json5Element}, thereby allowing you to build a full tree of
69 | * {@link Json5Element Json5Elements} rooted at this node.
70 | *
71 | * @param property name of the member.
72 | * @param value the member object.
73 | */
74 | public void add(String property, Json5Element value) {
75 | members.put(property, value == null ? Json5Primitive.fromNull() : value);
76 | }
77 |
78 | /**
79 | * Removes the {@code property} from this object.
80 | *
81 | * @param property name of the member that should be removed.
82 | * @return the {@link Json5Element} object that is being removed, or {@code null} if no member with
83 | * this name exists.
84 | */
85 | public Json5Element remove(String property) {
86 | return members.remove(property);
87 | }
88 |
89 | /**
90 | * Convenience method to add a char member. The specified value is converted to a {@link
91 | * Json5Primitive} of Character.
92 | *
93 | * @param property name of the member.
94 | * @param value the char value associated with the member.
95 | */
96 | public void addProperty(String property, Character value) {
97 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromCharacter(value));
98 | }
99 |
100 | /**
101 | * Convenience method to add a string member. The specified value is converted to a {@link
102 | * Json5Primitive} of String.
103 | *
104 | * @param property name of the member.
105 | * @param value the string value associated with the member.
106 | */
107 | public void addProperty(String property, String value) {
108 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromString(value));
109 | }
110 |
111 | /**
112 | * Convenience method to add a number member. The specified value is converted to a {@link
113 | * Json5Primitive} of Number.
114 | *
115 | * @param property name of the member.
116 | * @param value the number value associated with the member.
117 | */
118 | public void addProperty(String property, Number value) {
119 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromNumber(value));
120 | }
121 |
122 | public void addProperty(String property, Number value, int radix) {
123 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromNumber(value, radix));
124 | }
125 |
126 | /**
127 | * Convenience method to add a {@link Instant} member. The specified value is converted to a {@link
128 | * Json5Primitive} of {@link Instant}.
129 | *
130 | * @param property name of the member.
131 | * @param value the {@link Instant} value associated with the member.
132 | */
133 | public void addProperty(String property, Instant value) {
134 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromInstant(value));
135 | }
136 |
137 | /**
138 | * Convenience method to add a boolean member. The specified value is converted to a {@link
139 | * Json5Primitive} of Boolean.
140 | *
141 | * @param property name of the member.
142 | * @param value the boolean value associated with the member.
143 | */
144 | public void addProperty(String property, Boolean value) {
145 | add(property, value == null ? Json5Primitive.fromNull() : Json5Primitive.fromBoolean(value));
146 | }
147 |
148 | /**
149 | * Returns a set of members of this object. The set is ordered, and the order is in which the
150 | * elements were added.
151 | *
152 | * @return a set of members of this object.
153 | */
154 | public Set> entrySet() {
155 | return members.entrySet();
156 | }
157 |
158 | /**
159 | * Returns a set of members key values.
160 | *
161 | * @return a set of member keys as Strings
162 | */
163 | public Set keySet() {
164 | return members.keySet();
165 | }
166 |
167 | /**
168 | * Returns the number of key/value pairs in the object.
169 | *
170 | * @return the number of key/value pairs in the object.
171 | */
172 | public int size() {
173 | return members.size();
174 | }
175 |
176 | /**
177 | * Returns true if the number of key/value pairs in the object is zero.
178 | *
179 | * @return true if the number of key/value pairs in the object is zero.
180 | */
181 | public boolean isEmpty() {
182 | return members.isEmpty();
183 | }
184 |
185 | /**
186 | * Convenience method to check if a member with the specified name is present in this object.
187 | *
188 | * @param memberName name of the member that is being checked for presence.
189 | * @return true if there is a member with the specified name, false otherwise.
190 | */
191 | public boolean has(String memberName) {
192 | return members.containsKey(memberName);
193 | }
194 |
195 | /**
196 | * Returns the member with the specified name.
197 | *
198 | * @param memberName name of the member that is being requested.
199 | * @return the member matching the name, or {@code null} if no such member exists.
200 | */
201 | public Json5Element get(String memberName) {
202 | return members.get(memberName);
203 | }
204 |
205 | /**
206 | * Convenience method to get the specified member as a {@link Json5Primitive}.
207 | *
208 | * @param memberName name of the member being requested.
209 | * @return the {@code Json5Primitive} corresponding to the specified member, or {@code null} if no
210 | * member with this name exists.
211 | * @throws ClassCastException if the member is not of type {@code Json5Primitive}.
212 | */
213 | public Json5Primitive getAsJson5Primitive(String memberName) {
214 | return (Json5Primitive) members.get(memberName);
215 | }
216 |
217 | /**
218 | * Convenience method to get the specified member as a {@link Json5Array}.
219 | *
220 | * @param memberName name of the member being requested.
221 | * @return the {@code Json5Array} corresponding to the specified member, or {@code null} if no
222 | * member with this name exists.
223 | * @throws ClassCastException if the member is not of type {@code Json5Array}.
224 | */
225 | public Json5Array getAsJson5Array(String memberName) {
226 | return (Json5Array) members.get(memberName);
227 | }
228 |
229 | /**
230 | * Convenience method to get the specified member as a {@link Json5Object}.
231 | *
232 | * @param memberName name of the member being requested.
233 | * @return the {@code Json5Object} corresponding to the specified member, or {@code null} if no
234 | * member with this name exists.
235 | * @throws ClassCastException if the member is not of type {@code Json5Object}.
236 | */
237 | public Json5Object getAsJson5Object(String memberName) {
238 | return (Json5Object) members.get(memberName);
239 | }
240 |
241 | /**
242 | * Returns a mutable {@link Map} view of this {@code Json5Object}. Changes to the {@code Map} are
243 | * visible in this {@code Json5Object} and the other way around.
244 | *
245 | *
The {@code Map} does not permit {@code null} keys or values. Unlike {@code Json5Object}'s
246 | * {@code null} handling, a {@link NullPointerException} is thrown when trying to add {@code
247 | * null}. Use {@link Json5Null} for Json5 null values.
248 | *
249 | * @return mutable {@code Map} view
250 | */
251 | public Map asMap() {
252 | // It is safe to expose the underlying map because it disallows null keys and values
253 | return members;
254 | }
255 |
256 | @Override
257 | public boolean equals(Object o) {
258 | if (o == null || getClass() != o.getClass()) return false;
259 | if (!super.equals(o)) return false;
260 | Json5Object that = (Json5Object) o;
261 | return Objects.equals(members, that.members);
262 | }
263 |
264 | @Override
265 | public int hashCode() {
266 | return Objects.hash(super.hashCode(), members);
267 | }
268 | }
269 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
--------------------------------------------------------------------------------
/src/test/java/de/marhali/json5/Json5ArrayTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2022 - 2025 Marcel Haßlinger
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package de.marhali.json5;
18 |
19 | import org.junit.jupiter.api.Nested;
20 | import org.junit.jupiter.api.Test;
21 |
22 | import java.math.BigDecimal;
23 | import java.math.BigInteger;
24 | import java.time.Instant;
25 | import java.util.Iterator;
26 | import java.util.List;
27 |
28 | import static org.junit.jupiter.api.Assertions.*;
29 |
30 | /**
31 | * @author Marcel Haßlinger
32 | */
33 | public class Json5ArrayTest {
34 | @Test
35 | void constructors_and_capacity_validation() {
36 | assertDoesNotThrow(() -> new Json5Array());
37 | assertDoesNotThrow(() -> new Json5Array(4));
38 | assertThrows(IllegalArgumentException.class, () -> new Json5Array(-1));
39 | }
40 |
41 | @Nested
42 | class AddOverloadsAndNullConversions {
43 |
44 | @Test
45 | void add_Instant_boolean_char_number_string_element_and_nulls() {
46 | Json5Array arr = new Json5Array();
47 |
48 | arr.add(Instant.EPOCH);
49 | arr.add(true);
50 | arr.add('X');
51 | arr.add(123);
52 | arr.add("hi");
53 |
54 | // nulls -> Json5Null
55 | arr.add((Instant) null);
56 | arr.add((Boolean) null);
57 | arr.add((Character) null);
58 | arr.add((Number) null);
59 | arr.add((String) null);
60 | arr.add((Json5Element) null); // generic add null
61 |
62 | assertEquals(11, arr.size());
63 | assertEquals(Json5Primitive.fromInstant(Instant.EPOCH), arr.get(0));
64 | assertEquals(Json5Primitive.fromBoolean(true), arr.get(1));
65 | assertEquals(Json5Primitive.fromCharacter('X'), arr.get(2));
66 | assertEquals(Json5Primitive.fromNumber(123), arr.get(3));
67 | assertEquals(Json5Primitive.fromString("hi"), arr.get(4));
68 |
69 | assertEquals(Json5Primitive.fromNull(), arr.get(5));
70 | assertEquals(Json5Primitive.fromNull(), arr.get(6));
71 | assertEquals(Json5Primitive.fromNull(), arr.get(7));
72 | assertEquals(Json5Primitive.fromNull(), arr.get(8));
73 | assertEquals(Json5Primitive.fromNull(), arr.get(9));
74 | }
75 |
76 | @Test
77 | void add_number_with_radix() {
78 | Json5Array arr = new Json5Array();
79 | arr.add(255, 16);
80 | assertEquals(Json5Primitive.fromNumber(255, 16), arr.get(0));
81 | }
82 |
83 | @Test
84 | void addAll_appends_in_order() {
85 | Json5Array a = new Json5Array();
86 | a.add(1);
87 | a.add(2);
88 |
89 | Json5Array b = new Json5Array();
90 | b.add(3);
91 | b.add(4);
92 |
93 | a.addAll(b);
94 | assertEquals(4, a.size());
95 | assertEquals(Json5Primitive.fromNumber(1), a.get(0));
96 | assertEquals(Json5Primitive.fromNumber(2), a.get(1));
97 | assertEquals(Json5Primitive.fromNumber(3), a.get(2));
98 | assertEquals(Json5Primitive.fromNumber(4), a.get(3));
99 | }
100 | }
101 |
102 | @Nested
103 | class SetRemoveContainsAndGet {
104 |
105 | @Test
106 | void set_replaces_and_returns_previous_converts_null() {
107 | Json5Array arr = new Json5Array();
108 | arr.add("a");
109 | arr.add("b");
110 |
111 | Json5Element previous = arr.set(1, Json5Primitive.fromNumber(7));
112 | assertEquals(Json5Primitive.fromString("b"), previous);
113 | assertEquals(Json5Primitive.fromNumber(7), arr.get(1));
114 |
115 | // set null -> Json5Null
116 | previous = arr.set(0, null);
117 | assertEquals(Json5Primitive.fromString("a"), previous);
118 | assertEquals(Json5Primitive.fromNull(), arr.get(0));
119 | }
120 |
121 | @Test
122 | void remove_by_element_and_index_and_contains() {
123 | Json5Array arr = new Json5Array();
124 | Json5Element one = Json5Primitive.fromNumber(1);
125 | arr.add(one);
126 | arr.add(one.deepCopy()); // same value, other instance
127 | arr.add("x");
128 |
129 | assertTrue(arr.contains(Json5Primitive.fromNumber(1)));
130 | assertEquals(3, arr.size());
131 |
132 | // remove(element) remove first occurrence
133 | boolean removed = arr.remove(Json5Primitive.fromNumber(1));
134 | assertTrue(removed);
135 | assertEquals(2, arr.size());
136 | assertEquals(Json5Primitive.fromNumber(1), arr.get(0)); // second element stays
137 | assertEquals(Json5Primitive.fromString("x"), arr.get(1));
138 |
139 | // remove(index)
140 | Json5Element rem = arr.remove(0);
141 | assertEquals(Json5Primitive.fromNumber(1), rem);
142 | assertEquals(1, arr.size());
143 | assertEquals(Json5Primitive.fromString("x"), arr.get(0));
144 |
145 | // remove not existing
146 | assertFalse(arr.remove(Json5Primitive.fromBoolean(true)));
147 | }
148 |
149 | @Test
150 | void get_and_bounds() {
151 | Json5Array arr = new Json5Array();
152 | arr.add("a");
153 | assertEquals(Json5Primitive.fromString("a"), arr.get(0));
154 | assertThrows(IndexOutOfBoundsException.class, () -> arr.get(-1));
155 | assertThrows(IndexOutOfBoundsException.class, () -> arr.get(1));
156 | assertThrows(IndexOutOfBoundsException.class, () -> arr.remove(1));
157 | assertThrows(IndexOutOfBoundsException.class, () -> arr.set(1, Json5Primitive.fromNull()));
158 | }
159 |
160 | @Test
161 | void size_and_isEmpty_and_iterator_order() {
162 | Json5Array arr = new Json5Array();
163 | assertTrue(arr.isEmpty());
164 | assertEquals(0, arr.size());
165 |
166 | arr.add(10);
167 | arr.add(20);
168 | arr.add(30);
169 |
170 | assertFalse(arr.isEmpty());
171 | assertEquals(3, arr.size());
172 |
173 | Iterator it = arr.iterator();
174 | assertTrue(it.hasNext());
175 | assertEquals(Json5Primitive.fromNumber(10), it.next());
176 | assertEquals(Json5Primitive.fromNumber(20), it.next());
177 | assertEquals(Json5Primitive.fromNumber(30), it.next());
178 | assertFalse(it.hasNext());
179 | }
180 | }
181 |
182 | @Nested
183 | class SingleElementGetters {
184 |
185 | @Test
186 | void getters_throw_if_not_singleton() {
187 | Json5Array empty = new Json5Array();
188 | Json5Array multi = new Json5Array();
189 | multi.add(1);
190 | multi.add(2);
191 |
192 | assertAll(
193 | () -> assertThrows(IllegalStateException.class, empty::getAsBoolean),
194 | () -> assertThrows(IllegalStateException.class, empty::getAsString),
195 | () -> assertThrows(IllegalStateException.class, multi::getAsNumber),
196 | () -> assertThrows(IllegalStateException.class, multi::getAsJson5Null)
197 | );
198 | }
199 |
200 | @Test
201 | void getters_delegate_when_singleton_primitive_number() {
202 | Json5Primitive prim = Json5Primitive.fromNumber(42);
203 | Json5Array arr = new Json5Array();
204 | arr.add(prim);
205 |
206 | assertEquals(42, arr.getAsInt());
207 | assertEquals(42L, arr.getAsLong());
208 | assertEquals(42.0, arr.getAsDouble());
209 | assertEquals((short) 42, arr.getAsShort());
210 | assertEquals((byte) 42, arr.getAsByte());
211 | assertEquals(42.0f, arr.getAsFloat());
212 | assertEquals(new BigInteger("42"), arr.getAsBigInteger());
213 | assertEquals(new BigDecimal("42"), arr.getAsBigDecimal());
214 | assertEquals("42", arr.getAsString());
215 | assertFalse(arr.getAsBoolean());
216 | }
217 |
218 | @Test
219 | void getters_delegate_radix_and_null_and_boolean() {
220 | Json5Array radix = new Json5Array();
221 | radix.add(255, 16);
222 | assertEquals(radix.get(0).getAsHexString(), radix.getAsHexString());
223 | assertEquals(radix.get(0).getAsOctalString(), radix.getAsOctalString());
224 | assertEquals(radix.get(0).getAsBinaryString(), radix.getAsBinaryString());
225 | assertEquals(radix.get(0).getAsRadixNumber().toString(),
226 | radix.getAsRadixNumber().toString());
227 |
228 | Json5Array nul = new Json5Array();
229 | nul.add((String) null);
230 | assertEquals(Json5Primitive.fromNull(), nul.getAsJson5Null());
231 |
232 | Json5Array bool = new Json5Array();
233 | bool.add(true);
234 | assertTrue(bool.getAsBoolean());
235 | }
236 | }
237 |
238 | @Nested
239 | class ListView {
240 |
241 | @Test
242 | void asList_is_mutable_and_bidirectional_and_disallows_nulls() {
243 | Json5Array arr = new Json5Array();
244 | arr.add("a");
245 |
246 | List view = arr.asList();
247 |
248 | view.add(Json5Primitive.fromNumber(7));
249 | assertEquals(2, arr.size());
250 | assertEquals(Json5Primitive.fromNumber(7), arr.get(1));
251 |
252 | arr.add(false);
253 | assertEquals(Json5Primitive.fromBoolean(false), view.get(2));
254 |
255 | assertThrows(NullPointerException.class, () -> view.add(null));
256 | }
257 | }
258 |
259 | @Nested
260 | class DeepCopyEqualsHashCode {
261 |
262 | @Test
263 | void deepCopy_is_deep_and_copies_comment() {
264 | Json5Array original = new Json5Array();
265 | original.add(1);
266 | Json5Array inner = new Json5Array();
267 | inner.add("x");
268 | original.add(inner);
269 | original.setComment("note!");
270 |
271 | Json5Array copy = original.deepCopy();
272 |
273 | assertNotSame(original, copy);
274 | assertNotSame(original.get(1), copy.get(1)); // inner array deep-copied
275 | assertEquals(original, copy);
276 | assertEquals(original.hashCode(), copy.hashCode());
277 | assertEquals("note!", copy.getComment());
278 |
279 | // independent mutation
280 | ((Json5Array) copy.get(1)).set(0, Json5Primitive.fromString("changed"));
281 | assertNotEquals(original, copy);
282 | }
283 |
284 | @Test
285 | void equals_and_hashCode_contract() {
286 | Json5Array a = new Json5Array();
287 | a.add(1);
288 | a.add("x");
289 |
290 | Json5Array b = new Json5Array();
291 | b.add(1);
292 | b.add("x");
293 |
294 | Json5Array c = new Json5Array();
295 | c.add(1);
296 | c.add("x");
297 |
298 | assertEquals(a, a); // reflexive
299 | assertEquals(a, b); // symmetric
300 | assertEquals(b, a);
301 | assertEquals(b, c); // transitive
302 | assertEquals(a, c);
303 |
304 | assertEquals(a.hashCode(), b.hashCode());
305 | assertEquals(a.hashCode(), c.hashCode());
306 |
307 | Json5Array d = new Json5Array();
308 | d.add(2);
309 | assertNotEquals(a, d);
310 | }
311 | }
312 | }
313 |
--------------------------------------------------------------------------------