Skip to content

Add API entry for JsonDocument converter #60236

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

Merged
merged 6 commits into from
Apr 20, 2022
Merged
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
Expand Up @@ -34,6 +34,7 @@ private sealed class Parser
private const string JsonNodeFullName = "System.Text.Json.Nodes.JsonNode";
private const string JsonObjectFullName = "System.Text.Json.Nodes.JsonObject";
private const string JsonValueFullName = "System.Text.Json.Nodes.JsonValue";
private const string JsonDocumentFullName = "System.Text.Json.JsonDocument";
private const string JsonIgnoreAttributeFullName = "System.Text.Json.Serialization.JsonIgnoreAttribute";
private const string JsonIgnoreConditionFullName = "System.Text.Json.Serialization.JsonIgnoreCondition";
private const string JsonIncludeAttributeFullName = "System.Text.Json.Serialization.JsonIncludeAttribute";
Expand Down Expand Up @@ -92,6 +93,7 @@ private sealed class Parser
private readonly Type? _jsonNodeType;
private readonly Type? _jsonObjectType;
private readonly Type? _jsonValueType;
private readonly Type? _jsonDocumentType;

// Unsupported types
private readonly Type _delegateType;
Expand Down Expand Up @@ -229,6 +231,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
_jsonNodeType = _metadataLoadContext.Resolve(JsonNodeFullName);
_jsonObjectType = _metadataLoadContext.Resolve(JsonObjectFullName);
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);
_jsonDocumentType = _metadataLoadContext.Resolve(JsonDocumentFullName);

// Unsupported types.
_delegateType = _metadataLoadContext.Resolve(SpecialType.System_Delegate);
Expand Down Expand Up @@ -1556,6 +1559,7 @@ private void PopulateKnownTypes()
AddTypeIfNotNull(_knownTypes, _jsonNodeType);
AddTypeIfNotNull(_knownTypes, _jsonObjectType);
AddTypeIfNotNull(_knownTypes, _jsonValueType);
AddTypeIfNotNull(_knownTypes, _jsonDocumentType);

_knownUnsupportedTypes.Add(_typeType);
_knownUnsupportedTypes.Add(_serializationInfoType);
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ public static partial class JsonMetadataServices
public static System.Text.Json.Serialization.JsonConverter<System.Text.Json.Nodes.JsonNode> JsonNodeConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<System.Text.Json.Nodes.JsonObject> JsonObjectConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<System.Text.Json.Nodes.JsonValue> JsonValueConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<System.Text.Json.JsonDocument> JsonDocumentConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<object> ObjectConverter { get { throw null; } }
[System.CLSCompliantAttribute(false)]
public static System.Text.Json.Serialization.JsonConverter<sbyte> SByteConverter { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ private static Dictionary<Type, JsonConverter> GetDefaultSimpleConverters()
Add(JsonMetadataServices.Int16Converter);
Add(JsonMetadataServices.Int32Converter);
Add(JsonMetadataServices.Int64Converter);
Add(new JsonElementConverter());
Add(new JsonDocumentConverter());
Add(JsonMetadataServices.JsonElementConverter);
Add(JsonMetadataServices.JsonDocumentConverter);
Add(JsonMetadataServices.ObjectConverter);
Add(JsonMetadataServices.SByteConverter);
Add(JsonMetadataServices.SingleConverter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Converters;

Expand Down Expand Up @@ -128,6 +127,13 @@ public static partial class JsonMetadataServices
public static JsonConverter<JsonValue> JsonValueConverter => s_jsonValueConverter ??= new JsonValueConverter();
private static JsonConverter<JsonValue>? s_jsonValueConverter;

/// <summary>
/// Returns a <see cref="JsonConverter{T}"/> instance that converts <see cref="JsonDocument"/> values.
/// </summary>
/// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
public static JsonConverter<JsonDocument> JsonDocumentConverter => s_jsonDocumentConverter ??= new JsonDocumentConverter();
private static JsonConverter<JsonDocument>? s_jsonDocumentConverter;

/// <summary>
/// Returns a <see cref="JsonConverter{T}"/> instance that converts <see cref="object"/> values.
/// </summary>
Expand Down
13 changes: 9 additions & 4 deletions src/libraries/System.Text.Json/tests/Common/JsonTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public static void AssertJsonEqual(string expected, string actual)
{
using JsonDocument expectedDom = JsonDocument.Parse(expected);
using JsonDocument actualDom = JsonDocument.Parse(actual);
AssertJsonEqual(expectedDom.RootElement, actualDom.RootElement, new());
AssertJsonEqual(expectedDom.RootElement, actualDom.RootElement);
}

private static void AssertJsonEqual(JsonElement expected, JsonElement actual, Stack<object> path)
public static void AssertJsonEqual(JsonElement expected, JsonElement actual)
{
AssertJsonEqualCore(expected, actual, new());
}

private static void AssertJsonEqualCore(JsonElement expected, JsonElement actual, Stack<object> path)
{
JsonValueKind valueKind = expected.ValueKind;
AssertTrue(passCondition: valueKind == actual.ValueKind);
Expand Down Expand Up @@ -54,7 +59,7 @@ private static void AssertJsonEqual(JsonElement expected, JsonElement actual, St
foreach (string name in expectedProperties)
{
path.Push(name);
AssertJsonEqual(expected.GetProperty(name), actual.GetProperty(name), path);
AssertJsonEqualCore(expected.GetProperty(name), actual.GetProperty(name), path);
path.Pop();
}
break;
Expand All @@ -67,7 +72,7 @@ private static void AssertJsonEqual(JsonElement expected, JsonElement actual, St
{
AssertTrue(passCondition: actualEnumerator.MoveNext(), "Actual array contains fewer elements.");
path.Push(i++);
AssertJsonEqual(expectedEnumerator.Current, actualEnumerator.Current, path);
AssertJsonEqualCore(expectedEnumerator.Current, actualEnumerator.Current, path);
path.Pop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface ITestContext
public JsonTypeInfo<byte[]> ByteArray { get; }
public JsonTypeInfo<string> String { get; }
public JsonTypeInfo<(string Label1, int Label2, bool)> ValueTupleStringInt32Boolean { get; }
public JsonTypeInfo<JsonDocument> JsonDocument { get; }
public JsonTypeInfo<JsonElement> JsonElement { get; }
public JsonTypeInfo<RealWorldContextTests.ClassWithEnumAndNullable> ClassWithEnumAndNullable { get; }
public JsonTypeInfo<RealWorldContextTests.ClassWithNullableProperties> ClassWithNullableProperties { get; }
public JsonTypeInfo<ClassWithCustomConverter> ClassWithCustomConverter { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSerializable(typeof(object[]))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof((string Label1, int Label2, bool)))]
[JsonSerializable(typeof(JsonDocument))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithEnumAndNullable))]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithNullableProperties))]
[JsonSerializable(typeof(ClassWithCustomConverter))]
Expand Down Expand Up @@ -81,6 +83,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MetadataAndSerializationContext.Default.SampleEnum.SerializeHandler);
Assert.Null(MetadataAndSerializationContext.Default.String.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.ValueTupleStringInt32Boolean.SerializeHandler);
Assert.Null(MetadataAndSerializationContext.Default.JsonDocument.SerializeHandler);
Assert.Null(MetadataAndSerializationContext.Default.JsonElement.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithEnumAndNullable.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithNullableProperties.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithCustomConverter);
Expand All @@ -97,6 +101,7 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.NotNull(MetadataAndSerializationContext.Default.PersonStruct.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.TypeWithValidationAttributes.SerializeHandler);
Assert.NotNull(MetadataAndSerializationContext.Default.TypeWithDerivedAttribute.SerializeHandler);
Assert.Null(MetadataAndSerializationContext.Default.PolymorphicClass.SerializeHandler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSerializable(typeof(byte[]), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(string), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof((string Label1, int Label2, bool)), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(JsonDocument), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(JsonElement), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithEnumAndNullable), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithNullableProperties), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(ClassWithCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)]
Expand Down Expand Up @@ -79,6 +81,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MetadataWithPerTypeAttributeContext.Default.SampleEnum.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.String.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ValueTupleStringInt32Boolean.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.JsonDocument.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.JsonElement.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithEnumAndNullable.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithNullableProperties.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithCustomConverter.SerializeHandler);
Expand All @@ -95,6 +99,7 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MetadataWithPerTypeAttributeContext.Default.PersonStruct.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.TypeWithValidationAttributes.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.TypeWithDerivedAttribute.SerializeHandler);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.PolymorphicClass.SerializeHandler);
}
}

Expand All @@ -121,6 +126,8 @@ public override void EnsureFastPathGeneratedAsExpected()
[JsonSerializable(typeof(byte[]))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof((string Label1, int Label2, bool)))]
[JsonSerializable(typeof(JsonDocument))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithEnumAndNullable))]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithNullableProperties))]
[JsonSerializable(typeof(ClassWithCustomConverter))]
Expand Down Expand Up @@ -194,6 +201,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MetadataContext.Default.SampleEnum.SerializeHandler);
Assert.Null(MetadataContext.Default.String.SerializeHandler);
Assert.Null(MetadataContext.Default.ValueTupleStringInt32Boolean.SerializeHandler);
Assert.Null(MetadataContext.Default.JsonDocument.SerializeHandler);
Assert.Null(MetadataContext.Default.JsonElement.SerializeHandler);
Assert.Null(MetadataContext.Default.ClassWithEnumAndNullable.SerializeHandler);
Assert.Null(MetadataContext.Default.ClassWithNullableProperties.SerializeHandler);
Assert.Null(MetadataContext.Default.ClassWithCustomConverter.SerializeHandler);
Expand All @@ -210,6 +219,7 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MetadataContext.Default.PersonStruct.SerializeHandler);
Assert.Null(MetadataContext.Default.TypeWithValidationAttributes.SerializeHandler);
Assert.Null(MetadataContext.Default.TypeWithDerivedAttribute.SerializeHandler);
Assert.Null(MetadataContext.Default.PolymorphicClass.SerializeHandler);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSerializable(typeof(byte[]), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(string), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof((string Label1, int Label2, bool)), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(JsonDocument), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(JsonElement), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithEnumAndNullable), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(RealWorldContextTests.ClassWithNullableProperties), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ClassWithCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
Expand Down Expand Up @@ -81,6 +83,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.Null(MixedModeContext.Default.SampleEnum.SerializeHandler);
Assert.Null(MixedModeContext.Default.String.SerializeHandler);
Assert.NotNull(MixedModeContext.Default.ValueTupleStringInt32Boolean.SerializeHandler);
Assert.Null(MixedModeContext.Default.JsonDocument.SerializeHandler);
Assert.Null(MixedModeContext.Default.JsonElement.SerializeHandler);
Assert.NotNull(MixedModeContext.Default.ClassWithEnumAndNullable.SerializeHandler);
Assert.NotNull(MixedModeContext.Default.ClassWithNullableProperties.SerializeHandler);
Assert.Null(MixedModeContext.Default.ClassWithCustomConverter.SerializeHandler);
Expand All @@ -97,6 +101,7 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert.NotNull(MixedModeContext.Default.PersonStruct.SerializeHandler);
Assert.NotNull(MixedModeContext.Default.TypeWithValidationAttributes.SerializeHandler);
Assert.NotNull(MixedModeContext.Default.TypeWithDerivedAttribute.SerializeHandler);
Assert.Null(MixedModeContext.Default.PolymorphicClass.SerializeHandler);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ public virtual void RoundTripTypeNameClash()
VerifyRepeatedLocation(expected, obj);
}

[Theory]
[InlineData("0")]
[InlineData("false")]
[InlineData("\"str\"")]
[InlineData("[1,2,3]")]
[InlineData("{ \"key\" : \"value\" }")]
public void RoundtripJsonDocument(string json)
{
JsonDocument jsonDocument = JsonDocument.Parse(json);

string actualJson = JsonSerializer.Serialize(jsonDocument, DefaultContext.JsonDocument);
JsonTestHelper.AssertJsonEqual(json, actualJson);

JsonDocument actualJsonDocument = JsonSerializer.Deserialize(actualJson, DefaultContext.JsonDocument);
JsonTestHelper.AssertJsonEqual(jsonDocument.RootElement, actualJsonDocument.RootElement);
}

[Theory]
[InlineData("0")]
[InlineData("false")]
[InlineData("\"str\"")]
[InlineData("[1,2,3]")]
[InlineData("{ \"key\" : \"value\" }")]
public void RoundtripJsonElement(string json)
{
JsonElement jsonElement = JsonDocument.Parse(json).RootElement;

string actualJson = JsonSerializer.Serialize(jsonElement, DefaultContext.JsonElement);
JsonTestHelper.AssertJsonEqual(json, actualJson);

JsonElement actualJsonElement = JsonSerializer.Deserialize(actualJson, DefaultContext.JsonElement);
JsonTestHelper.AssertJsonEqual(jsonElement, actualJsonElement);
}

[Fact]
public virtual void RoundTripValueTuple()
{
Expand Down
Loading