Пример #1
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()));
        }
Пример #2
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);
        }
Пример #3
0
        public T GetArgument <T>(string argumentName)
        {
            if (string.IsNullOrEmpty(argumentName))
            {
                throw new ArgumentNullException(nameof(argumentName));
            }

            Dictionary <string, ArgumentNode> arguments = GetArguments();

            if (arguments.TryGetValue(argumentName, out ArgumentNode argValue) &&
                Type.Arguments.TryGetField(argumentName, out InputField arg))
            {
                if (typeof(T).IsAssignableFrom(arg.Type.ClrType))
                {
                    return((T)arg.Type.ParseLiteral(argValue.Value));
                }
                else
                {
                    return(ValueDeserializer
                           .ParseLiteral <T>(arg.Type, argValue.Value));
                }
            }

            throw new ArgumentException(
                      "The argument name is invalid.",
                      nameof(argumentName));
        }
            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);
        }
Пример #7
0
        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);
        }
        public void ParseScalarNullValue()
        {
            // arrange
            var           sourceType = new IntType();
            var           targetType = typeof(int);
            NullValueNode literal    = NullValueNode.Default;

            // act
            object result = ValueDeserializer
                            .ParseLiteral(sourceType, targetType, literal);

            // assert
            Assert.Null(result);
        }
Пример #9
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);
        }
Пример #10
0
        private void SetProperty(InputField argument, object obj, PropertyInfo property)
        {
            Dictionary <string, ArgumentNode> arguments = GetArguments();

            if (arguments.TryGetValue(argument.Name,
                                      out ArgumentNode argumentValue))
            {
                object parsedValue = ValueDeserializer.ParseLiteral(
                    argument.Type, property.PropertyType, argumentValue.Value);

                ValueDeserializer.SetProperty(
                    property, argument.Type.IsListType(),
                    obj, parsedValue);
            }
        }
        public void ParseScalarListToArray()
        {
            // arrange
            var sourceType = new ListType(new IntType());
            var targetType = typeof(int[]);
            var literal    = new ListValueNode(new IntValueNode("1"));

            // act
            object result = ValueDeserializer
                            .ParseLiteral(sourceType, targetType, literal);

            // assert
            Assert.IsType <int[]>(result);
            Assert.Collection((int[])result, t => Assert.Equal(1, t));
        }
        public void ParseNonNullScalar()
        {
            // arrange
            var          sourceType = new NonNullType(new IntType());
            var          targetType = typeof(int);
            IntValueNode literal    = new IntValueNode("1");

            // act
            object result = ValueDeserializer
                            .ParseLiteral(sourceType, targetType, literal);

            // assert
            Assert.IsType <int>(result);
            Assert.Equal(1, result);
        }
        public void ParseScalarAndConvert()
        {
            // arrange
            var sourceType = new IntType();
            var targetType = typeof(string);
            var literal    = new IntValueNode("1");

            // act
            object result = ValueDeserializer
                            .ParseLiteral(sourceType, targetType, literal);

            // assert
            Assert.IsType <string>(result);
            Assert.Equal("1", result);
        }
        public void ParseAndMapObject()
        {
            // arrange
            ISchema schema     = CreateSchema();
            var     sourceType = schema.GetType <INamedInputType>("FooInput");
            var     targetType = typeof(FooOnlyBar1);
            var     literal    = CreateFoo();

            // act
            FooOnlyBar1 result = ValueDeserializer
                                 .ParseLiteral(sourceType, targetType, literal) as FooOnlyBar1;

            // assert
            Assert.NotNull(result);
            Assert.Equal("123", result.Bar1);
        }
        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);
        }
        public void ParseObjectGraph()
        {
            // arrange
            ISchema schema     = CreateSchema();
            var     sourceType = schema.GetType <INamedInputType>("BarInput");
            var     targetType = typeof(Bar);
            var     literal    = CreateBar();

            // act
            Bar result = ValueDeserializer
                         .ParseLiteral(sourceType, targetType, literal)
                         as Bar;

            // assert
            Assert.NotNull(result);
            Assert.NotNull(result.Foo);
            Assert.Equal("123", result.Foo.Bar1);
            Assert.Equal("456", result.Foo.Bar2);
        }
        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 void ParseAndMapObjectGraphWithList()
        {
            // arrange
            ISchema schema     = CreateSchema();
            var     sourceType = schema.GetType <INamedInputType>(
                "BarWithArrayInput");
            var targetType = typeof(BarWithListOnlyGet);
            var literal    = CreateBarWithArray();

            // act
            BarWithListOnlyGet result = ValueDeserializer
                                        .ParseLiteral(sourceType, targetType, literal)
                                        as BarWithListOnlyGet;

            // assert
            Assert.NotNull(result);
            Assert.NotNull(result.Foos);
            Assert.Collection(result.Foos,
                              t =>
            {
                Assert.Equal("123", t.Bar1);
                Assert.Equal("456", t.Bar2);
            });
        }
 public object DeserializeMap_Attributed() =>
 ValueDeserializer.DeserializeMap(FakeDb, SampleData.SerializedMap, typeof(HighScore));
 public object DeserializeMap_Expando() =>
 ValueDeserializer.DeserializeMap(FakeDb, SampleData.SerializedMap, typeof(ExpandoObject));
 public object DeserializeMap_Dictionary() =>
 ValueDeserializer.DeserializeMap(FakeDb, SampleData.SerializedMap, typeof(Dictionary <string, object>));
 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));
Пример #26
0
 public TooltipsRegistry(HttpClient client, ValueDeserializer deserializer)
 {
     _client       = client;
     _deserializer = deserializer;
 }