コード例 #1
0
        public string Get(int id)
        {
            var jon = new Parson();

            jon.Age  = id;
            jon.Name = "Jon";

            return(JsonConvert.SerializeObject(jon));
        }
コード例 #2
0
        public void FormatTest()
        {
            Parson bob = new Parson
            {
                Name    = "Bob",
                Age     = 23,
                JobData = new Job
                {
                    Type     = JobType.PG,
                    JobCaria = 1
                }
            };
            Parson alice = new Parson
            {
                Name    = "Alice_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_1234",
                Age     = 35,
                JobData = new Job
                {
                    Type     = JobType.PM,
                    JobCaria = 5
                }
            };

            string    json;
            Parson    p;
            Stopwatch wc = Stopwatch.StartNew();

            Console.WriteLine("Json Deserializable");

            json = bob.ToDeserializableJson();
            Console.WriteLine(json);
            p = json.FromDeserializableJson <Parson>();
            Assert.True(p.Name == bob.Name);
            Assert.False(p.Name == alice.Name);

            wc.Stop();
            Console.WriteLine(wc.Elapsed.ToString());
            Console.WriteLine();
            Console.WriteLine("Json");
            wc.Restart();

            json = bob.ToJson(true);
            Console.WriteLine(json);
            p = json.FromDeserializableJson <Parson>();
            Assert.True(p.Name == bob.Name);
            Assert.False(p.Name == alice.Name);

            wc.Stop();
            Console.WriteLine(wc.Elapsed.ToString());
            Console.WriteLine();
            Console.WriteLine("Yaml");
            wc.Restart();

            string yaml;

            yaml = alice.ToYaml();
            Console.WriteLine(yaml);
            p = yaml.FromYaml <Parson>();
            Assert.True(p.Name == alice.Name);
            Assert.False(p.Name == bob.Name);

            wc.Stop();
            Console.WriteLine(wc.Elapsed.ToString());
            Console.WriteLine();
            Console.WriteLine("Bin");
            wc.Restart();

            byte[] bin;
            bin = alice.ToBinary();
            Console.WriteLine(Convert.ToBase64String(bin));
            p = bin.FromBinary <Parson>();
            Assert.True(p.Name == alice.Name);
            Assert.False(p.Name == bob.Name);

            wc.Stop();
            Console.WriteLine(wc.Elapsed.ToString());
            Console.WriteLine();
            Console.WriteLine("Bin Compress");
            wc.Restart();

            bin = alice.ToCompressBinary();
            Console.WriteLine(Convert.ToBase64String(bin));
            p = bin.FromCompressBinary <Parson>();
            Assert.True(p.Name == alice.Name);
            Assert.False(p.Name == bob.Name);

            wc.Stop();
            Console.WriteLine(wc.Elapsed.ToString());
        }
コード例 #3
0
        // Start is called before the first frame update
        void Start()
        {
            IParson data = new Parson("", 0);

            try
            {
                // Parsonクラスを自動でいい感じにjs環境に変数として取り込んでくれる
                var engine = new Engine().SetValue("gameObjectName", data);
                // Jsのコードを実行
                engine.Execute("gameObjectName.Name = 'ScriptEngine'")
                .Execute("gameObjectName.Age = 25");

                // Parsonクラスに自動でバインドしてくれる。
                Debug.Assert(data.Name == "ScriptEngine");
                Debug.Assert(data.Age == 25);

                // エンジンにSystemを登録
                engine = new Engine(cfg => cfg.AllowClr());

                // 関数を変数に格納
                engine.Execute("var concat = System.String.Concat;");
                // 実行
                var val = engine.GetValue("concat").Invoke("test", "abc");
                Debug.Assert(val.AsString() == "testabc");

                engine = new Engine();
                // 型を登録
                engine.SetValue("Parson", TypeReference.CreateTypeReference(engine, typeof(Parson)));
                // new でインスタンス作成
                engine.Execute("var p1 = new Parson('Test', 10)");
                // 変数を取得
                var val2 = engine.GetValue("p1").AsObject();
                Debug.Assert(val2.Get("Name").AsString() == "Test");
                Debug.Assert((int)val2.Get("Age").AsNumber() == 10);

                // static関数にアクセス
                engine.Execute("var p2 = Parson.Create('Bob', 30)");
                // 変数を取得
                var val3 = engine.GetValue("p2").AsObject();
                Debug.Assert(val3.Get("Name").AsString() == "Bob");
                Debug.Assert((int)val3.Get("Age").AsNumber() == 30);

                // 型を登録
                engine.SetValue("Hoge", TypeReference.CreateTypeReference(engine, typeof(Hoge)));
                // new でインスタンス作成
                engine.Execute("var go = Hoge.Go");
                // 変数を取得
                var val4 = engine.GetValue("go").AsNumber();
                Debug.Assert((Hoge)val4 == Hoge.Go);

                try
                {
                    engine = new Engine(new Action <Options>(options => options.TimeoutInterval(new TimeSpan(0, 0, 5))));
                    engine.Execute("while(true);");

                    Debug.LogError("Test Error!");
                }
                catch (TimeoutException e)
                {
                    Debug.Log("Test Clear!");
                }

#if UNITY_EDITOR
                // テストを終了
                EditorApplication.isPlaying = false;
                EditorUtility.DisplayDialog("", "テストが完了しました。", "OK");
#endif
            }
            catch (Exception e)
            {
                // エラー表示
                Debug.LogError(e);
            }
        }