示例#1
0
        public void DeserializeCustomPropertyConversion_ConverterRegistry()
        {
            Guid guid1 = Guid.NewGuid();
            Guid guid2 = Guid.NewGuid();
            var  value = new Value
            {
                MapValue = new MapValue
                {
                    Fields =
                    {
                        { "Name",       new Value {
                              StringValue = "test"
                          } },
                        { "Guid",       new Value {
                              StringValue = guid1.ToString("N")
                          } },
                        { "GuidOrNull", new Value {
                              StringValue = guid2.ToString("N")
                          } },
                    }
                }
            };
            var registry = new ConverterRegistry()
            {
                new SerializationTestData.GuidConverter()
            };
            var db       = FirestoreDb.Create("proj", "db", new FakeFirestoreClient(), converterRegistry: registry);
            var snapshot = GetSampleSnapshot(db, "doc1");
            var context  = new DeserializationContext(snapshot);
            var pair     = (SerializationTestData.GuidPair2)ValueDeserializer.Deserialize(context, value, typeof(SerializationTestData.GuidPair2));

            Assert.Equal("test", pair.Name);
            Assert.Equal(guid1, pair.Guid);
            Assert.Equal(guid2, pair.GuidOrNull);
        }
示例#2
0
        public void DeserializeToLinqResult_Fails()
        {
            var sequence = Enumerable.Range(1, 3);
            var value    = ValueSerializer.Serialize(SerializationContext.Default, sequence);

            Assert.Throws <NotSupportedException>(() => ValueDeserializer.Deserialize(SerializationTestData.Context, value, sequence.GetType()));
        }
        protected virtual void TranslateConstant(StringBuilder builder, object value, DbType type)
        {
            var name  = "@" + Index++;
            var param = _deserializer.Deserialize(value, type);

            Parameters.Add(name, param);
            _builder.Append(name);
        }
            internal void SetValue(FirestoreDb db, Value value, object target)
            {
                object converted =
                    _converter == null?ValueDeserializer.Deserialize(db, value, _propertyInfo.PropertyType)
                        : value.ValueTypeCase == Value.ValueTypeOneofCase.NullValue ? null
                    : _converter.DeserializeValue(db, value);

                _propertyInfo.SetValue(target, converted);
            }
示例#5
0
        protected override object DeserializeArray(FirestoreDb db, RepeatedField <Value> values)
        {
            Array array = Array.CreateInstance(_elementType, values.Count);

            for (int i = 0; i < values.Count; i++)
            {
                var converted = ValueDeserializer.Deserialize(db, values[i], _elementType);
                array.SetValue(converted, i);
            }
            return(array);
        }
        public override object DeserializeMap(FirestoreDb db, IDictionary <string, Value> values)
        {
            // TODO: Compile an expression tree on construction, or at least accept an optional delegate for construction
            // (allowing for special-casing of Dictionary<string, object>).
            var ret = (IDictionary <string, TValue>)Activator.CreateInstance(_concreteType);

            foreach (var pair in values)
            {
                ret.Add(pair.Key, (TValue)ValueDeserializer.Deserialize(db, pair.Value, typeof(TValue)));
            }
            return(ret);
        }
        protected override object DeserializeArray(DeserializationContext context, RepeatedField <Value> values)
        {
            // TODO: See if using a compiled expression tree is faster.
            var list = (IList)Activator.CreateInstance(TargetType);

            foreach (var value in values)
            {
                var deserialized = ValueDeserializer.Deserialize(context, value, _elementType);
                list.Add(deserialized);
            }
            return(list);
        }
示例#8
0
        public override object DeserializeMap(DeserializationContext context, IDictionary <string, Value> values)
        {
            // TODO: What if the keys in the map don't match our names, or there are spare/missing ones?
            var accessor = CreateAccessor(Activator.CreateInstance(TargetType));

            for (int i = 0; i < _names.Count; i++)
            {
                if (values.TryGetValue(_names[i], out var value))
                {
                    accessor[i] = ValueDeserializer.Deserialize(context, value, _elementTypes[i]);
                }
            }
            return(accessor.Value);
        }
        public void DeserializeToExpando()
        {
            var value = new Value {
                MapValue = new MapValue {
                    Fields = { { "name",  new Value {
                                     StringValue = "Jon"
                                 } },{ "score", new Value {
                                     IntegerValue = 10L
                                 } } }
                }
            };
            dynamic result = ValueDeserializer.Deserialize(SerializationTestData.Context, value, typeof(ExpandoObject));

            Assert.Equal("Jon", result.name);
            Assert.Equal(10L, result.score);
        }
示例#10
0
        public async Task Fetch()
        {
            if (Tooltips != null)
            {
                return;
            }

            Tooltips = await _client.GetJsonAsync <List <Tooltip> >("api/tooltips");

            foreach (var tooltip in Tooltips)
            {
                for (var i = 0; i < tooltip.Items.Count; i++)
                {
                    tooltip.Items[i] = _deserializer.Deserialize(tooltip.Items[i], tooltip.Type);
                }
            }
        }
        public void DeserializeToSpecificDictionary()
        {
            var value = new Value {
                MapValue = new MapValue {
                    Fields = { { "x", new Value {
                                     IntegerValue = 10L
                                 } },{ "y", new Value {
                                     IntegerValue = 20L
                                 } } }
                }
            };
            var result = ValueDeserializer.Deserialize(SerializationTestData.Context, value, typeof(Dictionary <string, int>));

            Assert.IsType <Dictionary <string, int> >(result);
            var expected = new Dictionary <string, int>
            {
                ["x"] = 10,
                ["y"] = 20
            };

            Assert.Equal(expected, result);
        }
        public void DeserializeToObjectDictionary()
        {
            var value = new Value {
                MapValue = new MapValue {
                    Fields = { { "name",  new Value {
                                     StringValue = "Jon"
                                 } },{ "score", new Value {
                                     IntegerValue = 10L
                                 } } }
                }
            };
            var result = ValueDeserializer.Deserialize(SerializationTestData.Context, value, typeof(object));

            Assert.IsType <Dictionary <string, object> >(result);
            var expected = new Dictionary <string, object>
            {
                ["name"]  = "Jon",
                ["score"] = 10L
            };

            Assert.Equal(expected, result);
        }
 public object Deserialize_Expando() =>
 ValueDeserializer.Deserialize(Context, SampleData.Serialized, typeof(ExpandoObject));
 public object Deserialize_Dictionary() =>
 ValueDeserializer.Deserialize(Context, SampleData.Serialized, typeof(Dictionary <string, object>));
 public object Deserialize_Attributed() =>
 ValueDeserializer.Deserialize(Context, SampleData.Serialized, typeof(HighScore));