Stream-like access to JSON text.
Esempio n. 1
0
    // 预加载基础资源
    public void PreLoad(LoadCallback callback)
    {
        Object asset = LoadResource("preload", false);

        if (asset == null)
        {
            return;
        }
        TextAsset textAsset = (TextAsset)asset;

        LitJson.JsonReader jsonR = new LitJson.JsonReader(textAsset.text);
        LitJson.JsonData   jsonD = LitJson.JsonMapper.ToObject(jsonR);
        if (!jsonD.IsArray || jsonD.Count == 0)
        {
            return;
        }
        // 逐个加载资源,全部加载完毕后执行回调函数
        for (int i = 0; i < jsonD.Count; i++)
        {
            LoadResource((string)jsonD[i]["name"], true);
        }
        if (callback != null)
        {
            callback(null, null);
        }
    }
Esempio n. 2
0
    /// <summary>
    /// 初始化
    /// </summary>
    void Init()
    {
        string jsonPath = Application.persistentDataPath + "/" + Const.ConfigFile;

        if (!System.IO.File.Exists(jsonPath))
        {
            Debug.LogError("Can't get Config.json file.");
            return;
        }

        string content = System.IO.File.ReadAllText(jsonPath);

        LitJson.JsonReader reader = new LitJson.JsonReader(content);
        m_config = LitJson.JsonMapper.ToObject(reader);

        if (m_config == null)
        {
            Debug.LogError("Config Json Data structure error.");
        }
        else
        {
            try
            {
                Const.UpdateMode = (bool)m_config["update_mode"];
            }
            catch (Exception err)
            {
                Debug.LogError(err.Message);
            }
        }
    }
Esempio n. 3
0
    /// <summary>
    /// 反序列化一场战斗
    /// </summary>
    private void DeserializeBattleField()
    {
        if (!File.Exists(outputPath))
        {
            UnityEditor.EditorUtility.DisplayDialog("错误", "找不到战斗序列化文件,请先序列化一场战斗...", "得嘞");
            return;
        }
        battleField?.Return();
        battleField = BattleField.Create();
        using (TextReader rt = new StreamReader(outputPath))
        {
            LitJson.JsonReader jsonReader = new LitJson.JsonReader(rt);
            var dh = DeserializeHelper.Create();
            dh.ObjectDeserializeCallback = delegate(string propertyName, JsonReader reader)
            {
                if (propertyName == "battleField")
                {
                    battleField.Deserialize(reader);
                    return(true);
                }
                return(false);
            };
            //开始反序列化
            dh.Deserialize(jsonReader, true);
        }

        Debug.Log("反序列化完成");

        //对反序列化战场进行序列化
        if (battleField != null)
        {
            SerializeBattleFieldToPath(mirrorOutputPath);
        }
    }
Esempio n. 4
0
        public void DoubleTest()
        {
            string json = @"[ 0.0, -0.0, 3.1416, 8e-3, 7E-5, -128.000009,
                   144e+3, 0.1e2 ]";

            JsonReader reader = new JsonReader (json);
            reader.Read ();

            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 0.0,
                             Double.Epsilon, "A1");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 0.0,
                             Double.Epsilon, "A2");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 3.1416,
                             Double.Epsilon, "A3");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 0.008,
                             Double.Epsilon, "A4");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 0.00007,
                             Double.Epsilon, "A5");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, -128.000009,
                             Double.Epsilon, "A6");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 144000.0,
                             Double.Epsilon, "A7");
            reader.Read ();
            Assert.AreEqual ((double) reader.Value, 10.0,
                             Double.Epsilon, "A8");

            reader.Close ();
        }
Esempio n. 5
0
        public void CommentsTest()
        {
            string json = @"
                {
                    // This is the first property
                    ""foo"" : ""bar"",

                    /**
                     * This is the second property
                     **/
                     ""baz"": ""blah""
                }";

            JsonReader reader = new JsonReader (json);

            reader.Read ();
            reader.Read ();
            Assert.AreEqual ("foo", (string) reader.Value, "A1");

            reader.Read ();
            reader.Read ();
            Assert.AreEqual ("baz", (string) reader.Value, "A2");

            reader.Read ();
            reader.Read ();
            Assert.IsTrue (reader.EndOfJson, "A3");
        }
Esempio n. 6
0
 public static ZenSettings Read(string filePath)
 {
     string src = File.ReadAllText(filePath);
     src = SanitizeJSon(src);
     JsonReader reader = new JsonReader(src);
     return ReadZenSettings(reader);
 }
Esempio n. 7
0
 public static OSD DeserializeJson(Stream json)
 {
     using (StreamReader streamReader = new StreamReader(json))
     {
         JsonReader reader = new JsonReader(streamReader);
         return DeserializeJson(JsonMapper.ToObject(reader));
     }
 }
Esempio n. 8
0
 private void ReadModules(JsonReader reader)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.ObjectStart) ReadModule(reader);
     }
 }
Esempio n. 9
0
        public static void LitJsonReaderStrings ()
        {
            for (int i = 0; i < Common.Iterations; i++) {
                JsonReader reader = new JsonReader (Common.JsonStrings);

                while (reader.Read ());
            }
        }
Esempio n. 10
0
 public override void Parse(Object asset)
 {
     if (asset != null && asset is TextAsset)          // 将解析到的数据放到 data 中
     {
         TextAsset          textAsset = (TextAsset)asset;
         string             sJson     = textAsset.text;
         LitJson.JsonReader jsonR     = new LitJson.JsonReader(sJson);
         m_data = LitJson.JsonMapper.ToObject(jsonR);
         Globals.It.BundleMgr.UnLoadBundleLocal(asset);
     }
 }
Esempio n. 11
0
        public void BooleanTest()
        {
            string json = "[ true, false ]";

            JsonReader reader = new JsonReader (json);
            reader.Read ();

            reader.Read ();
            Assert.IsTrue ((bool) reader.Value, "A1");
            reader.Read ();
            Assert.IsTrue (! ((bool) reader.Value), "A2");

            reader.Close ();
        }
Esempio n. 12
0
        public static void WriteJsonData(this JsonWriter writer, JsonData jsonData)
        {
            var reader = new JsonReader(jsonData.ToJson());

            while (reader.Read())
            {
                switch (reader.Token)
                {
                    case JsonToken.None:
                        break;
                    case JsonToken.ObjectStart:
                        writer.WriteObjectStart();
                        break;
                    case JsonToken.PropertyName:
                        writer.WritePropertyName(reader.Value.ToString());
                        break;
                    case JsonToken.ObjectEnd:
                        writer.WriteObjectEnd();
                        break;
                    case JsonToken.ArrayStart:
                        writer.WriteArrayStart();
                        break;
                    case JsonToken.ArrayEnd:
                        writer.WriteArrayEnd();
                        break;
                    case JsonToken.Int:
                        writer.Write((int)reader.Value);
                        break;
                    case JsonToken.Long:
                        writer.Write((long)reader.Value);
                        break;
                    case JsonToken.ULong:
                        writer.Write((ulong)reader.Value);
                        break;
                    case JsonToken.Double:
                        writer.Write((double)reader.Value);
                        break;
                    case JsonToken.String:
                        writer.Write((string)reader.Value);
                        break;
                    case JsonToken.Boolean:
                        writer.Write((bool)reader.Value);
                        break;
                    case JsonToken.Null:
                        break;
                    default:
                        break;
                }
            }
        }
Esempio n. 13
0
    /// <summary>
    /// 读json文件返回版本号
    /// </summary>
    /// <param name="verFileName"></param>
    /// <returns></returns>
    public static string ReadVersionFile(string verFilePath)
    {
        string versionPath = verFilePath;

        if (IsFileExist(versionPath))
        {
            string             content = ReadAllTextFromFile(versionPath);
            LitJson.JsonReader reader  = new LitJson.JsonReader(content);
            //VersionData config = LitJson.JsonMapper.ToObject<VersionData>(reader);
            LitJson.JsonData config = LitJson.JsonMapper.ToObject(reader);

            return((string)config["version"]);
        }
        else
        {
            return("0.0.0.0");
        }
    }
Esempio n. 14
0
 private void ReadModule(JsonReader reader)
 {
     // root/modules
     string prop = null;
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ObjectEnd) break;
         if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
         else if (reader.Token == JsonToken.ArrayStart)
         {
             if (prop == "types") ReadTypes(reader);
             else reader.SkipArray();
         }
         else if (reader.Token == JsonToken.ObjectStart) {
             reader.SkipObject();
         }
     }
 }
Esempio n. 15
0
        public static void LitJsonReaderFirstProperty ()
        {
            for (int i = 0; i < Common.Iterations; i++) {
                bool found = false;

                JsonReader reader = new JsonReader (Common.JsonText);

                while (reader.Read ()) {
                    if (reader.Token == JsonToken.PropertyName &&
                        (string) reader.Value == "FirstProperty") {
                        found = true;
                        break;
                    }
                }

                if (! found)
                    Console.WriteLine ("FirstProperty not found!");
            }
        }
Esempio n. 16
0
        private static ZenSettings ReadZenSettings(JsonReader reader)
        {
            ZenSettings settings = new ZenSettings();
            Type objType = settings.GetType();

            reader.Read();
            if (reader.Token != JsonToken.ObjectStart)
                return null;

            string currentProp = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName)
                    currentProp = reader.Value.ToString();
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    if (currentProp == "variables") settings.variables = ReadHashtable(reader);
                    else settings.langs.Add(currentProp, ReadZenLang(reader));
                }
            }

            foreach (ZenLang lang in settings.langs.Values)
            {
                if (lang.extends != null && settings.langs.ContainsKey(lang.extends))
                    ExtendLang(lang, settings.langs[lang.extends]);
            }

            if (!settings.langs.ContainsKey("xml"))
            {
                ZenLang xlang = new ZenLang();
                xlang.abbreviations = new Hashtable();
                xlang.element_types = new ZenElementTypes(null);
                xlang.filters = "xml, xsl";
                xlang.snippets = new Hashtable();
                settings.langs.Add("xml", xlang);
            }

            settings.variables["child"] = "";
            return settings;
        }
Esempio n. 17
0
 public override void Parse(Object asset)
 {
     if (asset != null && asset is TextAsset)
     {
         TextAsset          textAsset = (TextAsset)asset;
         string             sJson     = textAsset.text;
         LitJson.JsonReader jsonR     = new LitJson.JsonReader(sJson);
         LitJson.JsonData   jsonD     = LitJson.JsonMapper.ToObject(jsonR);
         int iCount = jsonD.Count;
         for (int i = 0; i < iCount; i++)             // 将解析到的数据放到dict
         {
             LitJson.JsonData itemData = jsonD[i];
             if (itemData == null)
             {
                 continue;
             }
             SkillJson jsonItem = SkillJson.Parse(itemData);
             m_jsons.Add(jsonItem.skillId, jsonItem);
         }
         Globals.It.BundleMgr.UnLoadBundleLocal(asset);
     }
 }
Esempio n. 18
0
        public static void ReadObject(JsonReader reader, object context, BeginObjectDelegate begin, ParserDelegate parser, EndObjectDelegate end)
        {
            System.Diagnostics.Debug.Assert(reader.Token == JsonToken.ObjectStart);

            if (begin != null)
            {
                begin(context);
            }

            // parse the properties
            reader.Read();
            while (reader.Token != JsonToken.ObjectEnd)
            {
                parser(reader, context);
                reader.Read();
            }

            if (end != null)
            {
                end(context);
            }
        }
Esempio n. 19
0
 private void Run()
 {
     using (StreamReader sr = new StreamReader(path.Path))
     {
         string raw = sr.ReadToEnd();
         sr.Close();
         JsonReader reader = new JsonReader(raw);
         try
         {
             if (reader.Read() && reader.Token == JsonToken.ObjectStart)
             {
                 files = new Dictionary<string, FileModel>();
                 BuildModel(reader);
                 path.SetFiles(files);
             }
         }
         catch (Exception ex)
         {
             TraceManager.AddAsync(ex.Message);
         }
     }
 }
Esempio n. 20
0
 private Reader(JsonReader json, string bundlePath, bool createdForThisInstance)
 {
     if (json == null)
     {
         throw new ArgumentNullException("json");
     }
     this.json = json;
     this.disposesTextReader = createdForThisInstance;
     this.prefix = bundlePath;
     if (!string.IsNullOrEmpty(this.prefix))
     {
         char chr = this.prefix[this.prefix.Length - 1];
         if (chr != '/' && chr != '\\')
         {
             this.prefix = string.Concat(this.prefix, "/");
         }
     }
     else
     {
         this.prefix = string.Empty;
     }
 }
Esempio n. 21
0
    public override void Parse(Object asset)
    {
        if (asset != null && asset is TextAsset)
        {
            TextAsset          textAsset = (TextAsset)asset;
            string             sJson     = textAsset.text;
            LitJson.JsonReader jsonR     = new LitJson.JsonReader(sJson);
            LitJson.JsonData   jsonD     = LitJson.JsonMapper.ToObject(jsonR);
            foreach (string skey in jsonD.Keys)             // 将解析到的数据放到dict
            {
                LitJson.JsonData itemData = jsonD[skey];
                if (itemData == null)
                {
                    continue;
                }
                HeroJson heroItem = HeroJson.Parse(itemData);
                m_heros.Add(heroItem.id, heroItem);
            }

            Globals.It.BundleMgr.UnLoadBundleLocal(asset);
        }
    }
Esempio n. 22
0
        public JsonWebTokenHeader(string originalData)
        {
            _originalData = originalData;
            var reader = new JsonReader(originalData.Base64UrlDecode(Encoding.UTF8));
            while (reader.Read())
            {
                if (reader.Token == JsonToken.PropertyName)
                {
                    switch (reader.Value.ToString())
                    {
                        case "alg":
                            reader.Read();
                            SetAlgorithm((string)reader.Value);
                            break;
                        case "kid":
                            reader.Read();
                            KeyId = (string)reader.Value;
                            break;
                        case "xdu":
                            reader.Read();
                            KeyFormat = KeyFormat.Rfc4050;
                            KeyUri = new Uri((string)reader.Value);
                            break;
                        case "jku":
                            reader.Read();
                            KeyFormat = KeyFormat.Json;
                            KeyUri = new Uri((string)reader.Value);
                            break;
                        case "xku":
                            reader.Read();
                            KeyFormat = KeyFormat.X509;
                            KeyUri = new Uri((string)reader.Value);
                            break;
                    }
                }

            }
        }
Esempio n. 23
0
    void Start()
    {
        // GameObject.Find("entity_01").transform.Translate(new Vector3(0, 0, 0));

        SetCharactersPosition();

        battleLog = new List <BattleLog>();
        int        count = readData.dataUserModel.warrior.Count;
        JsonReader test  = new LitJson.JsonReader("test");

        team = readData.dataUserModel.warrior.FindAll(x => x.position > 0);


        string world   = "1";
        string area    = "1";
        string level   = "1";
        string ordinal = "1";


        enemyIdWave1 = GetEnemyWave(world, area, level, ordinal, "enemyId_wave1");

        enemyIdWave2 = GetEnemyWave(world, area, level, ordinal, "enemyId_wave2");

        enemyIdWave3 = GetEnemyWave(world, area, level, ordinal, "enemyId_wave3");

        allWar       = new List <BattleWarrior>();
        allWarOrigin = new List <BattleWarrior>();

        SetEnemyToBattleList(enemyIdWave1);
        //random position enemy
        RandomPosition();

        SetWarriorToBattleList();



        GenerateSprite();
    }
Esempio n. 24
0
        public static void ReadArray(JsonReader reader, object context, BeginObjectDelegate begin, ParserDelegate parser, EndObjectDelegate end)
        {
            System.Diagnostics.Debug.Assert(reader.Token == JsonToken.ArrayStart);

            // parse the elements
            reader.Read();
            while (reader.Token != JsonToken.ArrayEnd)
            {
                if (reader.Token == JsonToken.ObjectStart)
                {
                    ReadObject(reader, context, begin, parser, end);
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    ReadArray(reader, context, parser);
                }
                else
                {
                    parser(reader, context);
                }

                reader.Read();
            }
        }
 public JsonKeyControlLoader(JsonReader reader)
 {
     dummyKeyControl = new DummyKeyControl();
     JsonMapper jsonMapper = new JsonMapper();
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ObjectEnd)
         {
             break;
         }
         if (reader.Token == JsonToken.PropertyName)
         {
             switch (reader.Value as string)
             {
                 case "walkKeyBinding":
                     dummyKeyControl.walkKeyBinding = jsonMapper.ToObject<Dictionary<string, string>>(reader);
                     break;
                 case "layBombKey":
                     dummyKeyControl.layBombKey = jsonMapper.ToObject<string>(reader);
                     break;
             }
         }
     }
 }
Esempio n. 26
0
    static bool SetData <T>(string s, List <object> a_refContainer)
    {
        bool bResult = true;

        try
        {
            // 필요없는 문자열 제거
            int nStart = s.IndexOf("(");
            int nEnd   = s.IndexOf(");");
            ++nStart;

            string data = s.Substring(nStart, nEnd - nStart);

            // 실제 값 파싱
            List <string>         liValueName = new List <string>();         // 변수명
            List <List <string> > liValues    = new List <List <string> >(); // 변수값

            LitJson.JsonReader reader = new LitJson.JsonReader(data);
            var mapParsed             = LitJson.JsonMapper.ToObject(reader);
            var map = mapParsed["table"];

            var liName = map["cols"];
            var liVal  = map["rows"];

            // 임시 캐싱 : 테이블명
            for (int i = 0; i < liName.Count; ++i)
            {
                var m1 = liName[i];
                liValueName.Add((string)m1["label"]);
            }

            // 임시 캐싱 : 각 로우의 값들
            for (int i = 0; i < liVal.Count; ++i)
            {
                var m2 = liVal[i];
                var li = m2["c"];

                liValues.Add(new List <string>());

                for (int j = 0; j < li.Count; ++j)
                {
                    var v = li[j];

                    string value = string.Empty;

                    if (v.ContainsKey("f") == true)
                    {
                        value = (string)v["f"];
                    }
                    else
                    {
                        value = (string)v["v"];
                    }

                    if (value == string.Empty)
                    {
                        Debug.LogError("table error");
                    }

                    liValues[i].Add(value);
                }
            }

            // 캐싱한 값으로부터 클래스 생성
            int nValCount = liValues.Count;

            for (int i = 0; i < nValCount; ++i)
            {
                T val = (T)GetInstance(typeof(T).FullName, liValues[i].ToArray());
                a_refContainer.Add(val);
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            Debug.LogError("Table Value error");
            bResult = false;
        }

        return(bResult);
    }
Esempio n. 27
0
 private void ReadInterfaces(JsonReader reader, ClassModel cClass)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.String)
         {
             string type = reader.Value.ToString();
             if (cClass.Implements == null) cClass.Implements = new List<string>();
             cClass.Implements.Add(type);
         }
     }
 }
Esempio n. 28
0
 private void ReadMetas(JsonReader reader, MemberModel member)
 {
     reader.SkipArray();
 }
Esempio n. 29
0
        public static T ToObject <T> (string json)
        {
            JsonReader reader = new JsonReader(json);

            return((T)ReadValue(typeof(T), reader));
        }
Esempio n. 30
0
        private void ReadProperty(JsonReader reader, ClassModel cClass)
        {
            MemberModel member = new MemberModel();
            member.Flags = FlagType.Variable;

            MemberModel getter = null;
            MemberModel setter = null;
            string prop = null;
            string name = null;
            string doc = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": name = val; break;
                        case "docstring": doc = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        //case "propertyattributes": ReadAttributes(reader, member); break;
                        //case "metainfo": reader.SkipArray(); break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "getter": 
                            getter = ReadMethod(reader, cClass);
                            getter.Flags |= FlagType.Getter;
                            break;
                        case "setter":
                            setter = ReadMethod(reader, cClass);
                            setter.Flags |= FlagType.Setter;
                            break;
                        default: reader.SkipObject(); break;
                    }
                }
            }
            if (getter != null)
            {
                getter.Name = name;
                getter.Comments = doc;
                getter.Flags &= ~FlagType.Function;
            }
            if (setter != null)
            {
                setter.Name = name;
                if (getter == null) setter.Comments = doc;
                setter.Flags &= ~FlagType.Function;
            }
        }
Esempio n. 31
0
        private void ReadParameters(JsonReader reader, MemberModel member)
        {
            member.Parameters = new List<MemberModel>();

            while (reader.Read())
            {
                if (reader.Token == JsonToken.ArrayEnd) break;
                if (reader.Token == JsonToken.ObjectStart) ReadParameter(reader, member);
            }
        }
Esempio n. 32
0
        private static object ReadValue(Type inst_type, JsonReader reader)
        {
            reader.Read();

            if (reader.Token == JsonToken.ArrayEnd)
            {
                return(null);
            }

            Type underlying_type = Nullable.GetUnderlyingType(inst_type);
            Type value_type      = underlying_type ?? inst_type;

            if (reader.Token == JsonToken.Null)
            {
                if (inst_type.IsClass || underlying_type != null)
                {
                    return(null);
                }

                throw new JsonException(String.Format(
                                            "Can't assign null to an instance of type {0}",
                                            inst_type));
            }

            if (reader.Token == JsonToken.Double ||
                reader.Token == JsonToken.Int ||
                reader.Token == JsonToken.Long ||
                reader.Token == JsonToken.String ||
                reader.Token == JsonToken.Boolean)
            {
                Type json_type = reader.Value.GetType();

                if (value_type.IsAssignableFrom(json_type))
                {
                    return(reader.Value);
                }

                // If there's a custom importer that fits, use it
                if (custom_importers_table.ContainsKey(json_type) &&
                    custom_importers_table[json_type].ContainsKey(
                        value_type))
                {
                    ImporterFunc importer =
                        custom_importers_table[json_type][value_type];

                    return(importer(reader.Value));
                }

                // Maybe there's a base importer that works
                if (base_importers_table.ContainsKey(json_type) &&
                    base_importers_table[json_type].ContainsKey(
                        value_type))
                {
                    ImporterFunc importer =
                        base_importers_table[json_type][value_type];

                    return(importer(reader.Value));
                }

                // Maybe it's an enum
                if (value_type.IsEnum)
                {
                    return(Enum.ToObject(value_type, reader.Value));
                }

                // Try using an implicit conversion operator
                MethodInfo conv_op = GetConvOp(value_type, json_type);

                if (conv_op != null)
                {
                    return(conv_op.Invoke(null,
                                          new object[] { reader.Value }));
                }

                // No luck
                throw new JsonException(String.Format(
                                            "Can't assign value '{0}' (type {1}) to type {2}",
                                            reader.Value, json_type, inst_type));
            }

            object instance = null;

            if (reader.Token == JsonToken.ArrayStart)
            {
                AddArrayMetadata(inst_type);
                ArrayMetadata t_data = array_metadata[inst_type];

                if (!t_data.IsArray && !t_data.IsList)
                {
                    throw new JsonException(String.Format(
                                                "Type {0} can't act as an array",
                                                inst_type));
                }

                IList list;
                Type  elem_type;

                if (!t_data.IsArray)
                {
                    list      = (IList)Activator.CreateInstance(inst_type);
                    elem_type = t_data.ElementType;
                }
                else
                {
                    list      = new ArrayList();
                    elem_type = inst_type.GetElementType();
                }

                while (true)
                {
                    object item = ReadValue(elem_type, reader);
                    if (item == null && reader.Token == JsonToken.ArrayEnd)
                    {
                        break;
                    }

                    list.Add(item);
                }

                if (t_data.IsArray)
                {
                    int n = list.Count;
                    instance = Array.CreateInstance(elem_type, n);

                    for (int i = 0; i < n; i++)
                    {
                        ((Array)instance).SetValue(list[i], i);
                    }
                }
                else
                {
                    instance = list;
                }
            }
            else if (reader.Token == JsonToken.ObjectStart)
            {
                AddObjectMetadata(value_type);
                ObjectMetadata t_data = object_metadata[value_type];

                instance = Activator.CreateInstance(value_type);

                while (true)
                {
                    reader.Read();

                    if (reader.Token == JsonToken.ObjectEnd)
                    {
                        break;
                    }

                    string property = (string)reader.Value;

                    if (t_data.Properties.ContainsKey(property))
                    {
                        PropertyMetadata prop_data =
                            t_data.Properties[property];

                        if (prop_data.IsField)
                        {
                            ((FieldInfo)prop_data.Info).SetValue(
                                instance, ReadValue(prop_data.Type, reader));
                        }
                        else
                        {
                            PropertyInfo p_info =
                                (PropertyInfo)prop_data.Info;

                            if (p_info.CanWrite)
                            {
                                p_info.SetValue(
                                    instance,
                                    ReadValue(prop_data.Type, reader),
                                    null);
                            }
                            else
                            {
                                ReadValue(prop_data.Type, reader);
                            }
                        }
                    }
                    else
                    {
                        if (!t_data.IsDictionary)
                        {
                            if (!reader.SkipNonMembers)
                            {
                                throw new JsonException(String.Format(
                                                            "The type {0} doesn't have the " +
                                                            "property '{1}'",
                                                            inst_type, property));
                            }
                            else
                            {
                                ReadSkip(reader);
                                continue;
                            }
                        }

                        ((IDictionary)instance).Add(
                            property, ReadValue(
                                t_data.ElementType, reader));
                    }
                }
            }

            return(instance);
        }
Esempio n. 33
0
 private void ReadProperties(JsonReader reader, ClassModel cClass)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.ObjectStart) ReadProperty(reader, cClass);
     }
 }
Esempio n. 34
0
 private static void ReadSkip(JsonReader reader)
 {
     ToWrapper(
         delegate { return(new JsonMockWrapper()); }, reader);
 }
Esempio n. 35
0
        private static IJsonWrapper ReadValue(WrapperFactory factory,
                                              JsonReader reader)
        {
            reader.Read();

            if (reader.Token == JsonToken.ArrayEnd ||
                reader.Token == JsonToken.Null)
            {
                return(null);
            }

            IJsonWrapper instance = factory();

            if (reader.Token == JsonToken.String)
            {
                instance.SetString((string)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Double)
            {
                instance.SetDouble((double)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Int)
            {
                instance.SetInt((int)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Long)
            {
                instance.SetLong((long)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Boolean)
            {
                instance.SetBoolean((bool)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.ArrayStart)
            {
                instance.SetJsonType(JsonType.Array);

                while (true)
                {
                    IJsonWrapper item = ReadValue(factory, reader);
                    if (item == null && reader.Token == JsonToken.ArrayEnd)
                    {
                        break;
                    }

                    ((IList)instance).Add(item);
                }
            }
            else if (reader.Token == JsonToken.ObjectStart)
            {
                instance.SetJsonType(JsonType.Object);

                while (true)
                {
                    reader.Read();

                    if (reader.Token == JsonToken.ObjectEnd)
                    {
                        break;
                    }

                    string property = (string)reader.Value;

                    ((IDictionary)instance)[property] = ReadValue(
                        factory, reader);
                }
            }

            return(instance);
        }
Esempio n. 36
0
 public static JsonData ToObject(JsonReader reader)
 {
     return((JsonData)ToWrapper(
                delegate { return new JsonData(); }, reader));
 }
Esempio n. 37
0
 public static T ToObject <T> (JsonReader reader)
 {
     return((T)ReadValue(typeof(T), reader));
 }
Esempio n. 38
0
        public static T ToObject <T> (TextReader reader)
        {
            JsonReader json_reader = new JsonReader(reader);

            return((T)ReadValue(typeof(T), json_reader));
        }
Esempio n. 39
0
 private List<String> ReadNames(JsonReader reader)
 {
     List<string> values = new List<string>();
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.String)
         {
             string val = CleanType(reader.Value.ToString());
             values.Add(val);
         }
     }
     return values;
 }
Esempio n. 40
0
 public static IJsonWrapper ToWrapper(WrapperFactory factory,
                                      JsonReader reader)
 {
     return(ReadValue(factory, reader));
 }
Esempio n. 41
0
        public static object ToObject(string json, Type t)
        {
            JsonReader reader = new JsonReader(json);

            return(ReadValue(t, reader));
        }
Esempio n. 42
0
 private static string ReadDefaults(JsonReader reader)
 {
     string prop = null;
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ObjectEnd) break;
         if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
         else if (reader.Token == JsonToken.String)
         {
             string val = reader.Value.ToString();
             switch (prop)
             {
                 case "last_latest": return val; 
             }
         }
         else if (reader.Token == JsonToken.ObjectStart)
         {
             if (prop == "display") ReadDisplay(reader);
             else reader.SkipObject();
         }
         else if (reader.Token == JsonToken.ArrayStart)
         {
             reader.SkipArray();
         }
     }
     return "";
 }
Esempio n. 43
0
        private MemberModel ReadMethod(JsonReader reader, ClassModel cClass)
        {
            MemberModel member = new MemberModel();
            member.Flags = FlagType.Function;

            string prop = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": 
                            if (!val.StartsWith("__op_")) {
                                member.Name = val; 
                                cClass.Members.Add(member); 
                            }
                            break;
                        case "returntype": if (member.Type == null) member.Type = CleanType(val); break;
                        case "docstring": member.Comments = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        case "methodattributes": 
                            ReadAttributes(reader, member); 
                            if ((member.Flags & FlagType.Static) == 0) member.Flags |= FlagType.Dynamic;
                            break;
                        case "parameters": ReadParameters(reader, member); break;
                        case "metainfo": ReadMetas(reader, member); break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "templatetypes": ReadTemplateTypes(reader, member); break;
                        default: reader.SkipObject(); break;
                    }
                }
            }
            return member;
        }
Esempio n. 44
0
        private void DeserializeTable(SerializeTable table, LitJson.JsonReader reader)
        {
            string propertyName = string.Empty;

            while (reader.Read())
            {
                switch (reader.Token)
                {
                case JsonToken.ObjectStart:
                    if (table.Name == propertyName && table.IsRoot)
                    {       // nothing to do
                    }
                    else
                    {
                        DeserializeTable(table.AddChild(propertyName), reader);
                    }
                    //propertyName = string.Empty;
                    break;

                case JsonToken.PropertyName:
                    propertyName = (string)reader.Value;
                    break;

                case JsonToken.ObjectEnd:
                    return;


                case JsonToken.ArrayStart:
                    // for arrary and list
                    DeserializeTable(table.AddChild(propertyName), reader);
                    break;

                case JsonToken.ArrayEnd:
                    // for arrary and list
                    return;

                case JsonToken.Int:
                    //table.AddValue<int>(propertyName, (int)reader.Value);
                    table.AddValue <string>(propertyName, reader.Value.ToString());
                    propertyName = string.Empty;
                    break;

                case JsonToken.Long:
                    //table.AddValue<int>(propertyName, (int)reader.Value);
                    table.AddValue <string>(propertyName, reader.Value.ToString());
                    propertyName = string.Empty;
                    break;

                // case JsonToken.Single:
                //   //table.AddValue<float>(propertyName, (float)reader.Value);
                //   table.AddValue<string>(propertyName, reader.Value.ToString());
                //   propertyName = string.Empty;
                //   break;

                case JsonToken.Double:
                    //table.AddValue<double>(propertyName, (double)reader.Value);
                    table.AddValue <string>(propertyName, reader.Value.ToString());
                    propertyName = string.Empty;
                    break;

                case JsonToken.String:
                    //table.AddValue<string>(propertyName, (string)reader.Value);
                    table.AddValue <string>(propertyName, reader.Value.ToString());
                    propertyName = string.Empty;
                    break;

                case JsonToken.Boolean:
                    //table.AddValue<bool>(propertyName, (bool)reader.Value);
                    table.AddValue <string>(propertyName, reader.Value.ToString());
                    propertyName = string.Empty;
                    break;

                case JsonToken.Null:
                    break;
                }
            }
        }
Esempio n. 45
0
 private void ReadTemplateTypes(JsonReader reader, MemberModel member)
 {
     string prop = null;
     List<string> names = null;
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ObjectEnd) break;
         if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
         else if (reader.Token == JsonToken.String)
         {
             string val = reader.Value.ToString();
             switch (prop)
             {
                 case "type": member.Type = CleanType(val); break;
             }
         }
         else if (reader.Token == JsonToken.ArrayStart)
         {
             switch (prop)
             {
                 case "types": names = ReadNames(reader); break;
                 default: reader.SkipArray(); break;
             }
         }
     }
     if (names != null) 
         member.Type += ".<" + String.Join(",", names.ToArray()) + ">";
 }
Esempio n. 46
0
 public virtual void LoadFromString(string json, Serializable value)
 {
     reader = new JsonReader(json);
     base.Deserialize(value);
 }
Esempio n. 47
0
        private MemberModel ReadParameter(JsonReader reader, MemberModel member)
        {
            MemberModel para = new MemberModel("???", null, FlagType.Variable | FlagType.ParameterVar, Visibility.Public);
            member.Parameters.Add(para);

            string prop = null;
            bool isRest = false;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "hasdefault": para.Value = "?"; break; // TODO "hasdefault" is that used?
                        case "isvarargs": isRest = true; break;  // TODO "isvarargs" is that used?
                        case "name": para.Name = val; break;
                        case "type": if (para.Type == null) para.Type = CleanType(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "templatetypes": ReadTemplateTypes(reader, para); break;
                        default: reader.SkipObject(); break;
                    }
                }
            }
            if (isRest) para.Type = "..." + para.Type;
            return para;
        }
 public static IDictionary<String, object> ReadJsonRpcData(Stream inputStream)
 {
     IDictionary<string, object> data = new Dictionary<string, object>();
     using (StreamReader sr = new StreamReader(inputStream))
     {
         JsonReader reader = new JsonReader(sr);
         reader.Read();
         if (reader.Token != JsonToken.ObjectStart)
             throw new ParseException("Unexpected token read!");
         while (reader.Read())
         {
             if (reader.Token == JsonToken.ObjectEnd)
                 break;
             if (reader.Token != JsonToken.PropertyName)
                 throw new ParseException("Unexpected token read!");
             string field = (string)reader.Value;
             if (!reader.Read())
                 throw new ParseException("Unexpected end of stream!");
             switch (field)
             {
             case "id":
                 data["id"] = (string)reader.Value;
                 break;
             case "method":
                 data["method"] = (string)reader.Value;
                 break;
             case "params":
                 if (reader.Token == JsonToken.ObjectStart)
                 {
                     data["parameters"] = JSON.ReadObject(reader);
                 }
                 break;
             default:
                 break;
             }
         }
     }
     return data;
 }
Esempio n. 49
0
 private void ReadAttributes(JsonReader reader, MemberModel decl)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.String)
         {
             string val = reader.Value.ToString();
             switch (val)
             {
                 case "native": decl.Flags |= FlagType.Native; break;
                 case "static": decl.Flags |= FlagType.Static; break;
                 case "public": decl.Access |= Visibility.Public; break;
                 case "internal": decl.Access |= Visibility.Internal; break;
                 case "protected": decl.Access |= Visibility.Protected; break;
                 case "private": decl.Access |= Visibility.Private; break;
                 case "final": decl.Flags |= FlagType.Final; break;
                 case "operator": break;
                 case "supercall": break;
                 default: break;
             }
         }
     }
 }
Esempio n. 50
0
        private void ReadType(JsonReader reader)
        {
            // root/modules/types
            FileModel cFile;
            ClassModel cClass;
            cFile = new FileModel();
            cFile.HasPackage = true;
            cFile.Version = 3;
            cFile.Context = context;
            cClass = new ClassModel();
            cClass.Flags = FlagType.Class;
            MemberModel cDelegate = new MemberModel();
            List<string> names;

            string prop = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": cClass.Name = val; break;
                        case "source":
                            if (val.IndexOf('/') > 0) val = val.Substring(val.IndexOf('/') + 1);
                            cFile.FileName = path.Path + "::" + val;
                            break;
                        case "package":
                            if (val.ToLower() == "cocos2d") val = "cocos2d"; // random casing through source
                            cFile.Package = val; 
                            break;
                        case "baseType": cClass.ExtendsType = val; break;
                        case "delegateReturnType": cDelegate.Type = CleanType(val); break;
                        case "type":
                            switch (val)
                            {
                                case "CLASS": break;
                                case "INTERFACE": cClass.Flags |= FlagType.Interface; break;
                                case "STRUCT": cClass.Flags |= FlagType.Struct; break;
                                case "DELEGATE": cDelegate.Name = cClass.Name; break;
                            }
                            break;
                        case "docstring": cClass.Comments = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        case "classattributes": ReadAttributes(reader, cClass); break;
                        case "fields": ReadFields(reader, cClass); break;
                        case "methods": ReadMethods(reader, cClass); break;
                        case "properties": ReadProperties(reader, cClass); break;
                        case "metainfo": ReadMetas(reader, cClass); break;
                        case "imports": 
                            names = ReadNames(reader);
                            foreach (string name in names)
                                cFile.Imports.Add(new MemberModel(name, name, FlagType.Import, Visibility.Public)); 
                            break;
                        case "interfaces": ReadInterfaces(reader, cClass); break;
                        case "delegateTypes":
                            names = ReadNames(reader);
                            if (names.Count > 0)
                            {
                                cDelegate.Parameters = new List<MemberModel>();
                                foreach (string argType in names)
                                {
                                    cDelegate.Parameters.Add(
                                        new MemberModel("p" + cDelegate.Parameters.Count, argType, FlagType.ParameterVar, Visibility.Public));
                                }
                            }
                            break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "constructor": 
                            MemberModel ctor = ReadMethod(reader, cClass);
                            cClass.Constructor = ctor.Name;
                            break;
                        default: reader.SkipObject(); break;
                    }
                }
            }

            if (cFile.FileName == null) return;
            string key = cFile.FileName.ToUpper();
            if (files.ContainsKey(key)) cFile = files[key];
            else files.Add(key, cFile);

            if (cFile.Package.ToLower() == "system") // System classes tweaks
            {
                cFile.Package = "";
                if (cClass.Name == "Vector") cClass.Name = "Vector.<T>";
                if (cClass.Name == "Object") cClass.ExtendsType = "void";
            }
            if (cClass.Access == Visibility.Private) cClass.Access = Visibility.Public;

            if (cDelegate.Name != null)
            {
                if (cDelegate.Parameters == null) cDelegate.Parameters = new List<MemberModel>();
                cDelegate.Access = Visibility.Public;
                cDelegate.Flags = FlagType.Function | FlagType.Delegate;
                cDelegate.InFile = cFile;
                cDelegate.IsPackageLevel = true;
                cFile.Members.Add(cDelegate);
            }
            else
            {
                cClass.Type = CleanType(String.IsNullOrEmpty(cFile.Package) ? cClass.Name : cFile.Package + "." + cClass.Name);
                cClass.InFile = cFile;
                cFile.Classes.Add(cClass);
            }
        }
Esempio n. 51
0
        public static object ToObject(string json, Type ConvertType)
        {
            JsonReader reader = new JsonReader(json);

            return(ReadValue(ConvertType, reader));
        }
Esempio n. 52
0
    public static void ApplyPlayerSettings(ProjectBuildData data)
    {
        Type type = typeof(PlayerSettings);


        var types = typeof(PlayerSettings).GetNestedTypes();

        foreach (var nested in types)
        {
            if (!data.Settings.ContainsKey(nested.Name))
            {
                continue;
            }

            string val    = data.Settings[nested.Name];
            var    reader = new LitJson.JsonReader(val);
            while (reader.Read())
            {
                switch (reader.Token)
                {
                case LitJson.JsonToken.PropertyName:
                {
                    string key = reader.Value.ToString();

                    reader.Read();

                    var info = nested.GetProperty(key);
                    if (info == null || !info.CanWrite)
                    {
                        Debug.LogWarning("ingore property:" + key);
                        continue;
                    }
                    if (info.PropertyType == typeof(string))
                    {
                        info.SetValue(null, reader.Value, null);
                    }
                    else if (info.PropertyType == typeof(bool))
                    {
                        info.SetValue(null, bool.Parse(reader.Value.ToString()), null);
                    }
                    else if (info.PropertyType == typeof(int))
                    {
                        info.SetValue(null, int.Parse(reader.Value.ToString()), null);
                    }
                    else if (info.PropertyType.IsEnum)
                    {
                        info.SetValue(null, Enum.Parse(info.PropertyType, reader.Value.ToString()), null);
                    }
                    else
                    {
                        Debug.LogWarning("unidentifiable property named:" + key + " type:" + info.PropertyType.Name);
                    }

                    break;
                }
                }
            }
        }

        foreach (var col in data.Settings)
        {
            PropertyInfo info = type.GetProperty(col.Key);
            if (info == null || !info.CanWrite)
            {
                Debug.LogWarning("ignore property:" + col.Key);
                continue;
            }

            Debug.LogWarning("set property:" + col.Key);
            if (info.PropertyType == typeof(string))
            {
                info.SetValue(null, col.Value, null);
            }
            else if (info.PropertyType == typeof(bool))
            {
                info.SetValue(null, bool.Parse(col.Value), null);
            }
            else if (info.PropertyType == typeof(int))
            {
                info.SetValue(null, int.Parse(col.Value), null);
            }
            else if (info.PropertyType.IsEnum)
            {
                info.SetValue(null, Enum.Parse(info.PropertyType, col.Value), null);
            }
            else
            {
                Debug.LogWarning("unidentifiable field named:" + col.Key + " type:" + info.PropertyType.Name);
            }
        }

        if (data.Settings.ContainsKey("Icons"))
        {
            string icons            = data.Settings["Icons"];
            var    iconsList        = icons.Split(',');
            var    iconsTextureList = new List <Texture2D>();
            foreach (var str in iconsList)
            {
                var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(str);
                iconsTextureList.Add(texture);
            }

            var group     = GetBuildGroupByTarget(data.Target);
            var iconSizes = PlayerSettings.GetIconSizesForTargetGroup(group);
            if (iconSizes.Length > iconsTextureList.Count)
            {
                int count = iconSizes.Length - iconsTextureList.Count;
                for (int i = 0; i < count; i++)
                {
                    iconsTextureList.Add(null);
                }
            }
            PlayerSettings.SetIconsForTargetGroup(group, iconsTextureList.ToArray());
        }

        ApplySelectedScene(data.Scenes);

        if (data.DebugBuild)
        {
            EditorUserBuildSettings.development     = data.DebugBuild;
            EditorUserBuildSettings.connectProfiler = true;
            EditorUserBuildSettings.allowDebugging  = true;
        }
        else
        {
            EditorUserBuildSettings.development     = false;
            EditorUserBuildSettings.connectProfiler = false;
            EditorUserBuildSettings.allowDebugging  = false;
        }

        PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, data.SymbolDefine);
        PlayerSettings.bundleVersion = data.Version;

        AssetDatabase.Refresh();
    }