Exemplo n.º 1
0
        static object Deserialize(JsonValue v, Type t)
        {
            switch (v.ValueType)
            {
            case JsonValueType.Number:
                JsonNumber jn = (JsonNumber)v;
                if (jn.NumberType == JsonNumberType.Signed)
                {
                    return(Convert.ChangeType(jn.ValueSigned, t));
                }
                if (jn.NumberType == JsonNumberType.Unsigned)
                {
                    return(Convert.ChangeType(jn.ValueUnsigned, t));
                }
                return(Convert.ChangeType(jn.Value, t));

            /*double d = (v as JsonNumber).Value;
             * if (t == typeof (ulong))
             *      return (ulong)d;
             * else if (t == typeof (long))
             *      return (long)d;
             * else if (t == typeof (uint))
             *      return (uint)d;
             * else if (t == typeof (int))
             *      return (int)d;
             * else if (t == typeof (double))
             *      return (double)d;
             * else if (t == typeof (float))
             *      return (float)d;
             * else if (t == typeof (ushort))
             *      return (ushort)d;
             * else if (t == typeof (short))
             *      return (short)d;
             * else if (t == typeof (byte))
             *      return (byte)d;
             * else if (t == typeof (sbyte))
             *      return (sbyte)d;
             * break;*/
            case JsonValueType.String:
                if (t == typeof(DateTime))
                {
                    return(DateTime.ParseExact((v as JsonString).Value, SerializationCache.JsonDateTimeFormat, SerializationCache.InvariantCulture));
                }
                else if (t.IsEnum)
                {
                    return(Enum.Parse(t, (v as JsonString).Value, true));
                }
                else
                {
                    return((v as JsonString).Value);
                }

            case JsonValueType.Boolean:
                return((v as JsonBoolean).Value);

            case JsonValueType.Object:
                return(Deserialize(v as JsonObject, t));

            case JsonValueType.Array:
                return(Deserialize(v as JsonArray, t));
            }
            throw new NotSupportedException();
        }
Exemplo n.º 2
0
        public JsonValue Read()
        {
            JsonValue cur = null;
            string lastPropName = null;
            Stack<JsonValue> stack = new Stack<JsonValue> ();
            Stack<string> propNameStack = new Stack<string> ();
            JsonReader reader = _reader;

            while (reader.Read ()) {
                switch (reader.Token) {
                    case JsonToken.ArrayStart:
                    case JsonToken.ObjectStart:
                        if (cur != null) {
                            stack.Push (cur);
                            if (cur is JsonObject) {
                                propNameStack.Push (lastPropName);
                                lastPropName = null;
                            }
                        }
                        if (reader.Token == JsonToken.ArrayStart)
                            cur = new JsonArray (new List<JsonValue> ());
                        else
                            cur = new JsonObject (new Dictionary<string,JsonValue> ());
                        break;
                    case JsonToken.ObjectEnd:
                    case JsonToken.ArrayEnd:
                        if (stack.Count == 0)
                            return cur;
                        JsonValue parent = stack.Pop ();
                        if (parent is JsonArray) {
                            (parent as JsonArray).Value.Add (cur);
                        } else if (parent is JsonObject) {
                            lastPropName = propNameStack.Pop ();
                            if (lastPropName == null)
                                throw new JsonException ();
                            (parent as JsonObject).Value.Add (lastPropName, cur);
                            lastPropName = null;
                        }
                        cur = parent;
                        break;
                    case JsonToken.PropertyName:
                        if (lastPropName != null)
                            throw new JsonException ();
                        lastPropName = (string)reader.Value;
                        break;
                    case JsonToken.Boolean:
                    case JsonToken.Null:
                    case JsonToken.Number:
                    case JsonToken.String:
                        JsonValue value;
                        switch (reader.Token) {
                            case JsonToken.Boolean: value = new JsonBoolean ((bool)reader.Value); break;
                            case JsonToken.Null: value = new JsonNull (); break;
                            case JsonToken.Number:
                                value = new JsonNumber (Convert (reader.NumberType), (double)reader.Value, reader.ValueSignedInteger, reader.ValueUnsignedInteger);
                                break;
                            case JsonToken.String: value = new JsonString ((string)reader.Value); break;
                            default: throw new JsonException ();
                        }
                        if (cur == null)
                            return value;
                        if (cur is JsonArray) {
                            (cur as JsonArray).Value.Add (value);
                        } else if (cur is JsonObject) {
                            if (lastPropName == null)
                                throw new JsonException ();
                            (cur as JsonObject).Value.Add (lastPropName, value);
                            lastPropName = null;
                        }
                        break;
                }
            }

            if (cur == null)
                return null;
            throw new JsonException ();
        }
Exemplo n.º 3
0
        public JsonValue Read()
        {
            JsonValue         cur           = null;
            string            lastPropName  = null;
            Stack <JsonValue> stack         = new Stack <JsonValue> ();
            Stack <string>    propNameStack = new Stack <string> ();
            JsonReader        reader        = _reader;

            while (reader.Read())
            {
                switch (reader.Token)
                {
                case JsonToken.ArrayStart:
                case JsonToken.ObjectStart:
                    if (cur != null)
                    {
                        stack.Push(cur);
                        if (cur is JsonObject)
                        {
                            propNameStack.Push(lastPropName);
                            lastPropName = null;
                        }
                    }
                    if (reader.Token == JsonToken.ArrayStart)
                    {
                        cur = new JsonArray(new List <JsonValue> ());
                    }
                    else
                    {
                        cur = new JsonObject(new Dictionary <string, JsonValue> ());
                    }
                    break;

                case JsonToken.ObjectEnd:
                case JsonToken.ArrayEnd:
                    if (stack.Count == 0)
                    {
                        return(cur);
                    }
                    JsonValue parent = stack.Pop();
                    if (parent is JsonArray)
                    {
                        (parent as JsonArray).Value.Add(cur);
                    }
                    else if (parent is JsonObject)
                    {
                        lastPropName = propNameStack.Pop();
                        if (lastPropName == null)
                        {
                            throw new JsonException();
                        }
                        (parent as JsonObject).Value.Add(lastPropName, cur);
                        lastPropName = null;
                    }
                    cur = parent;
                    break;

                case JsonToken.PropertyName:
                    if (lastPropName != null)
                    {
                        throw new JsonException();
                    }
                    lastPropName = (string)reader.Value;
                    break;

                case JsonToken.Boolean:
                case JsonToken.Null:
                case JsonToken.Number:
                case JsonToken.String:
                    JsonValue value;
                    switch (reader.Token)
                    {
                    case JsonToken.Boolean: value = new JsonBoolean((bool)reader.Value); break;

                    case JsonToken.Null: value = new JsonNull(); break;

                    case JsonToken.Number:
                        value = new JsonNumber(Convert(reader.NumberType), (double)reader.Value, reader.ValueSignedInteger, reader.ValueUnsignedInteger);
                        break;

                    case JsonToken.String: value = new JsonString((string)reader.Value); break;

                    default: throw new JsonException();
                    }
                    if (cur == null)
                    {
                        return(value);
                    }
                    if (cur is JsonArray)
                    {
                        (cur as JsonArray).Value.Add(value);
                    }
                    else if (cur is JsonObject)
                    {
                        if (lastPropName == null)
                        {
                            throw new JsonException();
                        }
                        (cur as JsonObject).Value.Add(lastPropName, value);
                        lastPropName = null;
                    }
                    break;
                }
            }

            if (cur == null)
            {
                return(null);
            }
            throw new JsonException();
        }