Skip to content

Commit

Permalink
Adds deserialization support for JsonDocument
Browse files Browse the repository at this point in the history
This change adds support to `System.Text.Json` for deserializing `JsonDocument`.

Specifically, an internal converter `JsonDocumentConverter` is added to the default converter dictionary.

I have created a basic test, but I feel more could be done here, and it may not be in the right file/class - some guidance would be helpful here.

Fixes dotnet#1573
  • Loading branch information
marcusturewicz committed Apr 4, 2020
1 parent 4e179eb commit fdbea3a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="System\Text\Json\Serialization\Converters\Value\Int32Converter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\Int64Converter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\JsonElementConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\JsonDocumentConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\KeyValuePairConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\KeyValuePairConverterFactory.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\NullableConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Text.Json.Serialization.Converters
{
internal sealed class JsonDocumentConverter : JsonConverter<JsonDocument>
{
public override JsonDocument Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonDocument.ParseValue(ref reader);
}

public override void Write(Utf8JsonWriter writer, JsonDocument value, JsonSerializerOptions options)
{
value.WriteTo(writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public sealed partial class JsonSerializerOptions

private static Dictionary<Type, JsonConverter> GetDefaultSimpleConverters()
{
const int NumberOfSimpleConverters = 22;
const int NumberOfSimpleConverters = 23;
var converters = new Dictionary<Type, JsonConverter>(NumberOfSimpleConverters);

// Use a dictionary for simple converters.
Expand All @@ -55,6 +55,7 @@ private static Dictionary<Type, JsonConverter> GetDefaultSimpleConverters()
Add(new Int32Converter());
Add(new Int64Converter());
Add(new JsonElementConverter());
Add(new JsonDocumentConverter());
Add(new ObjectConverter());
Add(new SByteConverter());
Add(new SingleConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public void PreserveReferencesCallbackTest()
p2.Spouse = p1;
Assert.Throws<JsonException> (() => JsonSerializer.Serialize(p1));
}

[Fact]
public void JsonDocumentDeserialization()
{
using (JsonDocument document = JsonSerializer.Deserialize<JsonDocument>("{\"data\": 12}"))
{
Assert.Equal(12, document.RootElement.GetProperty("data").GetInt32());
}
}
}

public class PersonReference
Expand Down

0 comments on commit fdbea3a

Please sign in to comment.