< Summary

Line coverage
9%
Covered lines: 6
Uncovered lines: 57
Coverable lines: 63
Total lines: 123
Line coverage: 9.5%
Branch coverage
9%
Covered branches: 2
Total branches: 22
Branch coverage: 9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ConfigureJsonTypeInfo(...)100%11100%
ReadElementAndSetProperty(...)0%660%
WriteExtensionDataValue(...)100%110%
Write(...)0%220%
Read(...)33.33%6637.5%
ReadAsJsonElement(...)100%110%
ReadAsJsonNode(...)0%880%
GetSupportedJsonValueTypes(...)100%110%
GetSchema(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Node/JsonObjectConverter.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Text.Json.Nodes;
 6using System.Text.Json.Schema;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    internal sealed class JsonObjectConverter : JsonConverter<JsonObject?>
 12    {
 13        internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
 8614        {
 8615            jsonTypeInfo.CreateObjectForExtensionDataProperty = () => new JsonObject(options.GetNodeOptions());
 8616        }
 17
 18        internal override void ReadElementAndSetProperty(
 19            object obj,
 20            string propertyName,
 21            ref Utf8JsonReader reader,
 22            JsonSerializerOptions options,
 23            scoped ref ReadStack state)
 024        {
 025            bool success = JsonNodeConverter.Instance.TryRead(ref reader, typeof(JsonNode), options, ref state, out Json
 026            Debug.Assert(success); // Node converters are not resumable.
 27
 028            Debug.Assert(obj is JsonObject);
 029            JsonObject jObject = (JsonObject)obj;
 30
 031            Debug.Assert(value is null || value is JsonNode);
 032            JsonNode? jNodeValue = value;
 33
 034            if (options.AllowDuplicateProperties)
 035            {
 036                jObject[propertyName] = jNodeValue;
 037            }
 038            else if (!jObject.TryAdd(propertyName, jNodeValue))
 039            {
 040                ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(propertyName);
 41            }
 042        }
 43
 44        internal override void WriteExtensionDataValue(Utf8JsonWriter writer, JsonObject? value, JsonSerializerOptions o
 045        {
 046            Debug.Assert(value is not null);
 047            value.WriteContentsTo(writer, options);
 048        }
 49
 50        public override void Write(Utf8JsonWriter writer, JsonObject? value, JsonSerializerOptions options)
 051        {
 052            if (value is null)
 053            {
 054                writer.WriteNullValue();
 055                return;
 56            }
 57
 058            value.WriteTo(writer, options);
 059        }
 60
 61        public override JsonObject? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 1262        {
 1263            switch (reader.TokenType)
 64            {
 65                case JsonTokenType.StartObject:
 066                    return options.AllowDuplicateProperties
 067                        ? ReadAsJsonElement(ref reader, options.GetNodeOptions())
 068                        : ReadAsJsonNode(ref reader, options.GetNodeOptions());
 69                case JsonTokenType.Null:
 070                    return null;
 71                default:
 1272                    throw ThrowHelper.GetInvalidOperationException_ExpectedObject(reader.TokenType);
 73            }
 074        }
 75
 76        internal static JsonObject ReadAsJsonElement(ref Utf8JsonReader reader, JsonNodeOptions options)
 077        {
 078            JsonElement jElement = JsonElement.ParseValue(ref reader);
 079            return new JsonObject(jElement, options);
 080        }
 81
 82        internal static JsonObject ReadAsJsonNode(ref Utf8JsonReader reader, JsonNodeOptions options)
 083        {
 084            Debug.Assert(reader.TokenType == JsonTokenType.StartObject);
 85
 086            JsonObject jObject = new JsonObject(options);
 87
 088            while (reader.Read())
 089            {
 090                if (reader.TokenType == JsonTokenType.EndObject)
 091                {
 092                    return jObject;
 93                }
 94
 095                if (reader.TokenType != JsonTokenType.PropertyName)
 096                {
 97                    // JSON is invalid so reader would have already thrown.
 098                    Debug.Fail("Property name expected.");
 99                    ThrowHelper.ThrowJsonException();
 100                }
 101
 0102                string propertyName = reader.GetString()!;
 0103                reader.Read(); // Move to the value token.
 0104                JsonNode? value = JsonNodeConverter.ReadAsJsonNode(ref reader, options);
 105
 0106                if (!jObject.TryAdd(propertyName, value))
 0107                {
 0108                    ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(propertyName);
 109                }
 0110            }
 111
 112            // JSON is invalid so reader would have already thrown.
 0113            Debug.Fail("End object token not found.");
 114            ThrowHelper.ThrowJsonException();
 115            return null;
 0116        }
 117
 0118        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.Object;
 119
 0120        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.Object };
 121    }
 122}
 123