From a96b4f8dfd36bb27f6bd8194efa052c1464bd3cb Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 17 May 2018 15:30:07 +0800 Subject: [PATCH] Add charset config --- .../java/org/jsfr/json/JacksonParserTest.java | 11 ++------- .../org/jsfr/json/BenchmarkCollectObject.java | 6 ++--- .../json/BenchmarkCollectSingleValue.java | 8 +++---- .../jsfr/json/BenchmarkParseLargeJson.java | 7 +++--- ...nchmarkParseLargeJsonWithoutStreaming.java | 4 ++-- .../org/jsfr/json/BenchmarkParseLongText.java | 6 ++--- .../main/java/org/jsfr/json/JsonSurfer.java | 3 +++ .../org/jsfr/json/SurfingConfiguration.java | 24 +++++++++++++++---- 8 files changed, 38 insertions(+), 31 deletions(-) diff --git a/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java b/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java index 176fb36..0614c50 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java @@ -76,8 +76,8 @@ public void testNonBlockingParser() throws Exception { verify(mockListener).onValue(eq(provider.primitive("abcd")), any(ParsingContext.class)); } - @Ignore @Test + @Ignore public void testProtobufParser() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); @@ -90,21 +90,14 @@ public void testProtobufParser() throws Exception { + "}\n"; final ProtobufSchema schema = ProtobufSchemaLoader.std.parse(protobuf_str); -// Employee boss = new Employee(); -// boss.age = 30; -// boss.emails = new String[]{"bar@gmail.com"}; -// boss.name = "bar"; - Employee empl = new Employee(); empl.age = 30; empl.emails = new String[]{"foo@gmail.com"}; empl.name = "foo"; -// empl.boss = boss; byte[] protobufData = mapper.writer(schema) .writeValueAsBytes(empl); - - // TODO Jackson's bug + // TODO Jackson's bug to be fixed in 2.9.6 JsonSurfer protobufSurfer = new JsonSurfer(new JacksonParser(new ProtobufFactory(), schema), provider); SurfingConfiguration config = protobufSurfer.configBuilder().bind("$.name", mockListener).build(); protobufSurfer.surf(new ByteArrayInputStream(protobufData), config); diff --git a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectObject.java b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectObject.java index de37970..808c704 100644 --- a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectObject.java +++ b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectObject.java @@ -39,7 +39,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -72,8 +72,8 @@ public void onTypedValue(Object value, ParsingContext context) { blackhole.consume(value); } }; - surfingConfiguration = SurfingConfiguration.builder().bind("$.store.book[*]", Map.class, collectOneListener).build(); - json = Resources.toString(Resources.getResource("sample.json"), Charset.forName("UTF-8")); + surfingConfiguration = SurfingConfiguration.builder().bind("$.store.book[*]", Map.class, collectOneListener).withCharset(StandardCharsets.UTF_8).build(); + json = Resources.toString(Resources.getResource("sample.json"), StandardCharsets.UTF_8); } diff --git a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectSingleValue.java b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectSingleValue.java index 8154060..cac4aaf 100644 --- a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectSingleValue.java +++ b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkCollectSingleValue.java @@ -46,11 +46,9 @@ import org.openjdk.jmh.runner.options.OptionsBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import profilers.FlightRecordingProfiler; import java.io.IOException; -import java.io.StringReader; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.concurrent.TimeUnit; @@ -82,8 +80,8 @@ public void setup() throws Exception { simpleSurfer = JsonSurferJsonSimple.INSTANCE; fastjsonSurfer = JsonSurferFastJson.INSTANCE; collectOneListener = new CollectOneListener(true); - surfingConfiguration = SurfingConfiguration.builder().bind("$.store.book[0].author", collectOneListener).build(); - json = Resources.toString(Resources.getResource("sample.json"), Charset.forName("UTF-8")); + surfingConfiguration = SurfingConfiguration.builder().bind("$.store.book[0].author", collectOneListener).withCharset(StandardCharsets.UTF_8).build(); + json = Resources.toString(Resources.getResource("sample.json"), StandardCharsets.UTF_8); } @Benchmark diff --git a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJson.java b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJson.java index 8022871..ff52009 100644 --- a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJson.java +++ b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJson.java @@ -41,10 +41,9 @@ import org.openjdk.jmh.runner.options.OptionsBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import profilers.FlightRecordingProfiler; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) @@ -79,8 +78,8 @@ public void onValue(Object value, ParsingContext context) { blackhole.consume(value); } }; - surfingConfiguration = SurfingConfiguration.builder().bind(jsonPath, blackHoleListener).skipOverlappedPath().build(); - json = Resources.toString(Resources.getResource("allthethings.json"), Charset.forName("UTF-8")); + surfingConfiguration = SurfingConfiguration.builder().bind(jsonPath, blackHoleListener).skipOverlappedPath().withCharset(StandardCharsets.UTF_8).build(); + json = Resources.toString(Resources.getResource("allthethings.json"), StandardCharsets.UTF_8); } @Benchmark diff --git a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJsonWithoutStreaming.java b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJsonWithoutStreaming.java index 66d22b2..5901d58 100644 --- a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJsonWithoutStreaming.java +++ b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLargeJsonWithoutStreaming.java @@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -70,7 +70,7 @@ public class BenchmarkParseLargeJsonWithoutStreaming { public void setup() throws IOException { gson = new GsonBuilder().create(); om = new ObjectMapper(); - json = Resources.toString(Resources.getResource("allthethings.json"), Charset.forName("UTF-8")); + json = Resources.toString(Resources.getResource("allthethings.json"), StandardCharsets.UTF_8); } @Benchmark diff --git a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLongText.java b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLongText.java index 9f7d95f..b5a9685 100644 --- a/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLongText.java +++ b/jsurfer-benchmark/src/main/java/org/jsfr/json/BenchmarkParseLongText.java @@ -44,7 +44,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -74,10 +74,10 @@ public void setup() throws Exception { simpleSurfer = JsonSurferJsonSimple.INSTANCE; fastjsonSurfer = JsonSurferFastJson.INSTANCE; collectOneListener = new CollectOneListener(true); - surfingConfiguration = SurfingConfiguration.builder().bind("$.findMe", collectOneListener).build(); + surfingConfiguration = SurfingConfiguration.builder().bind("$.findMe", collectOneListener).withCharset(StandardCharsets.UTF_8).build(); gson = new GsonBuilder().create(); om = new ObjectMapper(); - json = Resources.toString(Resources.getResource("longText.json"), Charset.forName("UTF-8")); + json = Resources.toString(Resources.getResource("longText.json"), StandardCharsets.UTF_8); } @Benchmark diff --git a/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java b/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java index 1211a57..d0e94c5 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java @@ -517,6 +517,9 @@ private void ensureSetting(SurfingConfiguration configuration) { if (configuration.getErrorHandlingStrategy() == null) { configuration.setErrorHandlingStrategy(errorHandlingStrategy); } + if (configuration.getParserCharset() == null) { + configuration.setParserCharset(parserCharset); + } } public Charset getParserCharset() { diff --git a/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java b/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java index 55272ad..d74176b 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java @@ -43,7 +43,7 @@ */ public class SurfingConfiguration { - static Builder builder() { + public static Builder builder() { Builder builder = new Builder(); builder.configuration = new SurfingConfiguration(); return builder; @@ -51,7 +51,7 @@ static Builder builder() { public static class Binding { - public Binding(JsonPath jsonPath, JsonPathListener[] listeners) { + Binding(JsonPath jsonPath, JsonPathListener[] listeners) { this.jsonPath = jsonPath; this.listeners = listeners; } @@ -65,7 +65,7 @@ public static class IndefinitePathBinding extends Binding { int minimumPathDepth; - public IndefinitePathBinding(JsonPath jsonPath, JsonPathListener[] listeners, int minimumPathDepth) { + IndefinitePathBinding(JsonPath jsonPath, JsonPathListener[] listeners, int minimumPathDepth) { super(jsonPath, listeners); this.minimumPathDepth = minimumPathDepth; } @@ -90,10 +90,20 @@ public SurfingConfiguration build() { configuration.definitePathLookup[entry.getKey() - configuration.minDepth] = entry.getValue().toArray(new Binding[entry.getValue().size()]); } } - configuration.parserCharset = jsonSurfer.getParserCharset(); return configuration; } + /** + * Associated with a Charset + * + * @param charset charset + * @return builder + */ + public Builder withCharset(Charset charset) { + configuration.parserCharset = charset; + return this; + } + /** * Associated with a JsonSurfer * @@ -257,7 +267,7 @@ void setErrorHandlingStrategy(ErrorHandlingStrategy errorHandlingStrategy) { this.errorHandlingStrategy = errorHandlingStrategy; } - public boolean withinRange(int currentDepth) { + private boolean withinRange(int currentDepth) { return minDepth <= currentDepth && currentDepth <= maxDepth; } @@ -273,4 +283,8 @@ public Charset getParserCharset() { return parserCharset; } + public void setParserCharset(Charset parserCharset) { + this.parserCharset = parserCharset; + } + }