forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonSerializer.Write.Utf8JsonWriter.cs
More file actions
77 lines (69 loc) · 3.24 KB
/
JsonSerializer.Write.Utf8JsonWriter.cs
File metadata and controls
77 lines (69 loc) · 3.24 KB
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
// 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.
using System.Text.Json.Serialization;
using System.Diagnostics;
namespace System.Text.Json
{
public static partial class JsonSerializer
{
/// <summary>
/// Write one JSON value (including objects or arrays) to the provided writer.
/// </summary>
/// <param name="writer">The writer to write.</param>
/// <param name="value">The value to convert and write.</param>
/// <param name="options">Options to control the behavior.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="writer"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions? options = null)
{
Serialize<TValue>(writer, value, typeof(TValue), options);
}
/// <summary>
/// Write one JSON value (including objects or arrays) to the provided writer.
/// </summary>
/// <param name="writer"></param>
/// <param name="value">The value to convert and write.</param>
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
/// <param name="options">Options to control the behavior.</param>
/// <exception cref="ArgumentException">
/// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="writer"/> or <paramref name="inputType"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
public static void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerOptions? options = null)
{
if (inputType == null)
{
throw new ArgumentNullException(nameof(inputType));
}
if (value != null && !inputType.IsAssignableFrom(value.GetType()))
{
ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value);
}
Serialize<object?>(writer, value, inputType, options);
}
private static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, Type type, JsonSerializerOptions? options)
{
if (options == null)
{
options = JsonSerializerOptions.s_defaultOptions;
}
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
WriteCore<TValue>(writer, value, type, options);
}
}
}