| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Nodes |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// A JsonValue that encapsulates arbitrary .NET type configurations. |
| | | 11 | | /// Paradoxically, instances of this type can be of any JsonValueKind |
| | | 12 | | /// (including objects and arrays) and introspecting these values is |
| | | 13 | | /// generally slower compared to the other JsonValue implementations. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class JsonValueCustomized<TValue> : JsonValue<TValue> |
| | | 16 | | { |
| | | 17 | | private readonly JsonTypeInfo<TValue> _jsonTypeInfo; |
| | | 18 | | private JsonValueKind? _valueKind; |
| | | 19 | | |
| | 0 | 20 | | public JsonValueCustomized(TValue value, JsonTypeInfo<TValue> jsonTypeInfo, JsonNodeOptions? options = null): ba |
| | 0 | 21 | | { |
| | 0 | 22 | | Debug.Assert(jsonTypeInfo.IsConfigured); |
| | 0 | 23 | | _jsonTypeInfo = jsonTypeInfo; |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | private protected override JsonValueKind GetValueKindCore() => _valueKind ??= ComputeValueKind(); |
| | 0 | 27 | | internal override JsonNode DeepCloneCore() => JsonSerializer.SerializeToNode(Value, _jsonTypeInfo)!; |
| | | 28 | | |
| | | 29 | | public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null) |
| | 0 | 30 | | { |
| | 0 | 31 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 32 | | |
| | 0 | 33 | | JsonTypeInfo<TValue> jsonTypeInfo = _jsonTypeInfo; |
| | | 34 | | |
| | 0 | 35 | | if (options != null && options != jsonTypeInfo.Options) |
| | 0 | 36 | | { |
| | 0 | 37 | | options.MakeReadOnly(); |
| | 0 | 38 | | jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfoInternal(typeof(TValue)); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | jsonTypeInfo.Serialize(writer, Value); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Computes the JsonValueKind of the value by serializing it and reading the resultant JSON. |
| | | 46 | | /// </summary> |
| | | 47 | | private JsonValueKind ComputeValueKind() |
| | 0 | 48 | | { |
| | 0 | 49 | | Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(options: default, JsonSerializerOptions.Buff |
| | | 50 | | try |
| | 0 | 51 | | { |
| | 0 | 52 | | WriteTo(writer); |
| | 0 | 53 | | writer.Flush(); |
| | 0 | 54 | | Utf8JsonReader reader = new(output.WrittenSpan); |
| | 0 | 55 | | bool success = reader.Read(); |
| | 0 | 56 | | Debug.Assert(success); |
| | 0 | 57 | | return JsonReaderHelper.ToValueKind(reader.TokenType); |
| | | 58 | | } |
| | | 59 | | finally |
| | 0 | 60 | | { |
| | 0 | 61 | | Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output); |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |