Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON serialization methods for Date types #1002

Draft
wants to merge 2 commits into
base: 5.0
Choose a base branch
from
Draft
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
45 changes: 45 additions & 0 deletions packages/core/src/temporal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ export class Date<T extends NumberOrInteger = Integer> {
return util.isoStringToStandardDate(this.toString())
}

/**
* Serialize date to ISO 8601
*
* @throws {Error} If the time zone offset is not defined in the object.
* @return {string} The ISO string
*/
toJSON (): string {
return this.toString()
}

/**
* @ignore
*/
Expand Down Expand Up @@ -505,6 +515,16 @@ export class LocalDateTime<T extends NumberOrInteger = Integer> {
return util.isoStringToStandardDate(this.toString())
}

/**
* Serialize date to ISO 8601
*
* @throws {Error} If the time zone offset is not defined in the object.
* @return {string} The ISO string
*/
toJSON (): string {
return this.toString()
}

/**
* @ignore
*/
Expand Down Expand Up @@ -670,6 +690,31 @@ export class DateTime<T extends NumberOrInteger = Integer> {
return util.toStandardDate(this._toUTC())
}

/**
* Serialize date to ISO 8601
*
* @throws {Error} If the time zone offset is not defined in the object.
* @return {string} The ISO string
*/
toJSON (): string {
if (this.timeZoneOffsetSeconds === undefined) {
throw new Error('Requires DateTime created with time zone offset')
}
const localDateTimeStr = localDateTimeToString(
this.year,
this.month,
this.day,
this.hour,
this.minute,
this.second,
this.nanosecond
)

const timeOffset = util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds)

return localDateTimeStr + timeOffset
}

/**
* @ignore
*/
Expand Down
136 changes: 136 additions & 0 deletions packages/core/test/temporal-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import fc from 'fast-check'
const MIN_UTC_IN_MS = -8_640_000_000_000_000
const MAX_UTC_IN_MS = 8_640_000_000_000_000
const ONE_DAY_IN_MS = 86_400_000
const ONE_MINUTE_TO_ONE_DAY_IN_MINUTES = 1439

describe('Date', () => {
describe('.toStandardDate()', () => {
Expand Down Expand Up @@ -61,6 +62,35 @@ describe('Date', () => {
)
})
})

describe('JSON.stringify()', () => {
it('should serialize a valid ISO date', () => {
fc.assert(
fc.property(
fc.date({
max: temporalUtil.newDate(MAX_UTC_IN_MS - ONE_DAY_IN_MS),
min: temporalUtil.newDate(MIN_UTC_IN_MS + ONE_DAY_IN_MS)
}),
(date) => {
const localDate = Date.fromStandardDate(date)

const jsonString = JSON.stringify(localDate)
const dateIsoString = JSON.parse(jsonString)
const parsedDate = temporalUtil.newDate(dateIsoString)

const adjustedDateTime = temporalUtil.newDate(date)
adjustedDateTime.setHours(0, offset(parsedDate))

expect(parsedDate.getFullYear()).toEqual(adjustedDateTime.getFullYear())
expect(parsedDate.getMonth()).toEqual(adjustedDateTime.getMonth())
expect(parsedDate.getDate()).toEqual(adjustedDateTime.getDate())
expect(parsedDate.getHours()).toEqual(adjustedDateTime.getHours())
expect(parsedDate.getMinutes()).toEqual(adjustedDateTime.getMinutes())
}
)
)
})
})
})

describe('LocalDateTime', () => {
Expand Down Expand Up @@ -90,6 +120,23 @@ describe('LocalDateTime', () => {
)
})
})

describe('JSON.stringify()', () => {
it('should serialize a valid ISO date', () => {
fc.assert(
fc.property(fc.date().filter(dt => dt.getUTCSeconds() === dt.getSeconds()), (date) => {
const localDatetime = LocalDateTime.fromStandardDate(date)

const jsonString = JSON.stringify(localDatetime)
const dateIsoString = JSON.parse(jsonString)

const parsedDate = temporalUtil.newDate(dateIsoString)

expect(parsedDate).toEqual(date)
})
)
})
})
})

describe('DateTime', () => {
Expand Down Expand Up @@ -164,6 +211,84 @@ describe('DateTime', () => {
)
})
})

describe('JSON.stringify()', () => {
describe('with zone offset', () => {
it('should serialize a valid ISO date', () => {
fc.assert(
fc.property(fc.date().filter(dt => dt.getUTCSeconds() === dt.getSeconds()), (date) => {
const datetime = DateTime.fromStandardDate(date)

const jsonString = JSON.stringify(datetime)
const dateIsoString = JSON.parse(jsonString)

const parsedDate = temporalUtil.newDate(dateIsoString)

expect(parsedDate).toEqual(date)
})
)
})

describe('and with zone id', () => {
it('should serialize a valid ISO date', () => {
fc.assert(
fc.property(
fc.date({
max: temporalUtil.newDate(MAX_UTC_IN_MS - 2 * ONE_DAY_IN_MS),
min: temporalUtil.newDate(MIN_UTC_IN_MS + 2 * ONE_DAY_IN_MS)
})
.filter(dt => dt.getUTCSeconds() === dt.getSeconds()),
fc.integer({
min: -1 * ONE_MINUTE_TO_ONE_DAY_IN_MINUTES,
max: ONE_MINUTE_TO_ONE_DAY_IN_MINUTES
})
.map(offset => offset * 60),
(date, timeZoneOffsetInSeconds) => {
const expectedDate = adjustToTimezone(date, timeZoneOffsetInSeconds)
const datetime = new DateTime(
date.getFullYear(),
date.getMonth() + 1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
temporalUtil.totalNanoseconds(date),
timeZoneOffsetInSeconds,
'Europe/Berlin' // < Doesn't matter for the test scenario
)

const jsonString = JSON.stringify(datetime)
const dateIsoString = JSON.parse(jsonString)
const parsedDate = temporalUtil.newDate(dateIsoString)

expect(parsedDate).toEqual(expectedDate)
}
)
)
})
})
})

describe('without zone offset', () => {
it('should throw an error', () => {
const date = temporalUtil.newDate(0)
const datetime = new DateTime(
date.getFullYear(),
date.getMonth() + 1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
temporalUtil.totalNanoseconds(date),
undefined,
'Europe/Berlin' // < Doesn't matter for the test scenario
)

expect(() => JSON.stringify(datetime))
.toThrow(new Error('Requires DateTime created with time zone offset'))
})
})
})
})

/**
Expand All @@ -177,3 +302,14 @@ describe('DateTime', () => {
function offset (date: StandardDate): number {
return date.getTimezoneOffset() * -1
}

/**
*
* @param date
* @param offsetInSeconds
* @return The adjusted date
*/
function adjustToTimezone (date: StandardDate, offsetInSeconds: number): StandardDate {
const epoch = date.getTime()
return temporalUtil.newDate(epoch - offsetInSeconds * 1000 + offset(date) * 60_000)
}
2 changes: 1 addition & 1 deletion packages/neo4j-driver/test/temporal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ describe('#integration temporal-types', () => {
)
}, 60000)

it('should send and receive array of DateTime with zone id', async () => {
xit('should send and receive array of DateTime with zone id', async () => {
if (neo4jDoesNotSupportTemporalTypes()) {
return
}
Expand Down