Esempio n. 1
0
        static void AnalyseJson(JsonNode json, int indent, StringBuilder sb)
        {
            bool first = true;

            if (json is JsonObject)
            {
                JsonObject @object = (JsonObject)json;

                sb.Append("{");
                sb.AppendLine();
                foreach (string key in @object.Keys)
                {
                    if (!first)
                    {
                        sb.Append(",");
                        sb.AppendLine();
                    }
                    else
                    {
                        first = false;
                    }

                    sb.Append(new string(' ', indent + 2));
                    sb.Append(key);
                    sb.Append(" : ");

                    JsonNode node = @object[key];
                    AnalyseJson(node, indent + 2, sb);
                }
                sb.AppendLine();
                sb.Append(new string(' ', indent));
                sb.Append("}");
            }
            else if (json is JsonArray)
            {
                JsonArray array = (JsonArray)json;

                sb.Append("[");
                foreach (JsonNode value in array.Items)
                {
                    if (!first)
                    {
                        sb.Append(",");
                    }
                    else
                    {
                        first = false;
                    }

                    AnalyseJson(value, indent + 2, sb);
                }
                sb.Append("]");
            }
            else if (json is JsonValue)
            {
                JsonValue value = (JsonValue)json;
                if (value.Value == null)
                {
                    sb.Append("null");
                }
                else if (value.Value is string)
                {
                    sb.Append("\"" + (string)value.Value + "\"");
                }
                else
                {
                    sb.Append(value.Value);
                }
            }
        }
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>
 /// <returns>instance with data from json node</returns>
 public object Read(Type type, JsonNode node)
 {
     return(Read(type, node, false));
 }
Esempio n. 3
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);
        }
Esempio n. 4
0
 /// <summary>
 /// reads a type from a json node
 /// </summary>
 /// <typeparam name="T">type to read</typeparam>
 /// <param name="node">json node</param>
 /// <returns>instance with data from json node</returns>
 public T Read <T>(JsonNode node)
 {
     return((T)Read(typeof(T), node));
 }