public TResponse Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var response       = new TResponse();
            var dictionary     = new Dictionary <TKey, TValue>();
            var count          = 0;
            var keyFormatter   = formatterResolver.GetFormatter <TKey>();
            var valueFormatter = formatterResolver.GetFormatter <TValue>();

            while (reader.ReadIsInObject(ref count))
            {
                var property = reader.ReadPropertyNameSegmentRaw();
                if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
                {
                    switch (errorValue)
                    {
                    case 0:
                        if (reader.GetCurrentJsonToken() == JsonToken.String)
                        {
                            response.Error = new Error {
                                Reason = reader.ReadString()
                            }
                        }
                        ;
                        else
                        {
                            var formatter = formatterResolver.GetFormatter <Error>();
                            response.Error = formatter.Deserialize(ref reader, formatterResolver);
                        }
                        break;

                    case 1:
                        if (reader.GetCurrentJsonToken() == JsonToken.Number)
                        {
                            response.StatusCode = reader.ReadInt32();
                        }
                        else
                        {
                            reader.ReadNextBlock();
                        }
                        break;
                    }
                }
                else
                {
                    // include opening string quote in reader (offset - 1)
                    var propertyReader = new JsonReader(property.Array, property.Offset - 1);
                    var key            = keyFormatter.Deserialize(ref propertyReader, formatterResolver);
                    var value          = valueFormatter.Deserialize(ref reader, formatterResolver);
                    dictionary.Add(key, value);
                }
            }

            var settings             = formatterResolver.GetConnectionSettings();
            var resolvableDictionary = new ResolvableDictionaryProxy <TKey, TValue>(settings, dictionary);

            response.BackingDictionary = resolvableDictionary;
            return(response);
        }
示例#2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var j = DictionaryResponseJsonConverterHelpers.ReadServerErrorFirst(reader, out var error, out var statusCode);

            var response = new TResponse();
            var d        = new Dictionary <TKey, TValue>();

            serializer.Populate(j.CreateReader(), d);
            var settings = serializer.GetConnectionSettings();
            var dict     = new ResolvableDictionaryProxy <TKey, TValue>(settings, d);

            response.BackingDictionary = dict;
            response.Error             = error;
            response.StatusCode        = statusCode;
            return(response);
        }
        public IReadOnlyDictionary <Field, IFieldMapping> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var fieldMappings = new Dictionary <Field, IFieldMapping>();

            if (reader.GetCurrentJsonToken() != JsonToken.BeginObject)
            {
                reader.ReadNext();
                return(fieldMappings);
            }

            var           count   = 0;
            IFieldMapping mapping = null;

            while (reader.ReadIsInObject(ref count))
            {
                var propertyName = reader.ReadPropertyNameSegmentRaw();
                if (Fields.TryGetValue(propertyName, out var value))
                {
                    switch (value)
                    {
                    case 0:
#pragma warning disable 618
                        mapping = formatterResolver.GetFormatter <AllField>()
                                  .Deserialize(ref reader, formatterResolver);
#pragma warning restore 618
                        break;

                    case 1:
                        mapping = formatterResolver.GetFormatter <SourceField>()
                                  .Deserialize(ref reader, formatterResolver);
                        break;

                    case 2:
                        mapping = formatterResolver.GetFormatter <RoutingField>()
                                  .Deserialize(ref reader, formatterResolver);
                        break;

                    case 3:
#pragma warning disable 618
                        mapping = formatterResolver.GetFormatter <IndexField>()
                                  .Deserialize(ref reader, formatterResolver);
#pragma warning restore 618
                        break;

                    case 4:
                        mapping = formatterResolver.GetFormatter <SizeField>()
                                  .Deserialize(ref reader, formatterResolver);
                        break;
                    }
                }
                else
                {
                    var property = formatterResolver.GetFormatter <IProperty>()
                                   .Deserialize(ref reader, formatterResolver);

                    if (property != null)
                    {
                        property.Name =
                            propertyName.Utf8String();
                    }

                    mapping = property;
                }

                if (mapping != null)
                {
                    var name = propertyName.Utf8String();
                    fieldMappings.Add(name, mapping);
                }
            }

            var settings             = formatterResolver.GetConnectionSettings();
            var resolvableDictionary = new ResolvableDictionaryProxy <Field, IFieldMapping>(settings, fieldMappings);
            return(resolvableDictionary);
        }