Skip to content

Commit 07ac69d

Browse files
authored
Save java.util.Date as "OSON TimeStamp" to preserve Time part. (#146)
1 parent 77b6970 commit 07ac69d

File tree

5 files changed

+88
-53
lines changed

5 files changed

+88
-53
lines changed

ojdbc-provider-jackson-oson/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ in order to use the Oracle JDBC driver's JSON processing capabilities. It, there
1414
- **Java Types Handling**: Support for various complex and basic Java types during serialization and deserialization.
1515
- **Jackson Annotation support**: Support for Jackson Annotations. Note: When **@Format** annotation is used, the values are processed as Strings.
1616

17-
## Java type to Oson Mappings
17+
## Java type to OSON Mappings
1818
When the **OSON Provider for Jackson** is used the Java types are stored as their corresponding OSON types. The type
1919
mapping is given by the following table.
2020

21-
| **Java Type** | **Oson Type** |
21+
| **Java Type** | **OSON Type** |
2222
|--------------------------------------------|--------------------|
2323
| `LocalDateTime` | `OSON TIMESTAMP` |
2424
| `OffsetDateTime` | `OSON TIMESTAMPTZ` |
@@ -27,7 +27,7 @@ mapping is given by the following table.
2727
| `BigInteger` | `OSON NUMBER` |
2828
| `Year` | `OSON NUMBER` |
2929
| `byte[]` | `OSON byte[]` |
30-
| `java.util.Date` | `OSON DATE` |
30+
| `java.util.Date` | `OSON TIMESTAMP` |
3131
| `java.sql.Date` | `OSON DATE` |
3232
| `Timestamp` | `OSON TIMESTAMP` |
3333
| `LocalDate` | `OSON DATE` |

ojdbc-provider-jackson-oson/src/main/java/oracle/jdbc/provider/oson/OsonGenerator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,10 @@ public void writeDate(Date value) throws IOException {
570570
}else {
571571
// java.util.Date
572572
logger.log(Level.FINEST, "writeDate: java.util.Date");
573-
DATE dd = new DATE(new java.sql.Date(value.getTime()));
574-
OracleJsonDate jsonDate = new OracleJsonDateImpl(dd.shareBytes());
575-
gen.write(jsonDate);
573+
Timestamp ts = new Timestamp(value.getTime());
574+
TIMESTAMP timestamp = new TIMESTAMP(ts);
575+
OracleJsonTimestamp writeTimeStamp = new OracleJsonTimestampImpl(timestamp.shareBytes());
576+
gen.write(writeTimeStamp);
576577
}
577578
}
578579

ojdbc-provider-jackson-oson/src/test/java/oracle/jdbc/provider/oson/model/Employee.java

+70-36
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public class Employee {
129129
*/
130130
private Date hireDate;
131131

132+
/**
133+
* The date the employee was hired in java.util.Date.
134+
*/
135+
private java.util.Date hireUtilDate;
136+
132137
/**
133138
* The LocalDate the employee was hired.
134139
*/
@@ -261,7 +266,7 @@ public Employee(int employeeId, BigDecimal salary, BigInteger largeNumber, Strin
261266
long logoutTime, ZonedDateTime vacationZonedDateTime, short deptId, long employeeCode, byte grade,
262267
float bonus, double prevSalary, char gender, Period totalPeriod, Integer idBoxed, Boolean isActiveBoxed,
263268
Byte rankBoxed, Character genderBoxed, Short ageBoxed, Long salaryBoxed, Float performanceScoreBoxed,
264-
Double bonusBoxed) {
269+
Double bonusBoxed, java.util.Date hireUtilDate) {
265270
this.employeeId = employeeId;
266271
this.salary = salary;
267272
this.largeNumber = largeNumber;
@@ -302,6 +307,7 @@ public Employee(int employeeId, BigDecimal salary, BigInteger largeNumber, Strin
302307
this.salaryBoxed = salaryBoxed;
303308
this.performanceScoreBoxed = performanceScoreBoxed;
304309
this.bonusBoxed = bonusBoxed;
310+
this.hireUtilDate = hireUtilDate;
305311
}
306312

307313
// Getters and Setters
@@ -940,6 +946,16 @@ public void setBonusBoxed(Double bonusBoxed) {
940946
this.bonusBoxed = bonusBoxed;
941947
}
942948

949+
public java.util.Date getHireUtilDate() {
950+
return hireUtilDate;
951+
}
952+
953+
public void setHireUtilDate(java.util.Date hireUtilDate) {
954+
this.hireUtilDate = hireUtilDate;
955+
}
956+
957+
958+
943959

944960
/**
945961
* Provides a string representation of the Employee object.
@@ -950,18 +966,26 @@ public void setBonusBoxed(Double bonusBoxed) {
950966
public String toString() {
951967
return "Employee{" +
952968
"employeeId=" + employeeId +
969+
", logoutTime=" + logoutTime +
970+
", active=" + active +
971+
", deptId=" + deptId +
972+
", employeeCode=" + employeeCode +
973+
", grade=" + grade +
974+
", bonus=" + bonus +
975+
", prevSalary=" + prevSalary +
976+
", gender=" + gender +
953977
", salary=" + salary +
954978
", largeNumber=" + largeNumber +
955979
", firstName='" + firstName + '\'' +
956980
", middleInitial='" + middleInitial + '\'' +
957981
", lastName='" + lastName + '\'' +
958982
", hireDate=" + hireDate +
983+
", hireUtilDate=" + hireUtilDate +
959984
", localHireDate=" + localHireDate +
960985
", loginInstant=" + loginInstant +
961986
", loginLocalTime=" + loginLocalTime +
962987
", salaryMonthDay=" + salaryMonthDay +
963988
", incrementYearmonth=" + incrementYearmonth +
964-
", logoutTime=" + logoutTime +
965989
", startTime=" + startTime +
966990
", lastUpdated=" + lastUpdated +
967991
", localDateTime=" + localDateTime +
@@ -970,16 +994,9 @@ public String toString() {
970994
", durationOfEmployment=" + durationOfEmployment +
971995
", picture=" + Arrays.toString(picture) +
972996
", resume='" + resume + '\'' +
973-
", active=" + active +
974997
", rawData=" + Arrays.toString(rawData) +
975998
", phones=" + phones +
976999
", vacationZonedDateTime=" + vacationZonedDateTime +
977-
", deptId=" + deptId +
978-
", employeeCode=" + employeeCode +
979-
", grade=" + grade +
980-
", bonus=" + bonus +
981-
", gender=" + gender +
982-
", prevSalary=" + prevSalary +
9831000
", totalPeriod=" + totalPeriod +
9841001
", idBoxed=" + idBoxed +
9851002
", isActiveBoxed=" + isActiveBoxed +
@@ -993,41 +1010,58 @@ public String toString() {
9931010
}
9941011

9951012
@Override
996-
public boolean equals(Object o) {
997-
if (this == o) return true;
998-
if (o == null || getClass() != o.getClass()) return false;
999-
Employee employee = (Employee) o;
1000-
return employeeId == employee.employeeId && logoutTime == employee.logoutTime && active == employee.active
1001-
&& deptId == employee.deptId && employeeCode == employee.employeeCode && grade == employee.grade
1002-
&& Float.compare(bonus, employee.bonus) == 0 && Double.compare(prevSalary, employee.prevSalary) == 0
1013+
public boolean equals(Object object) {
1014+
if (this == object) return true;
1015+
if (object == null || getClass() != object.getClass()) return false;
1016+
Employee employee = (Employee) object;
1017+
return employeeId == employee.employeeId && logoutTime == employee.logoutTime
1018+
&& active == employee.active && deptId == employee.deptId
1019+
&& employeeCode == employee.employeeCode && grade == employee.grade
1020+
&& Float.compare(bonus, employee.bonus) == 0
1021+
&& Double.compare(prevSalary, employee.prevSalary) == 0
10031022
&& gender == employee.gender && Objects.equals(salary, employee.salary)
1004-
&& Objects.equals(largeNumber, employee.largeNumber) && Objects.equals(firstName, employee.firstName)
1005-
&& Objects.equals(middleInitial, employee.middleInitial) && Objects.equals(lastName, employee.lastName)
1006-
&& Objects.equals(hireDate, employee.hireDate) && Objects.equals(localHireDate, employee.localHireDate)
1007-
&& Objects.equals(loginInstant, employee.loginInstant) && Objects.equals(loginLocalTime, employee.loginLocalTime)
1023+
&& Objects.equals(largeNumber, employee.largeNumber)
1024+
&& Objects.equals(firstName, employee.firstName)
1025+
&& Objects.equals(middleInitial, employee.middleInitial)
1026+
&& Objects.equals(lastName, employee.lastName)
1027+
&& Objects.equals(hireDate, employee.hireDate)
1028+
&& Objects.equals(hireUtilDate, employee.hireUtilDate)
1029+
&& Objects.equals(localHireDate, employee.localHireDate)
1030+
&& Objects.equals(loginInstant, employee.loginInstant)
1031+
&& Objects.equals(loginLocalTime, employee.loginLocalTime)
10081032
&& Objects.equals(salaryMonthDay, employee.salaryMonthDay)
10091033
&& Objects.equals(incrementYearmonth, employee.incrementYearmonth)
1010-
&& Objects.equals(startTime, employee.startTime) && Objects.equals(lastUpdated, employee.lastUpdated)
1011-
&& Objects.equals(localDateTime, employee.localDateTime) && Objects.equals(offsetDateTime, employee.offsetDateTime)
1012-
&& Objects.equals(yearJoined, employee.yearJoined) && Objects.equals(durationOfEmployment, employee.durationOfEmployment)
1013-
&& Objects.equals(resume, employee.resume)
1034+
&& Objects.equals(startTime, employee.startTime)
1035+
&& Objects.equals(lastUpdated, employee.lastUpdated)
1036+
&& Objects.equals(localDateTime, employee.localDateTime)
1037+
&& Objects.equals(offsetDateTime, employee.offsetDateTime)
1038+
&& Objects.equals(yearJoined, employee.yearJoined)
1039+
&& Objects.equals(durationOfEmployment, employee.durationOfEmployment)
1040+
&& Objects.deepEquals(picture, employee.picture)
1041+
&& Objects.equals(resume, employee.resume)
1042+
&& Objects.deepEquals(rawData, employee.rawData)
10141043
&& Objects.equals(phones, employee.phones)
10151044
&& Objects.equals(vacationZonedDateTime, employee.vacationZonedDateTime)
1016-
&& Objects.equals(totalPeriod, employee.totalPeriod) && Objects.equals(idBoxed, employee.idBoxed)
1017-
&& Objects.equals(isActiveBoxed, employee.isActiveBoxed) && Objects.equals(rankBoxed, employee.rankBoxed)
1018-
&& Objects.equals(genderBoxed, employee.genderBoxed) && Objects.equals(ageBoxed, employee.ageBoxed)
1019-
&& Objects.equals(salaryBoxed, employee.salaryBoxed) && Objects.equals(performanceScoreBoxed, employee.performanceScoreBoxed)
1020-
&& Objects.equals(bonusBoxed, employee.bonusBoxed)
1021-
&& Arrays.equals(picture, employee.picture) && Arrays.equals(rawData, employee.rawData);
1045+
&& Objects.equals(totalPeriod, employee.totalPeriod)
1046+
&& Objects.equals(idBoxed, employee.idBoxed)
1047+
&& Objects.equals(isActiveBoxed, employee.isActiveBoxed)
1048+
&& Objects.equals(rankBoxed, employee.rankBoxed)
1049+
&& Objects.equals(genderBoxed, employee.genderBoxed)
1050+
&& Objects.equals(ageBoxed, employee.ageBoxed)
1051+
&& Objects.equals(salaryBoxed, employee.salaryBoxed)
1052+
&& Objects.equals(performanceScoreBoxed, employee.performanceScoreBoxed)
1053+
&& Objects.equals(bonusBoxed, employee.bonusBoxed);
10221054
}
10231055

10241056
@Override
10251057
public int hashCode() {
1026-
return Objects.hash(employeeId, logoutTime, active, deptId, employeeCode, grade, bonus, prevSalary, gender,
1027-
salary, largeNumber, firstName, middleInitial, lastName, hireDate, localHireDate, loginInstant,
1028-
loginLocalTime, salaryMonthDay, incrementYearmonth, startTime, lastUpdated, localDateTime, offsetDateTime,
1029-
yearJoined, durationOfEmployment, Arrays.hashCode(picture), resume, Arrays.hashCode(rawData), phones,
1030-
vacationZonedDateTime, totalPeriod, idBoxed, isActiveBoxed, rankBoxed, genderBoxed, ageBoxed, salaryBoxed,
1031-
performanceScoreBoxed, bonusBoxed);
1058+
return Objects.hash(employeeId, logoutTime, active, deptId, employeeCode, grade,
1059+
bonus, prevSalary, gender, salary, largeNumber, firstName, middleInitial,
1060+
lastName, hireDate, hireUtilDate, localHireDate, loginInstant, loginLocalTime,
1061+
salaryMonthDay, incrementYearmonth, startTime, lastUpdated, localDateTime,
1062+
offsetDateTime, yearJoined, durationOfEmployment,
1063+
Arrays.hashCode(picture), resume, Arrays.hashCode(rawData), phones,
1064+
vacationZonedDateTime, totalPeriod, idBoxed, isActiveBoxed,
1065+
rankBoxed, genderBoxed, ageBoxed, salaryBoxed, performanceScoreBoxed, bonusBoxed);
10321066
}
10331067
}

ojdbc-provider-jackson-oson/src/test/java/oracle/jdbc/provider/oson/model/EmployeeInstances.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static void instantiateEmployees() {
105105
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)101,
106106
1234567890L, (byte)5, 1500.5f, 75000.0, 'M', Period.of(10,10,0),
107107
1, true, (byte) 1, 'M', (short) 25, 50000L,
108-
4.5f, 5000.0));
108+
4.5f, 5000.0, java.util.Date.from(Instant.now())));
109109

110110
employees.add(new Employee(2, new BigDecimal("60000.99"), new BigInteger("987654321"),
111111
"Jane", "B", "Smith", Date.valueOf("2016-02-20"), Time.valueOf("08:30:00"),
@@ -115,7 +115,7 @@ private static void instantiateEmployees() {
115115
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)102,
116116
1234567891L, (byte)4, 1200.0f, 68000.0, 'F',Period.of(10,10,0),
117117
2, false, (byte) 2, 'F', (short) 30, 60000L,
118-
4.0f, 6000.0));
118+
4.0f, 6000.0, java.util.Date.from(Instant.now())));
119119

120120
employees.add(new Employee(3, new BigDecimal("45000.99"), new BigInteger("456789123"),
121121
"Alice", "C", "Johnson", Date.valueOf("2017-03-30"), Time.valueOf("07:45:00"),
@@ -124,7 +124,7 @@ private static void instantiateEmployees() {
124124
false, new byte[]{5, 6}, phones3, LocalDate.now(), Instant.now(),LocalTime.now(),
125125
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)104,
126126
1234567893L, (byte)2, 950.0f, 60000.0, 'F',Period.of(10,10,0),3,
127-
true, (byte) 3, 'M', (short) 35, 70000L, 3.5f, 7000.0));
127+
true, (byte) 3, 'M', (short) 35, 70000L, 3.5f, 7000.0, java.util.Date.from(Instant.now())));
128128

129129
employees.add(new Employee(4, new BigDecimal("70000.99"), new BigInteger("654321987"),
130130
"Bob", "D", "Williams", Date.valueOf("2018-04-15"), Time.valueOf("10:15:00"),
@@ -133,7 +133,7 @@ private static void instantiateEmployees() {
133133
true, new byte[]{7, 8}, phones4, LocalDate.now(), Instant.now(),LocalTime.now(),
134134
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)103,
135135
1234567892L, (byte)3, 800.0f, 55000.0, 'M',Period.of(10,10,0),4,
136-
true, (byte) 4, 'F', (short) 40, 80000L, 4.8f, 8000.0));
136+
true, (byte) 4, 'F', (short) 40, 80000L, 4.8f, 8000.0, java.util.Date.from(Instant.now())));
137137

138138
employees.add(new Employee(5, new BigDecimal("55000.99"), new BigInteger("321987654"),
139139
"Charlie", "E", "Brown", Date.valueOf("2019-05-05"), Time.valueOf("11:00:00"),
@@ -142,7 +142,7 @@ private static void instantiateEmployees() {
142142
false, new byte[]{9, 10}, phones5, LocalDate.now(), Instant.now(),LocalTime.now(),
143143
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)105,
144144
1234567894L, (byte)5, 1400.0f, 72000.0, 'M',Period.of(10,10,0),5,
145-
false, (byte) 5, 'M', (short) 45, 90000L, 3.8f, 9000.0));
145+
false, (byte) 5, 'M', (short) 45, 90000L, 3.8f, 9000.0, java.util.Date.from(Instant.now())));
146146

147147
employees.add(new Employee(6, new BigDecimal("48000.99"), new BigInteger("159753852"),
148148
"Eve", "F", "Davis", Date.valueOf("2020-06-10"), Time.valueOf("12:00:00"),
@@ -151,7 +151,7 @@ private static void instantiateEmployees() {
151151
true, new byte[]{11, 12}, phones6, LocalDate.now(), Instant.now(),LocalTime.now(),
152152
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)110,
153153
1234567899L, (byte)5, 1600.0f, 80000.0, 'F',Period.of(10,10,0),6,
154-
true, (byte) 6, 'F', (short) 50, 100000L, 4.2f, 10000.0));
154+
true, (byte) 6, 'F', (short) 50, 100000L, 4.2f, 10000.0,java.util.Date.from(Instant.now())));
155155

156156
employees.add(new Employee(7, new BigDecimal("62000.99"), new BigInteger("951753852"),
157157
"Frank", "G", "Evans", Date.valueOf("2021-07-20"), Time.valueOf("08:00:00"),
@@ -160,7 +160,7 @@ private static void instantiateEmployees() {
160160
false, new byte[]{13, 14}, phones7, LocalDate.now(), Instant.now(),LocalTime.now(),
161161
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)109,
162162
1234567898L, (byte)2, 900.0f, 61000.0, 'M',Period.of(10,10,0),7,
163-
false, (byte) 7, 'M', (short) 55, 110000L, 3.2f, 11000.0));
163+
false, (byte) 7, 'M', (short) 55, 110000L, 3.2f, 11000.0,java.util.Date.from(Instant.now())));
164164

165165
employees.add(new Employee(8, new BigDecimal("53000.99"), new BigInteger("753951456"),
166166
"Grace", "H", "Green", Date.valueOf("2022-08-30"), Time.valueOf("09:30:00"),
@@ -169,7 +169,7 @@ private static void instantiateEmployees() {
169169
true, new byte[]{15, 16}, phones8, LocalDate.now(), Instant.now(),LocalTime.now(),
170170
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC), (short)106,
171171
1234567895L, (byte)1, 500.0f, 45000.0, 'F',Period.of(10,10,0),8,
172-
true, (byte) 8, 'F', (short) 60, 120000L, 4.7f, 12000.0));
172+
true, (byte) 8, 'F', (short) 60, 120000L, 4.7f, 12000.0,java.util.Date.from(Instant.now())));
173173

174174
employees.add(new Employee(9, new BigDecimal("59000.99"), new BigInteger("357159852"),
175175
"Hank", "I", "Martinez", Date.valueOf("2023-09-01"), Time.valueOf("10:45:00"),
@@ -178,7 +178,7 @@ private static void instantiateEmployees() {
178178
false, new byte[]{17, 18}, phones9, LocalDate.now(), Instant.now(),LocalTime.now(),
179179
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)107,
180180
1234567896L, (byte)3, 850.0f, 58000.0, 'M', Period.of(10,10,0),9,
181-
false, (byte) 9, 'M', (short) 65, 130000L, 3.6f, 13000.0));
181+
false, (byte) 9, 'M', (short) 65, 130000L, 3.6f, 13000.0,java.util.Date.from(Instant.now())));
182182

183183
employees.add(new Employee(10, new BigDecimal("61000.99"), new BigInteger("147258369"),
184184
"Ivy", "J", "Parker", Date.valueOf("2024-09-01"), Time.valueOf("11:15:00"),
@@ -187,6 +187,6 @@ private static void instantiateEmployees() {
187187
true, new byte[]{19, 20}, phones10, LocalDate.now(), Instant.now(),LocalTime.now(),
188188
MonthDay.now(), YearMonth.now(), System.currentTimeMillis(), ZonedDateTime.now(ZoneOffset.UTC),(short)106,
189189
1234567895L, (byte)1, 500.0f, 45000.0, 'F',Period.of(10,10,0),10,
190-
true, (byte) 10, 'F', (short) 70, 140000L, 4.9f, 14000.0));
190+
true, (byte) 10, 'F', (short) 70, 140000L, 4.9f, 14000.0,java.util.Date.from(Instant.now())));
191191
}
192192
}

ojdbc-provider-jackson-oson/src/test/java/oracle/jdbc/provider/oson/test/MultiThreadTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void multithreadTest2() {
103103
try(ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
104104
try (OracleJsonParser oParser = jsonFactory.createJsonBinaryParser(in)) {
105105
Organisation deserOrg = (Organisation) conv.deserialize(oParser, Organisation.class);
106-
Assertions.assertEquals(deserOrg, organisation);
106+
Assertions.assertEquals(organisation, deserOrg);
107107
}
108108
}
109109

0 commit comments

Comments
 (0)