-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
JsonSerializer.Write.Node.cs
169 lines (154 loc) · 7.77 KB
/
JsonSerializer.Write.Node.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json
{
public static partial class JsonSerializer
{
/// <summary>
/// Converts the provided value into a <see cref="JsonNode"/>.
/// </summary>
/// <typeparam name="TValue">The type of the value to serialize.</typeparam>
/// <returns>A <see cref="JsonNode"/> representation of the JSON value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="options">Options to control the conversion behavior.</param>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
public static JsonNode? SerializeToNode<TValue>(TValue value, JsonSerializerOptions? options = null)
{
JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
return WriteNode(value, jsonTypeInfo);
}
/// <summary>
/// Converts the provided value into a <see cref="JsonNode"/>.
/// </summary>
/// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
/// <param name="options">Options to control the conversion behavior.</param>
/// <exception cref="ArgumentException">
/// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
/// </exception>
/// <exception cref="NotSupportedException">
/// <exception cref="ArgumentNullException">
/// <paramref name="inputType"/> is <see langword="null"/>.
/// </exception>
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerOptions? options = null)
{
ValidateInputType(value, inputType);
JsonTypeInfo typeInfo = GetTypeInfo(options, inputType);
return WriteNodeAsObject(value, typeInfo);
}
/// <summary>
/// Converts the provided value into a <see cref="JsonNode"/>.
/// </summary>
/// <typeparam name="TValue">The type of the value to serialize.</typeparam>
/// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
/// </exception>
public static JsonNode? SerializeToNode<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
{
if (jsonTypeInfo is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
}
jsonTypeInfo.EnsureConfigured();
return WriteNode(value, jsonTypeInfo);
}
/// <summary>
/// Converts the provided value into a <see cref="JsonNode"/>.
/// </summary>
/// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidCastException">
/// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
/// </exception>
public static JsonNode? SerializeToNode(object? value, JsonTypeInfo jsonTypeInfo)
{
if (jsonTypeInfo is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
}
jsonTypeInfo.EnsureConfigured();
return WriteNodeAsObject(value, jsonTypeInfo);
}
/// <summary>
/// Converts the provided value into a <see cref="JsonNode"/>.
/// </summary>
/// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
/// <param name="context">A metadata provider for serializable types.</param>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
/// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="inputType"/> or <paramref name="context"/> is <see langword="null"/>.
/// </exception>
public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerContext context)
{
if (context is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(context));
}
ValidateInputType(value, inputType);
JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
return WriteNodeAsObject(value, jsonTypeInfo);
}
private static JsonNode? WriteNode<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
{
Debug.Assert(jsonTypeInfo.IsConfigured);
JsonSerializerOptions options = jsonTypeInfo.Options;
Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWriter output);
try
{
jsonTypeInfo.Serialize(writer, value);
return JsonNode.Parse(output.WrittenMemory.Span, options.GetNodeOptions(), options.GetDocumentOptions());
}
finally
{
Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
}
}
private static JsonNode? WriteNodeAsObject(object? value, JsonTypeInfo jsonTypeInfo)
{
Debug.Assert(jsonTypeInfo.IsConfigured);
JsonSerializerOptions options = jsonTypeInfo.Options;
Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWriter output);
try
{
jsonTypeInfo.SerializeAsObject(writer, value);
return JsonNode.Parse(output.WrittenMemory.Span, options.GetNodeOptions(), options.GetDocumentOptions());
}
finally
{
Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
}
}
}
}