Skip to content

Commit

Permalink
Merge pull request #179 from avaje/feature/unwrap-parser
Browse files Browse the repository at this point in the history
Add JsonReader.unwrap() to access underlying JsonParser when using avaje-jsonb-jackson
  • Loading branch information
rbygrave committed Oct 29, 2023
2 parents e3e556a + 95b139e commit d7f9954
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
//import io.avaje.jsonb.jackson.JacksonIOAdapter;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ final class JacksonReader implements JsonReader {
this.failOnUnknown = failOnUnknown;
}

@Override
public <T> T unwrap(Class<T> type) {
return type.cast(parser);
}

@Override
public void close() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ public void pretty(boolean pretty) {
}
}

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> underlying) {
return (T) generator;
public <T> T unwrap(Class<T> type) {
return type.cast(generator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.avaje.jsonb.jackson;

import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
import io.avaje.jsonb.JsonReader;
import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class UnwrapJacksonParserTest {

Jsonb jsonb = Jsonb.builder().build();

@Test
void unwrap() {
try (JsonReader reader = jsonb.reader("{\"id\":42,\"name\":\"rob\"}")) {
JsonParser jsonParser = reader.unwrap(JsonParser.class);
JsonLocation location = jsonParser.currentLocation();

assertThat(location.toString()).isEqualTo("[Source: (String)\"{\"id\":42,\"name\":\"rob\"}\"; line: 1, column: 1]");

JsonType<Map<String, Object>> jsonMap = jsonb.type(Object.class).map();

Map<String, Object> map = jsonMap.fromJson(reader);
assertThat(map.get("id")).isEqualTo(42D);
assertThat(map.get("name")).isEqualTo("rob");
}
}
}
14 changes: 14 additions & 0 deletions jsonb/src/main/java/io/avaje/jsonb/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
*/
public interface JsonReader extends Closeable {

/**
* Unwrap and return the underlying JsonParser.
* <p>
* When using avaje-jsonb-jackson this will return the underlying Jackson JsonParser.
*
* <pre>{@code
*
* // when using avaje-jsonb-jackson
* var jacksonParser = jsonReader.unwrap(JsonParser.class);
*
* }</pre>
*/
<T> T unwrap(Class<T> type);

/**
* Read the beginning of an ARRAY or x-json-stream (new line delimited json content).
*/
Expand Down
2 changes: 1 addition & 1 deletion jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface JsonWriter extends Closeable, Flushable {
*
* }</pre>
*/
<T> T unwrap(Class<T> underlying);
<T> T unwrap(Class<T> type);

/**
* Set to serialise null values or not.
Expand Down
5 changes: 5 additions & 0 deletions jsonb/src/main/java/io/avaje/jsonb/core/ObjectJsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class ObjectJsonReader implements JsonReader {
this.currentValue = source;
}

@Override
public <T> T unwrap(Class<T> type) {
throw new UnsupportedOperationException();
}

@Override
public void unmappedField(String fieldName) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public DelegateJsonWriter(JsonWriter delegate) {
}

@Override
public <T> T unwrap(Class<T> underlying) {
return delegate.unwrap(underlying);
public <T> T unwrap(Class<T> type) {
return delegate.unwrap(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class JsonReadAdapter implements JsonReader {
this.failOnUnknown = failOnUnknown;
}

@Override
public <T> T unwrap(Class<T> type) {
return type.cast(reader);
}

@Override
public void beginStream() {
reader.startStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ final class JsonWriteAdapter implements JsonWriter {
this.serializeEmpty = serializeEmpty;
}

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> underlying) {
return (T) generator;
public <T> T unwrap(Class<T> type) {
return type.cast(generator);
}

@Override
Expand Down

0 comments on commit d7f9954

Please sign in to comment.