public void TestSerialization()
        {
            var serializedODataEntity = @"{ a: 2, b: 'abc', c: { x: 2 } }";
            var entity = JsonConvert.DeserializeObject <ODataEntity>(serializedODataEntity);

            entity.Get <ODataEntity>("C").Get <int>("X").ShouldEqual(2);
            var @object = JsonConvert.DeserializeObject <ODataObject>(JsonConvert.SerializeObject(entity));

            ((ODataEntity)@object).Get <ODataEntity>("C").Get <int>("X").ShouldEqual(2);

            JsonConvert.DeserializeObject <ODataObject>("null").ShouldEqual(null);

            JsonConvert.DeserializeObject <ODataValue>("1").Value.ShouldEqual(1L);
            JsonConvert.SerializeObject(ODataValue.FromObject("abc")).ShouldEqual("\"abc\"");
        }
        public void TestFromObject()
        {
            var v1 = ODataValue.FromObject(1);

            v1.Value.ShouldEqual(1);

            var v2 = ODataValue.FromObject(null);

            v2.ShouldEqual(null);

            var v3 = ODataValue.FromObject(v1);

            ReferenceEquals(v1, v3).ShouldEqual(true);

            UnitTestHelpers.AssertThrows <ArgumentException>(() => ODataValue.FromObject(new { a = 2 }));
            UnitTestHelpers.AssertThrows <ArgumentException>(() => ODataValue.FromObject(new ODataEntity(Enumerable.Empty <KeyValuePair <string, object> >())));
        }
        public void TestGetValuesFromODataEntity()
        {
            var entity = new ODataEntity(new Dictionary <string, object>
            {
                { "A", null },
                { "B", 1 },
                { "C", new ODataEntity(new Dictionary <string, object> {
                        { "X", "abc" }
                    }) },
                { "D", ODataValue.FromObject(-1) },
                { "F", 1.5 },
            });

            entity.Get <string>("A").ShouldEqual(null);
            entity.Get <int>("b").ShouldEqual(1);
            entity.Get <int?>("b").ShouldEqual(1);
            entity.Get <ODataValue>("B").Value.ShouldEqual(1);
            entity.Get <ODataEntity>("c").Get <string>("x").ShouldEqual("abc");
            entity.Get <ODataObject>("C").GetType().ShouldEqual(typeof(ODataEntity));
            entity.Get <int>("d").ShouldEqual(-1);

            UnitTestHelpers.AssertThrows <InvalidCastException>(() => entity.Get <int>("a"));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => entity.Get <long>("F"));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the strongly-typed value of the named property. This method can be used in OData queries as long as <paramref name="propertyName"/>
        /// is a constant or local variable capture
        /// </summary>
        /// <typeparam name="TProperty">the property type</typeparam>
        /// <param name="propertyName">the property name</param>
        /// <returns>the property value</returns>
        public TProperty Get <TProperty>(string propertyName)
        {
            Throw.IfNull(propertyName, "propertyName");

            object result;

            if (!this.Values.TryGetValue(propertyName, out result))
            {
                throw new ArgumentException("The entity does not contain a value for property '" + propertyName + "'!");
            }

            if (result == null)
            {
                if (!typeof(TProperty).CanBeNull())
                {
                    throw new InvalidCastException(string.Format(
                                                       "Property '{0}' has a null value and cannot be cast to requested type '{1}'",
                                                       propertyName,
                                                       typeof(TProperty)
                                                       ));
                }
                return(default(TProperty)); // always null due to check above
            }

            if (result is TProperty)
            {
                return((TProperty)result);
            }

            var tProperty = typeof(TProperty);

            if (tProperty.IsNumeric() && result.GetType().IsNumeric())
            {
                try
                {
                    return(NumberHelper.CheckedConvert <TProperty>(result));
                }
                catch (Exception ex)
                {
                    throw new InvalidCastException(
                              string.Format(
                                  "Failed to convert property '{0}' value '{1}' of type {2} to requested type {3}",
                                  propertyName,
                                  result,
                                  result.GetType(),
                                  tProperty
                                  ),
                              ex
                              );
                }
            }

            if (typeof(ODataObject).IsAssignableFrom(tProperty))
            {
                // at this point, we already know it's not ODataEntity since that would be
                // handled above. Thus, we can just handle ODataValue
                return((TProperty)(object)ODataValue.FromObject(result));
            }

            throw new InvalidCastException(string.Format(
                                               "value '{0}' of type {1} for property '{2}' is not compatible with requested type {3}",
                                               result,
                                               result.GetType(), // already checked for null above!
                                               propertyName,
                                               tProperty
                                               ));
        }