Пример #1
0
        public override TMessage?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var message = new TMessage();

            JsonConverterHelper.PopulateList(ref reader, options, message, message.Descriptor.Fields[ListValue.ValuesFieldNumber]);

            return(message);
        }
Пример #2
0
        public override TMessage Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options)
        {
            var message = new TMessage();

            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new InvalidOperationException($"Unexpected JSON token: {reader.TokenType}");
            }

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonTokenType.EndObject:
                    return(message);

                case JsonTokenType.PropertyName:
                    if (_jsonFieldMap.TryGetValue(reader.GetString() !, out var fieldDescriptor))
                    {
                        if (fieldDescriptor.ContainingOneof != null)
                        {
                            if (fieldDescriptor.ContainingOneof.Accessor.GetCaseFieldDescriptor(message) != null)
                            {
                                throw new InvalidOperationException($"Multiple values specified for oneof {fieldDescriptor.ContainingOneof.Name}");
                            }
                        }

                        if (fieldDescriptor.IsMap)
                        {
                            JsonConverterHelper.PopulateMap(ref reader, options, message, fieldDescriptor);
                        }
                        else if (fieldDescriptor.IsRepeated)
                        {
                            JsonConverterHelper.PopulateList(ref reader, options, message, fieldDescriptor);
                        }
                        else
                        {
                            var fieldType     = JsonConverterHelper.GetFieldType(fieldDescriptor);
                            var propertyValue = JsonSerializer.Deserialize(ref reader, fieldType, options);
                            fieldDescriptor.Accessor.SetValue(message, propertyValue);
                        }
                    }
                    else
                    {
                        reader.Skip();
                    }
                    break;