Skip to content
Open
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
@@ -0,0 +1,9 @@
{
"core": {
"changeLogMessages": [
"Add DocumentJsonConverter to fix System.Text.Json serialization of Document types. Document's IEnumerable interfaces caused STJ to treat it as a collection, throwing InvalidDocumentTypeConversionException."
],
"type": "patch",
"updateMinimum": false
}
}
7 changes: 6 additions & 1 deletion sdk/src/Core/Amazon.Runtime/Documents/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using ThirdParty.RuntimeBackports;
using System.Text.Json;
using System.Text.Json.Serialization;

using ThirdParty.RuntimeBackports;
namespace Amazon.Runtime.Documents
{
/// <summary>
Expand Down Expand Up @@ -51,6 +53,7 @@ namespace Amazon.Runtime.Documents
/// Document Types specification specifies support for arbitrary precision integers. However, the dotnet implementation
/// is limited to representing numbers as either <see cref="double"/>, <see cref="int"/> or <see cref="long"/>.
/// </remarks>
[JsonConverter(typeof(DocumentJsonConverter))]
public partial struct Document : IEquatable<Document>, IEnumerable<Document>, IEnumerable<KeyValuePair<string, Document>>
{
private readonly bool _dataBool;
Expand Down Expand Up @@ -438,6 +441,8 @@ private static Document FromObject(JsonElement jsonElement)
case JsonValueKind.True:
return new Document(jsonElement.GetBoolean());
case JsonValueKind.Number:
if (jsonElement.TryGetInt32(out int intValue))
return new Document(intValue);
if (jsonElement.TryGetInt64(out long longValue))
return new Document(longValue);
if (jsonElement.TryGetDouble(out double doubleValue))
Expand Down
93 changes: 93 additions & 0 deletions sdk/src/Core/Amazon.Runtime/Documents/DocumentJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Amazon.Runtime.Documents.Internal.Transform;

namespace Amazon.Runtime.Documents
{
/// <summary>
/// Custom <see cref="JsonConverter{T}"/> for <see cref="Document"/> that prevents
/// System.Text.Json from treating Document as a collection (due to its IEnumerable implementations)
/// and instead serializes/deserializes it according to its actual document type.
/// </summary>
internal sealed class DocumentJsonConverter : JsonConverter<Document>
{
/// <inheritdoc/>
public override Document Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return ReadDocument(ref reader);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Document value, JsonSerializerOptions options)
{
DocumentMarshaller.Instance.Write(writer, value);
}

private static Document ReadDocument(ref Utf8JsonReader reader)
{
switch (reader.TokenType)
{
case JsonTokenType.Null:
return new Document();

case JsonTokenType.True:
case JsonTokenType.False:
return new Document(reader.GetBoolean());

case JsonTokenType.Number:
if (reader.TryGetInt32(out int intValue))
return new Document(intValue);
if (reader.TryGetInt64(out long longValue))
return new Document(longValue);
if (reader.TryGetDouble(out double doubleValue))
return new Document(doubleValue);
throw new JsonException("Unsupported number type.");

case JsonTokenType.String:
return new Document(reader.GetString());

case JsonTokenType.StartArray:
var list = new List<Document>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
break;
list.Add(ReadDocument(ref reader));
}
return new Document(list);

case JsonTokenType.StartObject:
var dict = new Dictionary<string, Document>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
break;
var key = reader.GetString();
reader.Read();
dict.Add(key, ReadDocument(ref reader));
}
return new Document(dict);

default:
throw new JsonException($"Unexpected JSON token type: {reader.TokenType}");
}
}
}
}
Loading
Loading