コード例 #1
0
        private void FillObject(object obj, Literacy lit, string json)
        {
            if (json == null || json.Length == 0)
            {
                return;
            }

            unsafe
            {
                fixed(char *p = json)
                {
                    UnsafeJsonReader reader = new UnsafeJsonReader(p, json.Length);

                    if (reader.IsEnd())
                    {
                        return;
                    }

                    if (reader.Current == '{')
                    {
                        reader.MoveNext();
                        FillObject(obj, lit, reader);
                        if (reader.Current != '}')
                        {
                            ThrowMissingCharException('}');
                        }
                    }
                    else if (reader.Current == '[')
                    {
                        reader.MoveNext();
                        var st = GenericCollection.GetList(obj.GetType());
                        if (st == null)
                        {
                            ThrowNoIList(obj.GetType());
                        }
                        FillList((IList)obj, st.ElementType, reader);

                        if (reader.Current != ']')
                        {
                            ThrowMissingCharException(']');
                        }
                    }
                    else
                    {
                        ThrowException("起始字符:" + reader.Current);
                    }
                    reader.MoveNext();
                    if (reader.IsEnd())
                    {
                        reader.Dispose();
                    }
                    else
                    {
                        ThrowException("错误的结束字符:" + reader.Current);
                    }
                }
            }
        }
コード例 #2
0
        private void FillObject(ref object obj, Literacy lit, string json)
        {
            if (json == null || json.Length == 0)
            {
                return;
            }

            unsafe
            {
                fixed(char *p = json)
                {
                    UnsafeJsonReader reader = new UnsafeJsonReader(p, json.Length);

                    if (reader.IsEnd())
                    {
                        return;
                    }

                    if (reader.Current == '{')
                    {
                        reader.MoveNext();
                        if (obj == null)
                        {
                            obj = new Dictionary <string, object>();
                            lit = Literacy.Cache(typeof(Dictionary <string, object>), true);
                        }
                        FillObject(obj, lit, reader);
                        if (reader.Current != '}')
                        {
                            ThrowMissingCharException('}');
                        }
                    }
                    else if (reader.Current == '[')
                    {
                        reader.MoveNext();

                        if (obj == null)
                        {
                            obj = new ArrayList();
                            FillList((IList)obj, typeof(object), reader);
                        }
                        else if (obj is ArrayList)
                        {
                            FillList((IList)obj, lit.Type, reader);
                        }
                        else
                        {
                            var st = GenericCollection.GetList(lit.Type);
                            if (st == null)
                            {
                                ThrowNoIList(lit.Type);
                            }
                            FillList((IList)obj, st.ElementType, reader);
                        }

                        if (reader.Current != ']')
                        {
                            ThrowMissingCharException(']');
                        }
                    }
                    else
                    {
                        ThrowException("起始字符:" + reader.Current);
                    }
                    reader.MoveNext();
                    if (reader.IsEnd())
                    {
                        reader.Dispose();
                    }
                    else
                    {
                        ThrowException("错误的结束字符:" + reader.Current);
                    }
                }
            }
        }