Esempio n. 1
0
        public static string Get(PropertyInfo property)
        {
#if WINDOWS_UWP
            JsonLoaderAttribute attribute = property.GetCustomAttribute <JsonLoaderAttribute>();
#else
            JsonLoaderAttribute attribute = GetCustomAttribute(property, typeof(JsonLoaderAttribute)) as JsonLoaderAttribute;
#endif
            return(attribute?.Method);
        }
Esempio n. 2
0
        /// <summary>
        /// reads a type from a json node
        /// </summary>
        /// <param name="type">type to read</param>
        /// <param name="node">json node</param>
        /// <param name="variant"></param>
        /// <returns>instance with data from json node</returns>
        object Read(Type type, JsonNode node, bool variant)
        {
            if (node is JsonValue && ((JsonValue)node).Value == null)
            {
                return(null);
            }

            if (type == typeof(object))
            {
                if (!(node is JsonValue))
                {
                    throw new JsonException("Currently only values are supported for object properties");
                }
                return(((JsonValue)node).Value);
            }

            if (type.IsArray)
            {
                if (type.GetElementType() == typeof(byte))
                {
                    if (!(node is JsonValue))
                    {
                        throw new JsonException("Unable to read value from non value node");
                    }
                    string base64value = ((JsonValue)node).Value as string;
                    if (base64value == null)
                    {
                        return(null);
                    }
                    return(Convert.FromBase64String(base64value));
                }

                if (!(node is JsonArray))
                {
                    throw new JsonException("Unable to read array from non array json node");
                }
                return(ReadArray(type.GetElementType(), (JsonArray)node, variant));
            }

#if WINDOWS_UWP
            if (type.GetTypeInfo().IsValueType || type.GetTypeInfo().IsEnum || type == typeof(string) || type == typeof(Version))
            {
#else
            if (type.IsValueType || type.IsEnum || type == typeof(string) || type == typeof(Version) || type == typeof(IntPtr) || type == typeof(UIntPtr))
            {
#endif
                if (!(node is JsonValue))
                {
                    throw new JsonException("Unable to read value from non value node");
                }
                return(Converter.Convert(((JsonValue)node).Value, type));
            }

            if (type == typeof(Type))
            {
                return(Type.GetType(node.SelectValue <string>()));
            }

            // check for variant types
            if (variant)
            {
                Type customtype = node.SelectValue <Type>("type");
                return(Read(customtype, node.SelectNode("data")));
            }

            if (serializers.Contains(type))
            {
                return(serializers.Get(type).Deserialize(node));
            }

            if (!(node is JsonObject))
            {
                throw new JsonException("Unable to read object from non object node");
            }
            JsonObject @object = (JsonObject)node;

#if WINDOWS_UWP
            object instance = Activator.CreateInstance(type);
#else
            object instance = Activator.CreateInstance(type, true);
#endif

            foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                string key = JsonKeyAttribute.GetKey(property) ?? property.Name.ToLower();

                if ([email protected](key))
                {
                    continue;
                }

                string loader = JsonLoaderAttribute.Get(property);

                object value;
                if (loader != null)
                {
                    MethodInfo method = type.GetMethod(loader, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                    value = method.Invoke(null, new object[] { @object.SelectValue <string>(key) });
                }
                else
                {
                    value = Read(property.PropertyType, @object.GetNode(key), VariantAttribute.IsVariant(property));
                }

                try {
                    property.SetValue(instance, Converter.Convert(value, property.PropertyType), null);
                }
                catch (Exception e) {
                    // TODO: log this shit
                }
            }
            return(instance);
        }

        object ReadArray(Type elementtype, JsonArray node, bool variant)
        {
            Array instance = Array.CreateInstance(elementtype, node.Count);

            for (int i = 0; i < node.Count; ++i)
            {
                instance.SetValue(Read(elementtype, node.GetNode(i), variant), i);
            }
            return(instance);
        }