Skip to content

Commit

Permalink
Added javadoc. Cleanup. Modified README.md and addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
BidyadharM committed Dec 20, 2024
1 parent 268481d commit bef1dbc
Show file tree
Hide file tree
Showing 27 changed files with 1,575 additions and 813 deletions.
23 changes: 19 additions & 4 deletions ojdbc-provider-jackson-oson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ JDK versions. The coordinates for the latest release are:
</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.
The extension uses ojdbc8 as it's dependency. If the application environment has a different version of JDBC,
be sure to exclude ojdbc8 from the dependencies.
It can be done in maven as:

```xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-provider-jackson-oson</artifactId>
<version>1.0.2</version>
<exclusions>
<exclusion>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</exclusion>
</exclusions>
</dependency>
```

## Building the provider module
1. Clone the repository:
Expand All @@ -48,12 +63,12 @@ Usage Examples for Oracle Jackson OSON Provider Extensions can be found at [ojdb
- **[AccessJsonColumnUsingPOJOAndJsonProvider](../ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJOAndJsonProvider.java)**:
Demonstrates the usage of the Jackson OSON provider to serialize a Plain Old Java Object (POJO) to OSON
bytes in order to save it in a JSON column in the database and deserialize OSON bytes to
POJO during retrieval. In this case, the JDBC Thin Driver invokes the provider to serialize/deserialize.
a POJO during retrieval. In this case, the JDBC Thin Driver invokes the provider to serialize/deserialize.
- **[AccessJsonColumnUsingHibernate](../ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingHibernate.java)**:
Performs the same task as above using Hibernate.
- **[AccessJsonColumnUsingJacksonObjectNode](../ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingJacksonObjectNode.java)**:
Demonstrates the usage of the Jackson OSON provider to serialize Jackson's ObjectNode
to OSON bytes for insertion and vice-versa for retrieval.
to OSON bytes for insertion and retrieval.
- **[AccessJsonColumnUsingPOJO](../ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJO.java)**:
Demonstrates how to use the Jackson OSON provider APIs to Serialize/Deserialize POJO and use JDBC
to directly insert the OSON bytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class JacksonOsonConverter implements OsonConverter{
om.findAndRegisterModules();
om.registerModule(new OsonModule());
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// om.setAnnotationIntrospector(new AnnotationIntrospector());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,10 @@ public JsonParser createParser(OracleJsonParser oParser) {
* @param input DataInput to use for reading content to parse
* @param ctxt I/O context to use for parsing
*
* @return
* @throws IOException
* @return the JsonParser from DataInput
*/
@Override
protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOException {
protected JsonParser _createParser(DataInput input, IOContext ctxt) {

InputStream stream = new InputStream() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
public class OsonGenerator extends GeneratorBase {

private static final boolean DEBUG = false;
private Logger logger = Logger.getLogger("OsonLogger");
private Logger logger = Logger.getLogger(OsonGenerator.class.getName());

private OutputStream out = null;
private OracleJsonGenerator gen = null;
Expand Down Expand Up @@ -126,7 +126,7 @@ public boolean canWriteBinaryNatively() {
*/
@Override
public void flush() throws IOException {
if(DEBUG) logger.log(Level.FINEST, "flush");
logger.log(Level.FINEST, "flush");
gen.flush();
}

Expand All @@ -135,7 +135,7 @@ public void flush() throws IOException {
*/
@Override
protected void _releaseBuffers() {
if(DEBUG) logger.log(Level.FINEST, "_releaseBuffers");
logger.log(Level.FINEST, "_releaseBuffers");
if(out instanceof ByteArrayOutputStream)
((ByteArrayOutputStream) out).reset();
}
Expand Down Expand Up @@ -519,11 +519,18 @@ public boolean isClosed() {
*/
@Override
public void close() throws IOException {
if(DEBUG) logger.log(Level.FINEST, "close");
logger.log(Level.FINEST, "close");
gen.close();
closed = true;
}

/**
* Writes a `java.util.Date` object as an Oracle JSON `DATE`.
* If the input is a `java.sql.Date`, it is first converted to a `DATE` object.
*
* @param value the `Date` to write
* @throws IOException if an I/O error occurs during writing
*/
public void writeDate(Date value) throws IOException {
_verifyValueWrite("write date");

Expand All @@ -539,18 +546,32 @@ public void writeDate(Date value) throws IOException {
}
}

/**
* Writes a `LocalDate` object as an Oracle JSON `DATE`.
* Converts the `LocalDate` to a `DATE` object and serializes it.
*
* @param value the `LocalDate` to write
* @throws IOException if an I/O error occurs during writing
*/
public void writeLocalDate(LocalDate value) throws IOException {
_verifyValueWrite("write LocalDate");
DATE dd = null;

try {
dd = new DATE(value);
DATE dd = new DATE(value);
OracleJsonDate jsonDate = new OracleJsonDateImpl(dd.shareBytes());
gen.write(jsonDate);
} catch (SQLException e) {
throw new RuntimeException(e);
}
OracleJsonDate jsonDate = new OracleJsonDateImpl(dd.shareBytes());
gen.write(jsonDate);
}

/**
* Writes a `Timestamp` object as an Oracle JSON `TIMESTAMP`.
* Converts the `Timestamp` to a `TIMESTAMP` object and serializes it.
*
* @param value the `Timestamp` to write
* @throws IOException if an I/O error occurs during writing
*/
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 @@ -72,7 +72,16 @@
* <li>{@link Duration} - {@link OsonDurationDeserializer} and {@link OsonDurationSerializer}</li>
* <li>{@link BigInteger} - {@link OsonBigIntegerDeserializer} and {@link OsonBigIntegerSerializer}</li>
* <li>{@link Year} - {@link OsonYearDeserializer} and {@link OsonYearSerializer}</li>
* <li>{@code byte[]} - {@link OsonByteDeserializer} and {@link OsonByteSerializer}</li>
* <li>{@link byte[]} - {@link OsonByteDeserializer} and {@link OsonByteSerializer}</li>
* <li>{@link java.util.Date}- {@link OsonDateSerializer} and {@link OsonDateDeserializer} </li>
* <li>{@link java.sql.Date}- {@link OsonDateSerializer} and {@link OsonSqlDateDeserializer} </li>
* <li>{@link Timestamp}- {@link OsonDateSerializer} and {@link OsonTimeStampDeserializer} </li>
* <li>{@link LocalDate}- {@link OsonLocalDateSerializer} and {@link OsonLocalDateDeserializer} </li>
* <li>{@link Enum}- {@link OsonEnumSerializer}</li>
* <li>{@link jakarta.persistence.AttributeConverter} - {@link OsonConverterSerializer} and {@link OsonConverterDeserializer}</li>
* <li>{@code jakarta.persistence.AttributeConverter[]} - {@link OsonConverterArraySerializer} and {@link OsonConverterArrayDeserializer}</li>
* <li>{@link Boolean}- {@link OsonBooleanDeserializer}</li>
* <li>{@link UUID}- {@link OsonUUIDDeserializer}</li>
* </ul>
*
*/
Expand All @@ -81,22 +90,23 @@ public class OsonModule extends SimpleModule {
public static String groupId;
public static String artifactId;
public static Version VERSION;
public static final String PROPERTIES_FILE_PATH =
"/META-INF/maven/com.oracle.database.jdbc/ojdbc-provider-jackson-oson/pom.properties";

static {
instantiateProviderVersionInfo();
VERSION = VersionUtil.parseVersion(providerVersion, groupId, artifactId);
}

private static void instantiateProviderVersionInfo() {
String propertiesFilePath = "/META-INF/maven/com.oracle.database.jdbc/ojdbc-provider-jackson-oson/pom.properties";
Properties properties = new Properties();
try (InputStream inputStream = OsonModule.class.getResourceAsStream(propertiesFilePath)) {
try (InputStream inputStream = OsonModule.class.getResourceAsStream(PROPERTIES_FILE_PATH)) {
if (inputStream != null) {
properties.load(inputStream);

providerVersion = properties.getProperty("version");
groupId = properties.getProperty("artifactId");
artifactId = properties.getProperty("groupId");
groupId = properties.getProperty("groupId");
artifactId = properties.getProperty("artifactId");

} else {
// when the tests are run locally using IDE.
Expand Down Expand Up @@ -158,15 +168,15 @@ public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescri
while (properties.hasNext()) {
boolean serializerAssigned = false;
BeanPropertyWriter writer = (BeanPropertyWriter) properties.next();
if (writer.getMember().hasAnnotation(Convert.class)) {
if (writer.getMember() != null && 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);
} else {
JsonSerializer<Object> mySerializer = new OsonConverterSerialiser(converterClass);
JsonSerializer<Object> mySerializer = new OsonConverterSerializer(converterClass);
writer.assignSerializer(mySerializer);
}
}
Expand Down Expand Up @@ -194,7 +204,7 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, Bean
while (properties.hasNext()) {
SettableBeanProperty property = properties.next();
boolean deserializerAssigned = false;
if (property.getMember().hasAnnotation(Convert.class)) {
if (property.getMember() != null && property.getMember().hasAnnotation(Convert.class)) {
Convert annotation = property.getMember().getAnnotation(Convert.class);
Class<? extends jakarta.persistence.AttributeConverter> converterClass = annotation.converter();
deserializerAssigned = true;
Expand All @@ -209,7 +219,7 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, Bean
if (Util.implementsSerializable(property.getType().getInterfaces())
&& !Util.isJavaWrapperSerializable(property.getType())
&& !deserializerAssigned){
JsonDeserializer<Object> deser = OsosnSerializableDeserializer.INSTANCE;
JsonDeserializer<Object> deser = OsonSerializableDeserializer.INSTANCE;
((BeanDeserializer) deserializer).replaceProperty(property,property.withValueDeserializer(deser));
}
}
Expand Down
Loading

0 comments on commit bef1dbc

Please sign in to comment.