Skip to content
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
@@ -1,12 +1,14 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.spring.json.jsonb;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import jakarta.json.bind.JsonbBuilder;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import oracle.sql.json.OracleJsonFactory;
Expand All @@ -16,6 +18,10 @@ public class JSONB {
private final OracleJsonFactory oracleJsonFactory;
private final YassonJsonb jsonb;

public static JSONB createDefault() {
return new JSONB(new OracleJsonFactory(), (YassonJsonb) JsonbBuilder.create());
}

public JSONB(OracleJsonFactory oracleJsonFactory, YassonJsonb jsonb) {
this.oracleJsonFactory = oracleJsonFactory;
this.jsonb = jsonb;
Expand All @@ -38,6 +44,12 @@ public JsonParser toJsonParser(Object o) {
return oracleJsonFactory.createJsonBinaryParser(buf).wrap(JsonParser.class);
}

public <T> T fromOSON(byte[] oson, Class<T> clazz) throws IOException {
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(oson)) {
return fromOSON(inputStream, clazz);
}
}

public <T> T fromOSON(JsonParser parser, Class<T> clazz) {
return jsonb.fromJson(parser, clazz);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.spring.json.jsonb;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

Expand All @@ -18,7 +19,7 @@

public class JSONBTest {

private JSONB jsonb = new JSONB(new OracleJsonFactory(), (YassonJsonb) JsonbBuilder.create());
private JSONB jsonb = JSONB.createDefault();
private Student s = new Student(Student.newId(), "Alice", new StudentDetails(
"Mathematics",
3.77,
Expand All @@ -36,8 +37,7 @@ void toOSON() {
void parserToObject() {
JsonParser parser = jsonb.toJsonParser(s);
Student student = jsonb.fromOSON(parser, Student.class);
assertThat(student).isNotNull();
assertThat(student).isEqualTo(s);
validateStudent(student);
}

@Test
Expand All @@ -46,16 +46,26 @@ void inputStreamToObject() {
InputStream is = new ByteArrayInputStream(oson);

Student student = jsonb.fromOSON(is, Student.class);
assertThat(student).isNotNull();
assertThat(student).isEqualTo(s);
validateStudent(student);
}

@Test
void byteBufferToOjbect() {
void byteBufferToObject() {
byte[] oson = jsonb.toOSON(s);
ByteBuffer buf = ByteBuffer.wrap(oson);

Student student = jsonb.fromOSON(buf, Student.class);
validateStudent(student);
}

@Test
void byteArrayToObject() throws IOException {
byte[] oson = jsonb.toOSON(s);
Student student = jsonb.fromOSON(oson, Student.class);
validateStudent(student);
}

void validateStudent(Student student) {
assertThat(student).isNotNull();
assertThat(student).isEqualTo(s);
}
Expand Down
Loading