Skip to content

Commit 73fe2ff

Browse files
authored
Extend JsonC support into azure-core-serializer-* libraries and azure-core (#41811)
* Extend JsonC support into azure-core-serializer-* libraries and azure-core. * fix version * spotless fixes * unused using
1 parent 1f7916f commit 73fe2ff

File tree

9 files changed

+107
-13
lines changed

9 files changed

+107
-13
lines changed

sdk/core/azure-core-serializer-json-gson/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<dependency>
8585
<groupId>com.azure</groupId>
8686
<artifactId>azure-json</artifactId>
87-
<version>1.2.0</version> <!-- {x-version-update;com.azure:azure-json;dependency} -->
87+
<version>1.3.0</version> <!-- {x-version-update;unreleased_com.azure:azure-json;dependency} -->
8888
</dependency>
8989

9090
<dependency>

sdk/core/azure-core-serializer-json-gson/src/main/java/com/azure/core/serializer/json/gson/implementation/GsonJsonReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public GsonJsonReader(Reader reader, byte[] jsonBytes, String jsonString, boolea
5252

5353
private static com.google.gson.stream.JsonReader createGsonReader(Reader reader, JsonOptions options) {
5454
com.google.gson.stream.JsonReader gsonReader = new com.google.gson.stream.JsonReader(reader);
55-
gsonReader.setLenient(options == null || options.isNonNumericNumbersSupported());
55+
gsonReader.setLenient(options == null || options.isNonNumericNumbersSupported() || options.isJsoncSupported());
5656

5757
return gsonReader;
5858
}

sdk/core/azure-core-serializer-json-gson/src/test/java/com/azure/core/serializer/json/gson/implementation/GsonJsonReaderContractTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
import com.azure.json.JsonOptions;
77
import com.azure.json.JsonReader;
88
import com.azure.json.contract.JsonReaderContractTests;
9+
import com.google.gson.stream.MalformedJsonException;
910
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.Test;
1012

1113
import java.io.IOException;
1214

15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
1318
/**
1419
* Tests {@link GsonJsonReader} against the contract required by {@link JsonReader}.
1520
*/
1621
public class GsonJsonReaderContractTests extends JsonReaderContractTests {
1722
private JsonReader reader;
23+
String jsonWithComments = "{ // single line comment\n" + " \"single-line\": \"comment\",\n" + " /*\n"
24+
+ " multi-line comment\n" + " */\n" + " \"multi-line\": \"comment\"}";
1825

1926
@Override
2027
public JsonReader getJsonReader(String json) throws IOException {
@@ -28,4 +35,28 @@ public void afterEach() throws IOException {
2835
reader.close();
2936
}
3037
}
38+
39+
@Test
40+
public void readJsonc() throws IOException {
41+
try (JsonReader jsonReader
42+
= AzureJsonUtils.createReader(jsonWithComments, new JsonOptions().setJsoncSupported(true))) {
43+
jsonReader.nextToken();
44+
String outputJson = jsonReader.readChildren();
45+
assertNotNull(outputJson);
46+
}
47+
}
48+
49+
@Test
50+
public void readJsoncFails() throws IOException {
51+
assertThrows(MalformedJsonException.class, () -> {
52+
// need to disable non-numeric number support as Gson only has a single "lenient" concept that both
53+
// of these control.
54+
try (JsonReader jsonReader = AzureJsonUtils.createReader(jsonWithComments,
55+
new JsonOptions().setNonNumericNumbersSupported(false))) {
56+
jsonReader.nextToken();
57+
String outputJson = jsonReader.readChildren();
58+
assertNotNull(outputJson);
59+
}
60+
});
61+
}
3162
}

sdk/core/azure-core-serializer-json-jackson/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<dependency>
8383
<groupId>com.azure</groupId>
8484
<artifactId>azure-json</artifactId>
85-
<version>1.2.0</version> <!-- {x-version-update;com.azure:azure-json;dependency} -->
85+
<version>1.3.0</version> <!-- {x-version-update;unreleased_com.azure:azure-json;dependency} -->
8686
</dependency>
8787
<dependency>
8888
<groupId>com.azure</groupId>
@@ -126,7 +126,7 @@
126126
<dependency>
127127
<groupId>com.azure</groupId>
128128
<artifactId>azure-json</artifactId>
129-
<version>1.2.0</version> <!-- {x-version-update;com.azure:azure-json;dependency} -->
129+
<version>1.3.0</version> <!-- {x-version-update;unreleased_com.azure:azure-json;dependency} -->
130130
<type>test-jar</type>
131131
<scope>test</scope>
132132
</dependency>

sdk/core/azure-core-serializer-json-jackson/src/main/java/com/azure/core/serializer/json/jackson/implementation/AzureJsonUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ public static JsonReader createReader(Reader json, JsonOptions options) throws I
7979

8080
private static JsonParser configureParser(JsonParser parser, JsonOptions options) {
8181
boolean nonNumericSupported = options == null || options.isNonNumericNumbersSupported();
82+
boolean commentsSupported = options != null && options.isJsoncSupported();
8283

83-
return parser.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS.mappedFeature(), nonNumericSupported);
84+
return parser.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS.mappedFeature(), nonNumericSupported)
85+
.configure(JsonParser.Feature.ALLOW_COMMENTS, commentsSupported);
8486
}
8587

8688
/**

sdk/core/azure-core-serializer-json-jackson/src/test/java/com/azure/core/serializer/json/jackson/implementation/JacksonJsonReaderContractTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33

44
package com.azure.core.serializer.json.jackson.implementation;
55

6+
import com.azure.json.JsonOptions;
67
import com.azure.json.JsonReader;
78
import com.azure.json.contract.JsonReaderContractTests;
9+
import com.fasterxml.jackson.core.JsonParseException;
810
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.Test;
912

1013
import java.io.IOException;
1114

15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
1218
/**
1319
* Tests {@link JacksonJsonReader} against the contract required by {@link JsonReader}.
1420
*/
1521
public class JacksonJsonReaderContractTests extends JsonReaderContractTests {
1622
private JsonReader reader;
23+
String jsonWithComments = "{ // single line comment\n" + " \"single-line\": \"comment\",\n" + " /*\n"
24+
+ " multi-line comment\n" + " */\n" + " \"multi-line\": \"comment\"}";
1725

1826
@Override
1927
public JsonReader getJsonReader(String json) throws IOException {
@@ -27,4 +35,26 @@ public void afterEach() throws IOException {
2735
reader.close();
2836
}
2937
}
38+
39+
@Test
40+
public void readJsonc() throws IOException {
41+
42+
try (JsonReader jsonReader
43+
= AzureJsonUtils.createReader(jsonWithComments, new JsonOptions().setJsoncSupported(true))) {
44+
jsonReader.nextToken();
45+
String outputJson = jsonReader.readChildren();
46+
assertNotNull(outputJson);
47+
}
48+
}
49+
50+
@Test
51+
public void readJsoncFails() throws IOException {
52+
assertThrows(JsonParseException.class, () -> {
53+
try (JsonReader jsonReader = getJsonReader(jsonWithComments)) {
54+
jsonReader.nextToken();
55+
String outputJson = jsonReader.readChildren();
56+
assertNotNull(outputJson);
57+
}
58+
});
59+
}
3060
}

sdk/core/azure-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<dependency>
105105
<groupId>com.azure</groupId>
106106
<artifactId>azure-json</artifactId>
107-
<version>1.2.0</version> <!-- {x-version-update;com.azure:azure-json;dependency} -->
107+
<version>1.3.0</version> <!-- {x-version-update;unreleased_com.azure:azure-json;dependency} -->
108108
</dependency>
109109
<dependency>
110110
<groupId>com.azure</groupId>

sdk/core/azure-core/src/main/java/com/azure/core/implementation/jackson/AzureJsonUtils.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ static JsonReader createReader(String json, JsonOptions options) throws IOExcept
4747

4848
private static JsonParser configureParser(JsonParser parser, JsonOptions options) {
4949
boolean nonNumericSupported = options == null || options.isNonNumericNumbersSupported();
50-
51-
return parser.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS.mappedFeature(), nonNumericSupported);
50+
boolean jsoncSupported = options != null && options.isJsoncSupported();
51+
return parser.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS.mappedFeature(), nonNumericSupported)
52+
.configure(JsonParser.Feature.ALLOW_COMMENTS, jsoncSupported);
5253
}
5354

5455
/**
55-
* Creates an instance of {@link JacksonJsonReader}.
56-
*
57-
* @param parser The {@link JsonParser} parsing JSON.
58-
* @return A {@link JacksonJsonReader} wrapping the {@link JsonParser}.
59-
*/
56+
* Creates an instance of {@link JacksonJsonReader}.
57+
*
58+
* @param parser The {@link JsonParser} parsing JSON.
59+
* @return A {@link JacksonJsonReader} wrapping the {@link JsonParser}.
60+
*/
6061
static JsonReader createReader(JsonParser parser) {
6162
return new JacksonJsonReader(parser, null, null, false, null);
6263
}

sdk/core/azure-core/src/test/java/com/azure/core/implementation/jackson/JacksonJsonReaderContractTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33

44
package com.azure.core.implementation.jackson;
55

6+
import com.azure.json.JsonOptions;
67
import com.azure.json.JsonReader;
78
import com.azure.json.contract.JsonReaderContractTests;
9+
import com.fasterxml.jackson.core.JsonParseException;
810
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.Test;
912

1013
import java.io.IOException;
1114

15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
1218
/**
1319
* Tests {@link JacksonJsonReader} against the contract required by {@link JsonReader}.
1420
*/
1521
public class JacksonJsonReaderContractTests extends JsonReaderContractTests {
1622
private JsonReader reader;
23+
String jsonWithComments = "{ // single line comment\n" + " \"single-line\": \"comment\",\n" + " /*\n"
24+
+ " multi-line comment\n" + " */\n" + " \"multi-line\": \"comment\"}";
1725

1826
@Override
1927
public JsonReader getJsonReader(String json) throws IOException {
@@ -27,4 +35,26 @@ public void afterEach() throws IOException {
2735
reader.close();
2836
}
2937
}
38+
39+
@Test
40+
public void readJsonc() throws IOException {
41+
try (JsonReader jsonReader
42+
= AzureJsonUtils.createReader(jsonWithComments, new JsonOptions().setJsoncSupported(true))) {
43+
jsonReader.nextToken();
44+
String outputJson = jsonReader.readChildren();
45+
assertNotNull(outputJson);
46+
}
47+
}
48+
49+
@Test
50+
public void readJsoncFails() throws IOException {
51+
assertThrows(JsonParseException.class, () -> {
52+
try (JsonReader jsonReader = getJsonReader(jsonWithComments)) {
53+
jsonReader.nextToken();
54+
String outputJson = jsonReader.readChildren();
55+
assertNotNull(outputJson);
56+
}
57+
});
58+
}
59+
3060
}

0 commit comments

Comments
 (0)