Ejemplo n.º 1
0
        private static bool _TryCastValue(ref Utf8JsonReader reader, Type vtype, out Object value)
        {
            value = null;

            if (reader.TokenType == JSONTOKEN.EndArray)
            {
                return(false);
            }
            if (reader.TokenType == JSONTOKEN.EndObject)
            {
                return(false);
            }
            // if (reader.TokenType == JsonToken.EndConstructor) return false;

            if (reader.TokenType == JSONTOKEN.PropertyName)
            {
                reader.Read();
            }

            // untangle nullable
            var ntype = Nullable.GetUnderlyingType(vtype);

            if (ntype != null)
            {
                vtype = ntype;
            }

            if (vtype == typeof(String))
            {
                value = reader.AsString(); return(true);
            }
            if (vtype == typeof(Boolean))
            {
                value = reader.AsBoolean(); return(true);
            }

            if (vtype == typeof(Int16))
            {
                value = reader.GetInt16(); return(true);
            }
            if (vtype == typeof(Int32))
            {
                value = reader.GetInt32(); return(true);
            }
            if (vtype == typeof(Int64))
            {
                value = reader.GetInt64(); return(true);
            }
            if (vtype == typeof(UInt16))
            {
                value = reader.GetUInt16(); return(true);
            }
            if (vtype == typeof(UInt32))
            {
                value = reader.GetUInt32(); return(true);
            }
            if (vtype == typeof(UInt64))
            {
                value = reader.GetUInt64(); return(true);
            }
            if (vtype == typeof(Single))
            {
                value = reader.GetSingle(); return(true);
            }
            if (vtype == typeof(Double))
            {
                value = reader.GetDouble(); return(true);
            }
            if (vtype == typeof(Decimal))
            {
                value = reader.GetDecimal(); return(true);
            }

            if (vtype.IsEnum)
            {
                value = reader.AsEnum(vtype); return(true);
            }

            if (vtype == typeof(Vector2))
            {
                var l = new List <float>(2);
                DeserializePropertyList <float>(ref reader, l);
                value = new Vector2(l[0], l[1]);
                return(true);
            }

            if (vtype == typeof(Vector3))
            {
                var l = new List <float>(3);
                DeserializePropertyList <float>(ref reader, l);
                value = new Vector3(l[0], l[1], l[2]);
                return(true);
            }

            if (vtype == typeof(Vector4))
            {
                var l = new List <float>(4);
                DeserializePropertyList <float>(ref reader, l);
                value = new Vector4(l[0], l[1], l[2], l[3]);
                return(true);
            }

            if (vtype == typeof(Quaternion))
            {
                var l = new List <float>(4);
                DeserializePropertyList <float>(ref reader, l);
                value = new System.Numerics.Quaternion(l[0], l[1], l[2], l[3]);
                return(true);
            }

            if (vtype == typeof(Matrix4x4))
            {
                var l = new List <float>(16);
                DeserializePropertyList <float>(ref reader, l);
                value = new Matrix4x4
                        (
                    l[0], l[1], l[2], l[3],
                    l[4], l[5], l[6], l[7],
                    l[8], l[9], l[10], l[11],
                    l[12], l[13], l[14], l[15]
                        );
                return(true);
            }

            if (typeof(JsonSerializable).IsAssignableFrom(vtype))
            {
                var item = Activator.CreateInstance(vtype, true) as JsonSerializable;

                // System.Diagnostics.Debug.Assert(reader.TokenType == JSONTOKEN.StartObject);
                item.Deserialize(ref reader);
                // System.Diagnostics.Debug.Assert(reader.TokenType == JSONTOKEN.EndObject);

                value = item;

                return(true);
            }

            if (vtype.IsGenericType)
            {
                if (vtype.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    var valType = vtype.GetGenericArguments()[1];

                    if (valType == typeof(Int32))
                    {
                        var dict = new Dictionary <string, Int32>();
                        DeserializePropertyDictionary(ref reader, dict);
                        value = dict;
                        return(true);
                    }
                }

                throw new NotImplementedException($"Can't deserialize {vtype}");
            }

            throw new NotImplementedException($"Can't deserialize {vtype}");
        }