Skip to content

Commit ee07190

Browse files
authored
Add JsonNode feature (#51025)
1 parent b80ddef commit ee07190

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5849
-20
lines changed

src/libraries/System.Text.Json/ref/System.Text.Json.cs

Lines changed: 168 additions & 0 deletions
Large diffs are not rendered by default.

src/libraries/System.Text.Json/ref/System.Text.Json.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent);netcoreapp3.0;netstandard2.0;net461</TargetFrameworks>
44
<Nullable>enable</Nullable>
@@ -13,10 +13,12 @@
1313
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
1414
<ProjectReference Include="$(LibrariesProjectRoot)System.Memory\ref\System.Memory.csproj" />
1515
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
16+
<ProjectReference Include="$(LibrariesProjectRoot)System.Linq.Expressions\ref\System.Linq.Expressions.csproj" />
1617
</ItemGroup>
1718
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
1819
<Reference Include="System.Memory" />
1920
<Reference Include="System.Runtime" />
21+
<Reference Include="System.Linq.Expressions" />
2022
<Reference Include="netstandard" />
2123
</ItemGroup>
2224
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or

src/libraries/System.Text.Json/src/Resources/Strings.resx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,34 @@
560560
<data name="SerializerConverterFactoryReturnsJsonConverterFactory" xml:space="preserve">
561561
<value>The converter '{0}' cannot return an instance of JsonConverterFactory.</value>
562562
</data>
563+
<data name="NodeDynamicObjectResultNotAssignable" xml:space="preserve">
564+
<value>The result type '{0}' of the dynamic binding produced by the object with type '{1}' for the binder '{2}' is not compatible with the result type '{3}' expected by the call site.</value>
565+
</data>
566+
<data name="NodeElementWrongType" xml:space="preserve">
567+
<value>The element must be of type '{0}'</value>
568+
</data>
569+
<data name="NodeElementCannotBeObjectOrArray" xml:space="preserve">
570+
<value>The element cannot be an object or array.</value>
571+
</data>
572+
<data name="NodeAlreadyHasParent" xml:space="preserve">
573+
<value>The node already has a parent.</value>
574+
</data>
575+
<data name="NodeCycleDetected" xml:space="preserve">
576+
<value>A node cycle was detected.</value>
577+
</data>
578+
<data name="NodeUnableToConvert" xml:space="preserve">
579+
<value>A value of type '{0}' cannot be converted to a '{1}'.</value>
580+
</data>
581+
<data name="NodeUnableToConvertElement" xml:space="preserve">
582+
<value>An element of type '{0}' cannot be converted to a '{1}'.</value>
583+
</data>
584+
<data name="NodeValueNotAllowed" xml:space="preserve">
585+
<value>A JsonNode cannot be used as a value.</value>
586+
</data>
587+
<data name="NodeWrongType" xml:space="preserve">
588+
<value>The node must be of type '{0}'.</value>
589+
</data>
590+
<data name="ValueCannotBeNull" xml:space="preserve">
591+
<value>Value cannot be null. (Parameter '{0}')</value>
592+
</data>
563593
</root>

src/libraries/System.Text.Json/src/System.Text.Json.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
<Compile Include="System\Text\Json\JsonHelpers.Date.cs" />
4545
<Compile Include="System\Text\Json\JsonHelpers.Escaping.cs" />
4646
<Compile Include="System\Text\Json\JsonTokenType.cs" />
47+
<Compile Include="System\Text\Json\Node\JsonArray.cs" />
48+
<Compile Include="System\Text\Json\Node\JsonArray.IList.cs" />
49+
<Compile Include="System\Text\Json\Node\JsonNode.cs" />
50+
<Compile Include="System\Text\Json\Node\JsonNode.Dynamic.cs" />
51+
<Compile Include="System\Text\Json\Node\JsonNode.Operators.cs" />
52+
<Compile Include="System\Text\Json\Node\JsonNode.Parse.cs" />
53+
<Compile Include="System\Text\Json\Node\JsonNode.To.cs" />
54+
<Compile Include="System\Text\Json\Node\JsonNodeOptions.cs" />
55+
<Compile Include="System\Text\Json\Node\JsonObject.cs" />
56+
<Compile Include="System\Text\Json\Node\JsonObject.Dynamic.cs" />
57+
<Compile Include="System\Text\Json\Node\JsonObject.IDictionary.cs" />
58+
<Compile Include="System\Text\Json\Node\JsonValue.cs" />
59+
<Compile Include="System\Text\Json\Node\JsonValueOfT.cs" />
60+
<Compile Include="System\Text\Json\Node\MetaDynamic.cs" />
4761
<Compile Include="System\Text\Json\Reader\ConsumeNumberResult.cs" />
4862
<Compile Include="System\Text\Json\Reader\ConsumeTokenResult.cs" />
4963
<Compile Include="System\Text\Json\Reader\JsonReaderException.cs" />
@@ -95,6 +109,11 @@
95109
<Compile Include="System\Text\Json\Serialization\Converters\Collection\ListOfTConverter.cs" />
96110
<Compile Include="System\Text\Json\Serialization\Converters\Collection\QueueOfTConverter.cs" />
97111
<Compile Include="System\Text\Json\Serialization\Converters\Collection\StackOfTConverter.cs" />
112+
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonArrayConverter.cs" />
113+
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonNodeConverter.cs" />
114+
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonNodeConverterFactory.cs" />
115+
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonObjectConverter.cs" />
116+
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonValueConverter.cs" />
98117
<Compile Include="System\Text\Json\Serialization\Converters\Object\JsonObjectConverter.cs" />
99118
<Compile Include="System\Text\Json\Serialization\Converters\Object\KeyValuePairConverter.cs" />
100119
<Compile Include="System\Text\Json\Serialization\Converters\Object\ObjectConverterFactory.cs" />
@@ -169,6 +188,7 @@
169188
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs" />
170189
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
171190
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
191+
<Compile Include="System\Text\Json\Serialization\JsonUnknownTypeHandling.cs" />
172192
<Compile Include="System\Text\Json\Serialization\MemberAccessor.cs" />
173193
<Compile Include="System\Text\Json\Serialization\MetadataPropertyName.cs" />
174194
<Compile Include="System\Text\Json\Serialization\ParameterRef.cs" />
@@ -189,6 +209,7 @@
189209
<Compile Include="System\Text\Json\Serialization\WriteStack.cs" />
190210
<Compile Include="System\Text\Json\Serialization\WriteStackFrame.cs" />
191211
<Compile Include="System\Text\Json\ThrowHelper.cs" />
212+
<Compile Include="System\Text\Json\ThrowHelper.Node.cs" />
192213
<Compile Include="System\Text\Json\ThrowHelper.Serialization.cs" />
193214
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.cs" />
194215
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.Date.cs" />
@@ -259,8 +280,12 @@
259280
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
260281
<Reference Include="System.Diagnostics.Tools" />
261282
</ItemGroup>
283+
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)' or '$(TargetFramework)' == 'netcoreapp3.0'">
284+
<Reference Include="System.Linq.Expressions" />
285+
</ItemGroup>
262286
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or $(TargetFramework.StartsWith('net4'))">
263287
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
288+
<PackageReference Include="System.Linq.Expressions" Version="$(SystemLinqExpressionsVersion)" />
264289
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
265290
<PackageReference Include="System.Numerics.Vectors" Version="$(SystemNumericsVectorsVersion)" />
266291
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,39 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options =
137137
}
138138
}
139139

140+
internal static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions options)
141+
{
142+
Debug.Assert(utf8Json != null);
143+
144+
ArraySegment<byte> drained = ReadToEnd(utf8Json);
145+
Debug.Assert(drained.Array != null);
146+
147+
byte[] owned = new byte[drained.Count];
148+
Buffer.BlockCopy(drained.Array, 0, owned, 0, drained.Count);
149+
150+
// Holds document content, clear it before returning it.
151+
drained.AsSpan().Clear();
152+
ArrayPool<byte>.Shared.Return(drained.Array);
153+
154+
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
155+
}
156+
157+
internal static JsonDocument ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumentOptions options)
158+
{
159+
Debug.Assert(utf8Json != null);
160+
161+
byte[] owned = new byte[utf8Json.Length];
162+
utf8Json.CopyTo(owned);
163+
164+
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
165+
}
166+
167+
internal static JsonDocument ParseValue(string json, JsonDocumentOptions options)
168+
{
169+
Debug.Assert(json != null);
170+
return ParseValue(json.AsMemory(), options);
171+
}
172+
140173
/// <summary>
141174
/// Parse a <see cref="Stream"/> as UTF-8-encoded data representing a single JSON value into a
142175
/// JsonDocument. The Stream will be read to completion.
@@ -230,6 +263,31 @@ public static JsonDocument Parse(ReadOnlyMemory<char> json, JsonDocumentOptions
230263
}
231264
}
232265

266+
internal static JsonDocument ParseValue(ReadOnlyMemory<char> json, JsonDocumentOptions options)
267+
{
268+
ReadOnlySpan<char> jsonChars = json.Span;
269+
int expectedByteCount = JsonReaderHelper.GetUtf8ByteCount(jsonChars);
270+
byte[] owned;
271+
byte[] utf8Bytes = ArrayPool<byte>.Shared.Rent(expectedByteCount);
272+
273+
try
274+
{
275+
int actualByteCount = JsonReaderHelper.GetUtf8FromText(jsonChars, utf8Bytes);
276+
Debug.Assert(expectedByteCount == actualByteCount);
277+
278+
owned = new byte[actualByteCount];
279+
Buffer.BlockCopy(utf8Bytes, 0, owned, 0, actualByteCount);
280+
}
281+
finally
282+
{
283+
// Holds document content, clear it before returning it.
284+
utf8Bytes.AsSpan(0, expectedByteCount).Clear();
285+
ArrayPool<byte>.Shared.Return(utf8Bytes);
286+
}
287+
288+
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
289+
}
290+
233291
/// <summary>
234292
/// Parse text representing a single JSON value into a JsonDocument.
235293
/// </summary>
@@ -639,7 +697,7 @@ private static JsonDocument Parse(
639697
private static JsonDocument ParseUnrented(
640698
ReadOnlyMemory<byte> utf8Json,
641699
JsonReaderOptions readerOptions,
642-
JsonTokenType tokenType)
700+
JsonTokenType tokenType = JsonTokenType.None)
643701
{
644702
// These tokens should already have been processed.
645703
Debug.Assert(

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.Parse.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.IO;
67

78
namespace System.Text.Json
89
{
@@ -52,6 +53,24 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader)
5253
return document.RootElement;
5354
}
5455

56+
internal static JsonElement ParseValue(Stream utf8Json, JsonDocumentOptions options)
57+
{
58+
JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
59+
return document.RootElement;
60+
}
61+
62+
internal static JsonElement ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumentOptions options)
63+
{
64+
JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
65+
return document.RootElement;
66+
}
67+
68+
internal static JsonElement ParseValue(string json, JsonDocumentOptions options)
69+
{
70+
JsonDocument document = JsonDocument.ParseValue(json, options);
71+
return document.RootElement;
72+
}
73+
5574
/// <summary>
5675
/// Attempts to parse one JSON value (including objects or arrays) from the provided reader.
5776
/// </summary>

src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Buffers;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Runtime.CompilerServices;
89
using System.Text.Json.Serialization;
910

@@ -14,6 +15,12 @@ internal static partial class JsonHelpers
1415
// Copy of Array.MaxArrayLength. For byte arrays the limit is slightly larger
1516
private const int MaxArrayLength = 0X7FEFFFFF;
1617

18+
// Members accessed by the serializer when deserializing.
19+
public const DynamicallyAccessedMemberTypes MembersAccessedOnRead =
20+
DynamicallyAccessedMemberTypes.PublicConstructors |
21+
DynamicallyAccessedMemberTypes.PublicProperties |
22+
DynamicallyAccessedMemberTypes.PublicFields;
23+
1724
/// <summary>
1825
/// Returns the span for the given reader.
1926
/// </summary>

0 commit comments

Comments
 (0)