Skip to content

Commit

Permalink
Support for missing Oracle JSON types serde (#398)
Browse files Browse the repository at this point in the history
* Added missing serde support for Oracle JSON

---------

Co-authored-by: Graeme Rocher <graeme.rocher@oracle.com>
  • Loading branch information
radovanradic and graemerocher authored Mar 16, 2023
1 parent 3f1282c commit 6a0f4d9
Show file tree
Hide file tree
Showing 22 changed files with 1,126 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public <T> byte[] writeValueAsBytes(Argument<T> type, T object) throws IOExcepti

@Override
public <T> T readValueFromTree(JsonNode tree, Argument<T> type) throws IOException {
final Deserializer<? extends T> deserializer = this.registry.findDeserializer(type).createSpecific(decoderContext, type);
final Deserializer<? extends T> deserializer = this.decoderContext.findDeserializer(type).createSpecific(decoderContext, type);
return deserializer.deserialize(JsonNodeDecoder.create(tree), decoderContext, type);
}

Expand All @@ -112,7 +112,7 @@ private <T> T readValue(ByteBuffer byteBuffer, Argument<T> type) throws IOExcept
}

private <T> T readValue(BsonReader bsonReader, Argument<T> type) throws IOException {
return registry.findDeserializer(type)
return decoderContext.findDeserializer(type)
.createSpecific(decoderContext, type)
.deserialize(new BsonReaderDecoder(bsonReader), decoderContext, type);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ private void serialize(Encoder encoder, Object object) throws IOException {
}

private void serialize(Encoder encoder, Object object, Argument type) throws IOException {
final Serializer<Object> serializer = registry.findSerializer(type).createSpecific(encoderContext, type);
final Serializer<Object> serializer = encoderContext.findSerializer(type).createSpecific(encoderContext, type);
serializer.serialize(encoder, encoderContext, type, object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private <T> void writeValue0(JsonGenerator gen, T value, Class<T> type) throws I

private <T> void writeValue(JsonGenerator gen, T value, Argument<T> argument) throws IOException {
gen.setCodec(objectCodecImpl);
Serializer<? super T> serializer = registry.findSerializer(argument)
Serializer<? super T> serializer = encoderContext.findSerializer(argument)
.createSpecific(encoderContext, argument);
final Encoder encoder = JacksonEncoder.create(gen);
serializer.serialize(
Expand All @@ -123,7 +123,7 @@ private <T> T readValue(JsonParser parser, Argument<T> type) throws IOException
@SuppressWarnings({"rawtypes", "unchecked"})
private <T> T readValue0(JsonParser parser, Argument<?> type) throws IOException {
parser.setCodec(objectCodecImpl);
Deserializer deserializer = registry.findDeserializer(type).createSpecific(decoderContext, (Argument) type);
Deserializer deserializer = decoderContext.findDeserializer(type).createSpecific(decoderContext, (Argument) type);
if (!parser.hasCurrentToken()) {
parser.nextToken();
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public JsonMapper cloneWithViewClass(@NonNull Class<?> viewClass) {
public void updateValueFromTree(Object value, JsonNode tree) throws IOException {
if (tree != null && value != null) {
Argument<Object> type = (Argument<Object>) Argument.of(value.getClass());
Deserializer deserializer = registry.findDeserializer(type).createSpecific(decoderContext, type);
Deserializer deserializer = decoderContext.findDeserializer(type).createSpecific(decoderContext, type);
if (deserializer instanceof UpdatingDeserializer) {

try (JsonParser parser = treeCodec.treeAsTokens(tree)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class Test {

}

void "test @JsonIgnore without @Inherited on interface method is not inherited"() {
void "test @JsonIgnore without @Inherited on interface method is inherited"() {
given:
def context = buildContext('test.Test', """
package test;
Expand Down Expand Up @@ -428,7 +428,7 @@ interface MyInterface {
""", [value:'test'])
expect:
writeJson(jsonMapper, beanUnderTest) == '{"value":"test","ignored":false}'
writeJson(jsonMapper, beanUnderTest) == '{"value":"test"}'

cleanup:
context.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.json.JsonMapper;
import io.micronaut.json.JsonStreamConfig;
import io.micronaut.json.tree.JsonNode;
import io.micronaut.serde.Decoder;
import io.micronaut.serde.Deserializer;
import io.micronaut.serde.Encoder;
import io.micronaut.serde.ObjectMapper;
Expand Down Expand Up @@ -72,7 +73,7 @@ public JsonMapper cloneWithViewClass(Class<?> viewClass) {
@Override
public <T> T readValueFromTree(JsonNode tree, Argument<T> type) throws IOException {
Deserializer.DecoderContext context = registry.newDecoderContext(view);
final Deserializer<? extends T> deserializer = this.registry.findDeserializer(type).createSpecific(context, type);
final Deserializer<? extends T> deserializer = context.findDeserializer(type).createSpecific(context, type);
return deserializer.deserialize(
JsonNodeDecoder.create(tree),
context,
Expand All @@ -95,10 +96,11 @@ public <T> T readValue(byte[] byteArray, Argument<T> type) throws IOException {
}

private <T> T readValue(JsonParser parser, Argument<T> type) throws IOException {
Decoder decoder = new JsonParserDecoder(parser);
Deserializer.DecoderContext context = registry.newDecoderContext(view);
final Deserializer<? extends T> deserializer = this.registry.findDeserializer(type).createSpecific(context, type);
final Deserializer<? extends T> deserializer = context.findDeserializer(type).createSpecific(context, type);
return deserializer.deserialize(
new JsonParserDecoder(parser),
decoder,
context,
type
);
Expand Down Expand Up @@ -166,7 +168,7 @@ private void serialize(Encoder encoder, Object object) throws IOException {

private void serialize(Encoder encoder, Object object, Argument type) throws IOException {
Serializer.EncoderContext context = registry.newEncoderContext(view);
final Serializer<Object> serializer = registry.findSerializer(type).createSpecific(context, type);
final Serializer<Object> serializer = context.findSerializer(type).createSpecific(context, type);
serializer.serialize(
encoder,
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

/**
* Implementation of the {@link Encoder} interface for Oracle JDBC JSON.
Expand All @@ -31,7 +33,7 @@
* @since 1.2.0
*/
@Internal
final class OracleJdbcJsonGeneratorEncoder implements Encoder {
public final class OracleJdbcJsonGeneratorEncoder implements Encoder {
private final OracleJsonGenerator jsonGenerator;
private final OracleJdbcJsonGeneratorEncoder parent;
private String currentKey;
Expand Down Expand Up @@ -170,4 +172,24 @@ public String currentPath() {
}
return builder.toString();
}

/**
* Encodes local date time.
*
* @param localDateTime the local date time
*/
public void encodeLocalDateTime(LocalDateTime localDateTime) {
jsonGenerator.write(localDateTime.toString());
postEncodeValue();
}

/**
* Encodes offset date time.
*
* @param offsetDateTime the offset date time
*/
public void encodeOffsetDateTime(OffsetDateTime offsetDateTime) {
jsonGenerator.write(offsetDateTime.toString());
postEncodeValue();
}
}
Loading

0 comments on commit 6a0f4d9

Please sign in to comment.