コード例 #1
0
        private JsonObject ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            DuplicatePropertyNameHandling duplicatePropertyNameHandling = options.GetDuplicatePropertyNameHandling();

            JsonObject obj = new JsonObject();

            while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
            {
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                string?propertyName = reader.GetString();

                if (propertyName == null)
                {
                    throw new JsonException("Property name cannot be null");
                }

                reader.Read();
                JsonNode propertyValue = Read(ref reader, typeof(JsonNode), options);

                switch (duplicatePropertyNameHandling)
                {
                case DuplicatePropertyNameHandling.Replace:
                    obj[propertyName] = propertyValue;
                    break;

                case DuplicatePropertyNameHandling.Ignore:
                    if (!obj.ContainsProperty(propertyName))
                    {
                        obj.Add(propertyName, propertyValue);
                    }
                    break;

                case DuplicatePropertyNameHandling.Error:
                    if (!obj.ContainsProperty(propertyName))
                    {
                        obj.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        throw new JsonException($"Duplicate property '{propertyName}'");
                    }
                    break;
                }
            }

            return(obj);
        }