Skip to content

Add JsonReader.unwrap() to access underlying JsonParser when using avaje-jsonb-jackson #179

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

Merged
merged 1 commit into from
Oct 29, 2023
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 @@ -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