Skip to content

Commit

Permalink
Save LocalDate as Oson Date. Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
BidyadharM committed Dec 19, 2024
1 parent f2d524e commit 49323fe
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 24 deletions.
6 changes: 5 additions & 1 deletion ojdbc-provider-jackson-oson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ JDK versions. The coordinates for the latest release are:
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-provider-jackson-oson</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```
### Note
The extention uses ojdbc8 as it's dependency. If the application env. has other ojdbc jar
in the dependency, be sure to exclude the ojdbc8 jar from the extention.

## Building the provider module
1. Clone the repository:
```bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.*;
import java.util.Arrays;
Expand Down Expand Up @@ -538,6 +539,18 @@ public void writeDate(Date value) throws IOException {
}
}

public void writeLocalDate(LocalDate value) throws IOException {
_verifyValueWrite("write LocalDate");
DATE dd = null;
try {
dd = new DATE(value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
OracleJsonDate jsonDate = new OracleJsonDateImpl(dd.shareBytes());
gen.write(jsonDate);
}

public void writeTimeStamp(Timestamp value) throws IOException {
_verifyValueWrite("write TimeStamp");
TIMESTAMP timestamp = new TIMESTAMP(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public OsonModule() {
addDeserializer(UUID.class, OsonUUIDDeserializer.INSTANCE);
addDeserializer(Boolean.class, OsonBooleanDeserializer.INSTANCE);

addSerializer(LocalDate.class, OsonLocalDateSerializer.INSTANCE);
addDeserializer(LocalDate.class, OsonLocalDateDeserializer.INSTANCE);


}

Expand All @@ -153,14 +156,12 @@ public void setupModule(SetupContext context) {
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
Iterator<PropertyWriter> properties = serializer.properties();
while (properties.hasNext()) {
boolean serializerAssigned = false;
BeanPropertyWriter writer = (BeanPropertyWriter) properties.next();
if (Util.implementsSerializable(writer.getType().getInterfaces())
&& !Util.isJavaWrapperSerializable(writer)){
writer.assignSerializer(OsonSerializableSerializer.INSTANCE);
}
if (writer.getMember().hasAnnotation(Convert.class)) {
Convert annotation = writer.getMember().getAnnotation(Convert.class);
Class<? extends jakarta.persistence.AttributeConverter> converterClass = annotation.converter();
serializerAssigned = true;
if (writer.getType().isArrayType()) {
JsonSerializer<?> mySerializer = new OsonConverterArraySerializer(converterClass);
writer.assignSerializer((JsonSerializer<Object>) mySerializer);
Expand All @@ -169,6 +170,11 @@ public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescri
writer.assignSerializer(mySerializer);
}
}
if (Util.implementsSerializable(writer.getType().getInterfaces())
&& !Util.isJavaWrapperSerializable(writer)
&& !serializerAssigned){
writer.assignSerializer(OsonSerializableSerializer.INSTANCE);
}
}
return serializer;
}
Expand All @@ -187,15 +193,11 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, Bean
Iterator<SettableBeanProperty> properties = ((BeanDeserializer) deserializer).properties();
while (properties.hasNext()) {
SettableBeanProperty property = properties.next();
if (Util.implementsSerializable(property.getType().getInterfaces()) && !Util.isJavaWrapperSerializable(property.getType())){
JsonDeserializer<Object> deser = OsosnSerializableDeserializer.INSTANCE;
((BeanDeserializer) deserializer).replaceProperty(property,property.withValueDeserializer(deser));
}

boolean deserializerAssigned = false;
if (property.getMember().hasAnnotation(Convert.class)) {
Convert annotation = property.getMember().getAnnotation(Convert.class);
Class<? extends jakarta.persistence.AttributeConverter> converterClass = annotation.converter();

deserializerAssigned = true;
if(property.getType().isArrayType()){
JsonDeserializer<Object[]> deser = new OsonConverterArrayDeserializer(converterClass);
((BeanDeserializer) deserializer).replaceProperty(property,property.withValueDeserializer(deser));
Expand All @@ -204,6 +206,12 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, Bean
((BeanDeserializer) deserializer).replaceProperty(property,property.withValueDeserializer(deser));
}
}
if (Util.implementsSerializable(property.getType().getInterfaces())
&& !Util.isJavaWrapperSerializable(property.getType())
&& !deserializerAssigned){
JsonDeserializer<Object> deser = OsosnSerializableDeserializer.INSTANCE;
((BeanDeserializer) deserializer).replaceProperty(property,property.withValueDeserializer(deser));
}
}
return deserializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Util {
public static final ArrayList<String> ignoreList = new ArrayList<>();
static {
ignoreList.add("java.lang");
ignoreList.add("java.util");
ignoreList.add("java.sql");

}
public static boolean isJavaSerializableType(Class<?> clazz) {
String packageName = clazz.getPackage().getName();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package oracle.jdbc.provider.oson.deser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import oracle.jdbc.provider.oson.OsonParser;
import oracle.sql.json.OracleJsonParser;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class OsonLocalDateDeserializer extends LocalDateDeserializer {
public static final OsonLocalDateDeserializer INSTANCE = new OsonLocalDateDeserializer();
public OsonLocalDateDeserializer() {
super();
}
public OsonLocalDateDeserializer(DateTimeFormatter dateFormat) {
super(dateFormat);
}

public OsonLocalDateDeserializer(OsonLocalDateDeserializer base, DateTimeFormatter dtf) {
super(base, dtf);
}

public OsonLocalDateDeserializer(OsonLocalDateDeserializer base, Boolean leniency) {
super(base, leniency);
}

public OsonLocalDateDeserializer(OsonLocalDateDeserializer base, JsonFormat.Shape shape) {
super(base, shape);
}

@Override
protected OsonLocalDateDeserializer withDateFormat(DateTimeFormatter dtf) {
return new OsonLocalDateDeserializer(dtf);
}

@Override
protected OsonLocalDateDeserializer withShape(JsonFormat.Shape shape) {
return new OsonLocalDateDeserializer(this,shape);
}

@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if(_shape != null || _formatter!= DateTimeFormatter.ISO_LOCAL_DATE)
return super.deserialize(parser, context);

if (parser instanceof OsonParser) {
OsonParser _parser = (OsonParser) parser;

if(_parser.currentOsonEvent().equals(OracleJsonParser.Event.VALUE_DATE)) {
LocalDateTime dateTime = _parser.getLocalDateTime();
LocalDate localDate = dateTime.toLocalDate();
return localDate;
}
}
else {
return super.deserialize(parser, context);
}
return super.deserialize(parser, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.deser.std.UUIDDeserializer;
import oracle.jdbc.provider.oson.OsonParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package oracle.jdbc.provider.oson.ser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import oracle.jdbc.provider.oson.OsonGenerator;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class OsonLocalDateSerializer extends LocalDateSerializer {

public static final OsonLocalDateSerializer INSTANCE = new OsonLocalDateSerializer();

public OsonLocalDateSerializer() {
super();
}

public OsonLocalDateSerializer(OsonLocalDateSerializer base, Boolean useTimestamp,
DateTimeFormatter dtf, JsonFormat.Shape shape) {
super(base, useTimestamp, dtf, shape);
}

public OsonLocalDateSerializer(DateTimeFormatter formatter) {
super(formatter);
}

@Override
protected OsonLocalDateSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) {
return new OsonLocalDateSerializer(this, useTimestamp, dtf, shape);
}

@Override
public void serialize(LocalDate date, JsonGenerator g, SerializerProvider provider) throws IOException {
if(_formatter != null || _shape != null) {
super.serialize(date, g, provider);
return;
}
if (g instanceof OsonGenerator) {
OsonGenerator generator = (OsonGenerator) g;
generator.writeLocalDate(date);
return;
}else {
super.serialize(date, g, provider);
}
}
}

0 comments on commit 49323fe

Please sign in to comment.