Exemplo n.º 1
0
        static NameValueCollection CreateNameValueCollection(JsonDict d)
        {
            NameValueCollection nv = new NameValueCollection();

            foreach (var o in d)
            {
                var k  = o.Key;
                var ov = o.Value;
                if (ov == null)
                {
                    nv.Add(k, null);
                    continue;
                }
                var s = ov as string;
                if (s != null)
                {
                    nv.Add(k, s);
                    continue;
                }
                var sa = ov as IList;
                if (sa != null)
                {
                    foreach (string item in sa)
                    {
                        nv.Add(k, item);
                    }
                    continue;
                }
                nv.Add(k, ov.ToString());
            }

            return(nv);
        }
Exemplo n.º 2
0
        JsonDict ParseObject()
        {
            var table = new JsonDict();

            ConsumeToken();              // {

            while (true)
            {
                switch (LookAhead())
                {
                case Token.Comma:
                    ConsumeToken();
                    break;

                case Token.Curly_Close:
                    ConsumeToken();
                    return(table);

                default:
                {
                    // name
                    string name = ParseString();

                    // :
                    if (NextToken() != Token.Colon)
                    {
                        throw new JsonParserException("Expected colon at index ", _index, GetContextText());
                    }

                    // value
                    object value = ParseValue();

                    if (name.Length == 0)
                    {
                        // ignores unnamed item
                        continue;
                    }
                    if (name[0] == '$')
                    {
                        switch (name)
                        {
                        case JsonDict.ExtTypes: table.Types = (JsonDict)value; continue;

                        case JsonDict.ExtType: table.Type = (string)value; continue;

                        case JsonDict.ExtRefIndex: table.RefIndex = (int)(long)value; continue;

                        case JsonDict.ExtSchema: table.Schema = value; continue;

                        default:
                            break;
                        }
                    }
                    table.Add(new KeyValuePair <string, object> (name, value));
                }
                break;
                }
            }
        }
Exemplo n.º 3
0
        static StringDictionary CreateStringDictionary(JsonDict d)
        {
            StringDictionary nv = new StringDictionary();

            foreach (var o in d)
            {
                nv.Add(o.Key, (string)o.Value);
            }

            return(nv);
        }
Exemplo n.º 4
0
        object CreateStringKeyDictionary(JsonDict reader, ReflectionCache pt)
        {
            var col = (IDictionary)pt.Instantiate();
            // NOTE: argument 0 is not used
            ReflectionCache ec = pt.ArgumentReflections != null ? pt.ArgumentReflections[1] : null;
            var             m  = ec != null ? ec.DeserializeMethod : RevertUndefined;

            foreach (KeyValuePair <string, object> values in reader)
            {
                col.Add(values.Key, m(this, values.Value, ec));
            }
            return(col);
        }
Exemplo n.º 5
0
        DataSet CreateDataSet(JsonDict reader)
        {
            DataSet ds = new DataSet();

            ds.EnforceConstraints = false;
            ds.BeginInit();

            // read dataset schema here
            var schema = reader.Schema;

            if (schema is string)
            {
                TextReader tr = new StringReader((string)schema);
                ds.ReadXmlSchema(tr);
            }
            else
            {
                DatasetSchema ms = (DatasetSchema)CreateObject((JsonDict)schema, _manager.GetReflectionCache(typeof(DatasetSchema)), null);
                ds.DataSetName = ms.Name;
                for (int i = 0; i < ms.Info.Count; i += 3)
                {
                    if (ds.Tables.Contains(ms.Info[i]) == false)
                    {
                        ds.Tables.Add(ms.Info[i]);
                    }
                    ds.Tables[ms.Info[i]].Columns.Add(ms.Info[i + 1], Type.GetType(ms.Info[i + 2]));
                }
            }

            foreach (KeyValuePair <string, object> pair in reader)
            {
                //if (pair.Key == "$type" || pair.Key == "$schema") continue;

                JsonArray rows = (JsonArray)pair.Value;
                if (rows == null)
                {
                    continue;
                }

                DataTable dt = ds.Tables[pair.Key];
                ReadDataTable(rows, dt);
            }

            ds.EndInit();

            return(ds);
        }
Exemplo n.º 6
0
        DataTable CreateDataTable(JsonDict reader)
        {
            var dt = new DataTable();

            // read dataset schema here
            var schema = reader.Schema;

            if (schema is string)
            {
                TextReader tr = new StringReader((string)schema);
                dt.ReadXmlSchema(tr);
            }
            else
            {
                var ms = (DatasetSchema)CreateObject((JsonDict)schema, _manager.GetReflectionCache(typeof(DatasetSchema)), null);
                dt.TableName = ms.Info[0];
                for (int i = 0; i < ms.Info.Count; i += 3)
                {
                    dt.Columns.Add(ms.Info[i + 1], Reflection.Instance.GetTypeFromCache(ms.Info[i + 2]));
                }
            }

            foreach (var pair in reader)
            {
                //if (pair.Key == "$type" || pair.Key == "$schema")
                //	continue;

                var rows = (JsonArray)pair.Value;
                if (rows == null)
                {
                    continue;
                }

                if (!dt.TableName.Equals(pair.Key, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                ReadDataTable(rows, dt);
            }

            return(dt);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="data">The data to be deserialized.</param>
        /// <param name="type">The reflection cache of the type.</param>
        /// <param name="input">The data container. If this value is not null, deserialized members will be written to it. If null, new object will be created.</param>
        /// <returns>The deserialized object.</returns>
        /// <exception cref="JsonSerializationException">Cannot determine type from <paramref name="data"/>.</exception>
        internal object CreateObject(JsonDict data, ReflectionCache type, object input)
        {
            if (data.RefIndex > 0)
            {
                object v = null;
                _cirrev.TryGetValue(data.RefIndex, out v);
                return(v);
            }

            if (data.Types != null && data.Types.Count > 0)
            {
                _usingglobals = true;
                globaltypes   = new Dictionary <string, object> ();
                foreach (var kv in data.Types)
                {
                    globaltypes.Add((string)kv.Value, kv.Key);
                }
            }

            var  tn    = data.Type;
            bool found = (tn != null && tn.Length > 0);

#if !SILVERLIGHT
            if (found == false && type != null && typeof(object).Equals(type.Type))
            {
                return(data);
            }
#endif
            if (found)
            {
                if (_usingglobals)
                {
                    object tname = "";
                    if (globaltypes != null && globaltypes.TryGetValue(data.Type, out tname))
                    {
                        tn = (string)tname;
                    }
                }
                type = _manager.GetReflectionCache(Reflection.Instance.GetTypeFromCache(tn));
            }

            if (type == null)
            {
                throw new JsonSerializationException("Cannot determine type");
            }

            object o = input;
            if (o == null)
            {
                o = _params.ParametricConstructorOverride
                                        ? System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type.Type)
                                        : type.Instantiate();
            }
            int circount = 0;
            // dictionary lookup makes it impossible to use objects with a custom GetHashCode based on (unchanging) properties:
            if (_circobj.TryGetValue(o, out circount) == false)
            {
                circount = _circobj.Count + 1;
                _circobj.Add(o, circount);
                _cirrev.Add(circount, o);
            }

            var si = type.Interceptor;
            if (si != null)
            {
                si.OnDeserializing(o);
            }
            Dictionary <string, JsonMemberSetter> props = type.Setters;
            //TODO: Candidate to removal of unknown use of map
            //if (data.Map != null) {
            //	ProcessMap (o, props, data.Map);
            //}
            foreach (var kv in data)
            {
                var n = kv.Key;
                var v = kv.Value;
                JsonMemberSetter pi;
                if (props.TryGetValue(n, out pi) == false || pi.CanWrite == false && pi.Member.JsonDataType != JsonDataType.List)
                {
                    continue;
                }
                MemberCache m         = pi.Member;
                var         ji        = new JsonItem(n, v, false);
                bool        converted = false;
                // TODO: Convert items for types implements IEnumerable and Add(?) method
                if (v is IList && pi.ItemConverter != null)
                {
                    converted = ConvertItems(pi, ji);
                }
                if (pi.Converter != null || m.MemberTypeReflection.Converter != null)
                {
                    ConvertProperty(o, pi, ji);
                }

                object oset = null;
                // use the converted value
                if (converted || ReferenceEquals(ji._Value, v) == false)
                {
                    if (pi.CanWrite == false && m.JsonDataType == JsonDataType.List)
                    {
                        ji._Value = CreateList((JsonArray)ji._Value, m.MemberTypeReflection, m.Getter(o));
                    }
                    if (ji._Value != null || m.IsClass || m.IsNullable)
                    {
                        oset = ji._Value;
                        goto SET_VALUE;
                    }
                    continue;
                }
                // process null value
                if (ji._Value == null)
                {
                    var i = new JsonItem(n, null, false);
                    if (si != null && si.OnDeserializing(o, i) == false)
                    {
                        continue;
                    }
                    if (i.Value != null || m.IsClass || m.IsNullable)
                    {
                        o = m.Setter(o, i.Value);
                    }
                    continue;
                }
                v = ji._Value;
                // set member value
                switch (m.JsonDataType)
                {
                case JsonDataType.Undefined: goto default;

                case JsonDataType.Int: oset = (int)(long)v; break;

                case JsonDataType.String:
                case JsonDataType.Bool:
                case JsonDataType.Long: oset = v; break;

                case JsonDataType.Double: oset = v is long?(double)(long)v : (double)v; break;

                case JsonDataType.Single: oset = v is long?(float)(long)v : (float)(double)v; break;

                case JsonDataType.DateTime: oset = CreateDateTime(this, v); break;

                case JsonDataType.Guid: oset = CreateGuid(v); break;

                case JsonDataType.ByteArray: oset = Convert.FromBase64String((string)v); break;

                case JsonDataType.List:
                    if (m.MemberTypeReflection.CollectionName != null)
                    {
                        goto default;
                    }
                    oset = CreateList((JsonArray)v, m.MemberTypeReflection, pi.CanWrite && (m.IsClass || m.IsStruct) ? null : m.Getter(o));
                    break;

                case JsonDataType.Object: oset = v; break;

                default:
                    if (m.DeserializeMethod != null)
                    {
                        oset = m.MemberTypeReflection.DeserializeMethod(this, ji._Value, m.MemberTypeReflection);
                        goto SET_VALUE;
                    }
                    if ((m.IsClass || m.IsStruct) && v is JsonDict)
                    {
                        oset = CreateObject((JsonDict)v, m.MemberTypeReflection, m.Getter(o));
                    }

                    else if (v is JsonArray)
                    {
                        oset = CreateArray((JsonArray)v, _manager.GetReflectionCache(typeof(object[])));
                    }

                    else if (m.IsValueType)
                    {
                        oset = ChangeType(v, m.ChangeType);
                    }

                    else
                    {
                        oset = v;
                    }

                    break;
                }
SET_VALUE:
                ji.Value = oset;
                if (si != null)
                {
                    if (si.OnDeserializing(o, ji) == false)
                    {
                        continue;
                    }
                }
                if (m.Setter != null)
                {
                    o = m.Setter(o, ji.Value);
                }
            }
            if (si != null)
            {
                si.OnDeserialized(o);
            }
            return(o);
        }