Skip to content

Commit

Permalink
Add charset config
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglingsong committed May 17, 2018
1 parent 8049eae commit a96b4f8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
11 changes: 2 additions & 9 deletions jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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[]{"[email protected]"};
// boss.name = "bar";

Employee empl = new Employee();
empl.age = 30;
empl.emails = new String[]{"[email protected]"};
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 19 additions & 5 deletions jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
*/
public class SurfingConfiguration {

static Builder builder() {
public static Builder builder() {
Builder builder = new Builder();
builder.configuration = new SurfingConfiguration();
return builder;
}

public static class Binding {

public Binding(JsonPath jsonPath, JsonPathListener[] listeners) {
Binding(JsonPath jsonPath, JsonPathListener[] listeners) {
this.jsonPath = jsonPath;
this.listeners = listeners;
}
Expand All @@ -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;
}
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand All @@ -273,4 +283,8 @@ public Charset getParserCharset() {
return parserCharset;
}

public void setParserCharset(Charset parserCharset) {
this.parserCharset = parserCharset;
}

}

0 comments on commit a96b4f8

Please sign in to comment.