Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json processing support now uses default encoding of JSON-P rather th… #421

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
Expand Down Expand Up @@ -90,7 +92,7 @@ private JsonProcessing(JsonReaderFactory readerFactory, JsonWriterFactory writer
* a {@link javax.json.JsonException}
*/
public Reader<JsonStructure> reader() {
return reader(Charset.defaultCharset());
return reader(null);
}

/**
Expand All @@ -107,7 +109,15 @@ public Reader<JsonStructure> reader() {
public Reader<JsonStructure> reader(Charset charset) {
return (publisher, clazz) -> ContentReaders.byteArrayReader()
.apply(publisher)
.thenApply(bytes -> jsonReaderFactory.createReader(new ByteArrayInputStream(bytes), charset).read());
.thenApply(bytes -> {
InputStream is = new ByteArrayInputStream(bytes);

JsonReader reader = (charset == null)
? jsonReaderFactory.createReader(is)
: jsonReaderFactory.createReader(is, charset);

return reader.read();
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -200,7 +199,7 @@ private Charset determineCharset(Parameters headers) {
return null; // Do not need default charset. Can use JSON specification.
}
})
.orElse(StandardCharsets.UTF_8);
.orElse(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ public void pingPong() throws Exception {
assertThat(json2, is(json));
}

@Test
public void pingPongNoCharset() throws Exception {
Routing routing = Routing.builder()
.register(JsonSupport.create())
.post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json)))
.build();
JsonObject json = createJson();
TestResponse response = TestClient.create(routing)
.path("/foo")
.post(MediaPublisher
.create(MediaType.APPLICATION_JSON, json.toString()));

assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.APPLICATION_JSON.toString()));
byte[] bytes = response.asBytes().toCompletableFuture().get(10, TimeUnit.SECONDS);
JsonObject json2 = Json.createReader(new ByteArrayInputStream(bytes)).readObject();
assertThat(json2, is(json));
}

@Test
public void invalidJson() throws Exception {
Routing routing = Routing.builder()
Expand Down