-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CF/P AVAD: Fixes Deserialization of ChangeFeedItem and ChangeFeedMeta…
…data to support System.Text.Json and Newtonsoft.Json (#4618) * checkin in * support for both STJ and NSJ * update contracts. * name change PreviousLsn * STJ TypeConverter support for ChangeFeedMetadata * adding bacl StringEnumConverter * test for Writes ChangeFeedMetadata * removing DateTimeOffset as results are inconsistent. * trying to get GMT, not local * static UnixEpoch * static qualifier in tests * PropertyNameCaseInsensitive = false tests. copy of True tests. * setting PropertyNameCaseInsensitive correctly for tests * removed duplication for propertyNameCaseInsensitive tests * remove JsonStringEnumConverter(), from tests --------- Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com>
- Loading branch information
1 parent
2fbc278
commit a1144f4
Showing
8 changed files
with
947 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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
15 changes: 15 additions & 0 deletions
15
Microsoft.Azure.Cosmos/src/Resource/FullFidelity/ChangeFeedMetadataFields.cs
This file contains 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,15 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Resource.FullFidelity | ||
{ | ||
internal class ChangeFeedMetadataFields | ||
{ | ||
public const string ConflictResolutionTimestamp = "crts"; | ||
public const string Lsn = "lsn"; | ||
public const string OperationType = "operationType"; | ||
public const string PreviousImageLSN = "previousImageLSN"; | ||
public const string TimeToLiveExpired = "timeToLiveExpired"; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
Microsoft.Azure.Cosmos/src/Resource/FullFidelity/Converters/ChangeFeedMetadataConverter.cs
This file contains 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,92 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Resource.FullFidelity.Converters | ||
{ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Azure.Cosmos.Resource.FullFidelity; | ||
using Microsoft.Azure.Documents; | ||
|
||
/// <summary> | ||
/// Converter used to support System.Text.Json de/serialization of type ChangeFeedMetadata/>. | ||
/// </summary> | ||
internal class ChangeFeedMetadataConverter : JsonConverter<ChangeFeedMetadata> | ||
{ | ||
private readonly static DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); | ||
|
||
public override ChangeFeedMetadata Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException(string.Format(CultureInfo.CurrentCulture, RMResources.JsonUnexpectedToken)); | ||
} | ||
|
||
JsonElement element = JsonDocument.ParseValue(ref reader).RootElement; | ||
|
||
ChangeFeedMetadata metadata = new (); | ||
|
||
foreach (JsonProperty property in element.EnumerateObject()) | ||
{ | ||
if (property.NameEquals(ChangeFeedMetadataFields.Lsn)) | ||
{ | ||
metadata.Lsn = property.Value.GetInt64(); | ||
} | ||
else if (property.NameEquals(ChangeFeedMetadataFields.ConflictResolutionTimestamp)) | ||
{ | ||
metadata.ConflictResolutionTimestamp = ChangeFeedMetadataConverter.ToDateTimeFromUnixTimeInSeconds(property.Value.GetInt64()); | ||
} | ||
else if (property.NameEquals(ChangeFeedMetadataFields.OperationType)) | ||
{ | ||
metadata.OperationType = (ChangeFeedOperationType)Enum.Parse(enumType: typeof(ChangeFeedOperationType), value: property.Value.GetString(), ignoreCase: true); | ||
} | ||
else if (property.NameEquals(ChangeFeedMetadataFields.TimeToLiveExpired)) | ||
{ | ||
metadata.IsTimeToLiveExpired = property.Value.GetBoolean(); | ||
} | ||
else if (property.NameEquals(ChangeFeedMetadataFields.PreviousImageLSN)) | ||
{ | ||
metadata.PreviousLsn = property.Value.GetInt64(); | ||
} | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ChangeFeedMetadata value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
return; | ||
} | ||
|
||
writer.WriteStartObject(); | ||
|
||
writer.WriteNumber(ChangeFeedMetadataFields.ConflictResolutionTimestamp, ChangeFeedMetadataConverter.ToUnixTimeInSecondsFromDateTime(value.ConflictResolutionTimestamp)); | ||
writer.WriteBoolean(ChangeFeedMetadataFields.TimeToLiveExpired, value.IsTimeToLiveExpired); | ||
writer.WriteNumber(ChangeFeedMetadataFields.Lsn, value.Lsn); | ||
writer.WriteString(ChangeFeedMetadataFields.OperationType, value.OperationType.ToString()); | ||
writer.WriteNumber(ChangeFeedMetadataFields.PreviousImageLSN, value.PreviousLsn); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
private static long ToUnixTimeInSecondsFromDateTime(DateTime date) | ||
{ | ||
return (long)(date - ChangeFeedMetadataConverter.UnixEpoch).TotalSeconds; | ||
} | ||
|
||
private static DateTime ToDateTimeFromUnixTimeInSeconds(long unixTimeInSeconds) | ||
{ | ||
return ChangeFeedMetadataConverter.UnixEpoch.AddSeconds(unixTimeInSeconds); | ||
} | ||
} | ||
} |
Oops, something went wrong.