-
Notifications
You must be signed in to change notification settings - Fork 830
Backport OffsetDateTime serializers in GLVs to enable deserialization from server #3168
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
Open
xiazcy
wants to merge
1
commit into
3.7-dev
Choose a base branch
from
offsetdatetime_ser_backport
base: 3.7-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/Types/OffsetDateTimeSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#region License | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#endregion | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Gremlin.Net.Structure.IO.GraphBinary.Types | ||
{ | ||
/// <summary> | ||
/// A serializer for the GraphBinary type OffsetDateTime, represented as <see cref="DateTimeOffset"/> | ||
/// in .NET. | ||
/// </summary> | ||
public class OffsetDateTimeSerializer : SimpleTypeSerializer<DateTimeOffset> | ||
{ | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OffsetDateTimeSerializer" /> class. | ||
/// </summary> | ||
public OffsetDateTimeSerializer() : base(DataType.OffsetDateTime) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task WriteValueAsync(DateTimeOffset value, Stream stream, GraphBinaryWriter writer, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
await stream.WriteIntAsync(value.Year, cancellationToken).ConfigureAwait(false); | ||
await stream.WriteByteAsync(Convert.ToByte(value.Month), cancellationToken).ConfigureAwait(false); | ||
await stream.WriteByteAsync(Convert.ToByte(value.Day), cancellationToken).ConfigureAwait(false); | ||
// Note that nanosecond precisions were added after .NET 7 | ||
// Get the time of day as TimeSpan | ||
var timeOfDay = value.TimeOfDay; | ||
// Convert ticks to nanoseconds (1 tick = 100 nanoseconds) | ||
var ns = timeOfDay.Ticks * 100; | ||
await stream.WriteLongAsync(Convert.ToInt64(ns), cancellationToken).ConfigureAwait(false); | ||
|
||
var offset = value.Offset; | ||
var os = offset.Hours * 60 * 60 + offset.Minutes * 60 + offset.Seconds; | ||
await stream.WriteIntAsync(os, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task<DateTimeOffset> ReadValueAsync(Stream stream, GraphBinaryReader reader, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var year = await stream.ReadIntAsync(cancellationToken).ConfigureAwait(false); | ||
var month = await stream.ReadByteAsync(cancellationToken).ConfigureAwait(false); | ||
var day = await stream.ReadByteAsync(cancellationToken).ConfigureAwait(false); | ||
var ns = await stream.ReadLongAsync(cancellationToken).ConfigureAwait(false); | ||
var timeDelta = TimeSpan.FromMilliseconds(ns / 1e6); | ||
|
||
var os = await stream.ReadIntAsync(cancellationToken).ConfigureAwait(false); | ||
var offset = TimeSpan.FromSeconds(os); | ||
|
||
return new DateTimeOffset(year, Convert.ToInt32(month), Convert.ToInt32(day), 0, 0, 0, offset).Add(timeDelta); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/OffsetDateTimeDeserializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#region License | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#endregion | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
|
||
namespace Gremlin.Net.Structure.IO.GraphSON | ||
{ | ||
internal class OffsetDateTimeDeserializer : IGraphSONDeserializer | ||
{ | ||
public dynamic Objectify(JsonElement graphsonObject, GraphSONReader reader) | ||
{ | ||
return DateTimeOffset.Parse(graphsonObject.GetString() ?? | ||
throw new IOException("Read null but expected a OffsetDateTime value")); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/OffsetDateTimeSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#region License | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Gremlin.Net.Structure.IO.GraphSON | ||
{ | ||
internal class OffsetDateTimeSerializer : IGraphSONSerializer | ||
{ | ||
public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer) | ||
{ | ||
DateTimeOffset value = objectData; | ||
return GraphSONUtil.ToTypedValue("OffsetDateTime", value.ToString("O"), "gx"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -108,6 +108,19 @@ public void ShouldDeserializeDateToDateTimeOffset(int version) | |||||||||||||||||||||||||||||||
Assert.Equal(expectedDateTimeOffset, deserializedValue); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
[Theory, MemberData(nameof(Versions))] | ||||||||||||||||||||||||||||||||
public void ShouldDeserializeOffsetDateTimeToDateTimeOffset(int version) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
const string graphSon = "{\"@type\":\"gx:OffsetDateTime\",\"@value\":\"2016-10-04T12:17:22.5520000+00:00\"}"; | ||||||||||||||||||||||||||||||||
var reader = CreateStandardGraphSONReader(version); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var jsonElement = JsonSerializer.Deserialize<JsonElement>(graphSon); | ||||||||||||||||||||||||||||||||
var deserializedValue = reader.ToObject(jsonElement); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var expectedDateTimeOffset = TestUtils.FromJavaTime(1475583442552); | ||||||||||||||||||||||||||||||||
Comment on lines
+114
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
Assert.Equal(expectedDateTimeOffset, deserializedValue); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
[Theory, MemberData(nameof(Versions))] | ||||||||||||||||||||||||||||||||
public void ShouldDeserializeDictionary(int version) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to clarify that I'm understanding this correctly. This
OffsetDateTimeSerializer
will not be used until 3.8 and is not being used in 3.7.4. This was not added to theGraphSONWriter.cs
logic because for 3.7.4 we are still serializingDateTimeOffset
asDate
and there is no way to writeOffsetDateTime
to a request.Is my understanding correct? If so, is there any value with adding this
OffsetDateTimeSerializer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is correct. This is more of a connivence back-port for the ability to deserialize potential OffsetDateTime from server in the GLVs (Java has the support already). I don't quite expect this to be used per se, but the difference would be a deserialization error vs successful deserialization.
Potential downside is that OffsetDateTime will not be able to round-trip, given GLVs still use Date as default type for native date supports.