Skip to content

Commit

Permalink
Json processing support now uses default encoding of JSON-P rather th…
Browse files Browse the repository at this point in the history
…an UTF-8 for reader.

Signed-off-by: Tomas Langer <[email protected]>
  • Loading branch information
tomas-langer committed Feb 21, 2019
1 parent 26c04bf commit 4d475bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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

0 comments on commit 4d475bf

Please sign in to comment.