public void EnumsAndValueTypes() { Assert.AreEqual(@"""Second""", JsonReflector.Reflect(TestEnum.Second)); Assert.AreEqual(@"null", JsonReflector.Reflect((TestEnum)100)); Assert.AreEqual(@"{""x"":3,""y"":5,""z"":100}", JsonReflector.Reflect(new Vector3(3.0f, 5.0f, 100.0f))); }
//---------------------------------------------------------------------------------------------------- public static void Query(RequestAdapter req, string path) { // if we have already cached the query then use that if (req.CachedContext != null) { var cache = req.CachedContext as CachedQuery; var query = cache.Q; if (cache.Reselect) { query.Select(); req.Respond(JsonReflector.Reflect(query.Execute())); query.Clear(); } else { req.Respond(JsonReflector.Reflect(query.Execute())); } } // otherwise execute it else { var query = new Query(path, Unium.Root).Select(); req.Respond(JsonReflector.Reflect(query.Execute())); } }
public void Objects() { // object Assert.AreEqual(@"{""a"":6,""b"":""hello""}", JsonReflector.Reflect(new { a = 6, b = "hello" })); // array Assert.AreEqual(@"[1,2,3,4]", JsonReflector.Reflect(new int[] { 1, 2, 3, 4 })); // list Assert.AreEqual(@"[1,2,3,4]", JsonReflector.Reflect(new List <int>() { 1, 2, 3, 4 })); // dictionary var dict = new Dictionary <string, int>() { { "k1", 1 }, { "k2", 2 }, { "k3", 3 }, }; Assert.AreEqual(@"{""k1"":1,""k2"":2,""k3"":3}", JsonReflector.Reflect(dict)); var nonStringKeys = new Dictionary <object, int>() { { new { x = 1 }, 1 }, { new { z = 1 }, 2 }, { new { fish = 1, z = 2 }, 3 }, }; Assert.AreEqual(@"{""{ x = 1 }"":1,""{ z = 1 }"":2,""{ fish = 1, z = 2 }"":3}", JsonReflector.Reflect(nonStringKeys)); }
public void Converters() { JsonSerialiser aSerializer = new SerialiseType(); JsonReflector.Add(typeof(A), aSerializer); Assert.AreEqual(@"TestReflection+A", JsonReflector.Reflect(new A())); Assert.AreEqual(@"TestReflection+B", JsonReflector.Reflect(new B())); }
//---------------------------------------------------------------------------------------------------- public static void HandlerStatus(RequestAdapter req, string path) { req.Respond(JsonReflector.Reflect(new { FPS = 1.0f / Time.smoothDeltaTime, RunningTime = Time.realtimeSinceStartup, Scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name, })); }
public void PrimitiveTypes() { Assert.AreEqual("null", JsonReflector.Reflect(null)); Assert.AreEqual("true", JsonReflector.Reflect(true)); Assert.AreEqual("7", JsonReflector.Reflect(7)); Assert.AreEqual("3.14", JsonReflector.Reflect(3.14)); Assert.AreEqual(@"""c""", JsonReflector.Reflect('c')); Assert.AreEqual(@"""hello""", JsonReflector.Reflect("hello")); // edge cases Assert.AreEqual(int.MaxValue.ToString(), JsonReflector.Reflect(int.MaxValue)); Assert.AreEqual(@"""':\\{}\""""", JsonReflector.Reflect(@"':\{}""")); }
//---------------------------------------------------------------------------------------------------- public static void HandlerAbout(RequestAdapter req, string path) { req.Respond(JsonReflector.Reflect(new { Unium = Unium.Version.ToString(2), Unity = Application.unityVersion, Mono = Environment.Version.ToString(), IsEditor = Application.isEditor, Product = Application.productName, Company = Application.companyName, Version = Application.version, IPAddress = Util.DetectPublicIPAddress(), FPS = 1.0f / Time.smoothDeltaTime, RunningTime = Time.realtimeSinceStartup, Scene = SceneManager.GetActiveScene().name, })); }
override public string Convert(object o) { var t = o as Transform; if (t == null) { return("null"); } var json = new JsonBuilder(); json.BeginObject(); json.Name("name"); json.StringValue(t.name); json.Name("position"); json.Value(JsonReflector.Reflect(t.position)); json.Name("rotation"); json.Value(JsonReflector.Reflect(t.rotation)); json.Name("scale"); json.Value(JsonReflector.Reflect(t.localScale)); json.Name("children"); json.BeginArray(); foreach (Transform c in t) { if (c != null) { json.StringValue(c.name); } } json.EndArray(); json.EndObject(); return(json.GetString()); }
//---------------------------------------------------------------------------------------------------- static void DownloadFile(RequestAdapter req, string filepath) { #if UNITY_ANDROID /* * var data = new WWW( filepath ); * * while( !data.isDone ) * { * Thread.Sleep( 10 ); * } * * if( string.IsNullOrEmpty( data.error ) ) * { * req.SetContentType( GetMimeType( filepath ) ); * req.Respond( data.bytes ); * } * else * { * req.Reject( ResponseCode.InternalServerError ); * } */ req.Reject(ResponseCode.InternalServerError); #else // System.IO if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory) { // list contents of directory var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c); req.Respond(JsonReflector.Reflect(files.ToArray())); } else { // dump bytes req.SetContentType(GetMimeType(filepath)); req.Respond(File.ReadAllBytes(filepath)); } #endif }
private static IEnumerator LoadScene(RequestAdapter req, string name) { var asyncOp = SceneManager.LoadSceneAsync(name, LoadSceneMode.Single); if (asyncOp == null) { req.Reject(ResponseCode.NotFound); } else { asyncOp.allowSceneActivation = true; while (asyncOp.isDone == false) { yield return(asyncOp); } req.Respond(JsonReflector.Reflect(new { scene = name })); } }
//---------------------------------------------------------------------------------------------------- static void DownloadFileNative(RequestAdapter req, string filepath) { // System.IO if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory) { // list contents of directory var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c); req.Respond(JsonReflector.Reflect(files.ToArray())); } else { // dump bytes req.SetContentType(GetMimeType(filepath)); req.Respond(File.ReadAllBytes(filepath)); } }
//---------------------------------------------------------------------------------------------------- public static void HandlerScene(RequestAdapter req, string path) { // if path empty then list all loaded scenes if (string.IsNullOrEmpty(path) || path.Length <= 1) { var scenes = new List <object>(); #if UNITY_EDITOR foreach (var scene in EditorBuildSettings.scenes) { scenes.Add(new { name = Path.GetFileNameWithoutExtension(scene.path), path = scene.path, enabled = scene.enabled }); } #else for (int i = 0; i < SceneManager.sceneCount; i++) { var scene = SceneManager.GetSceneAt(i); scenes.Add(new { name = scene.name, path = scene.path, enabled = true }); } #endif req.Respond(JsonReflector.Reflect(scenes.ToArray())); } // otherwise load scene else { UniumComponent.Singleton.StartCoroutine(LoadScene(req, path.Substring(1))); } }
public void Reply(object data) { Socket.Send(id, "data", JsonReflector.Reflect(data)); }
public static string ExecuteGQL(string queryString) { var query = new Query(queryString, Unium.Root).Select(); return(JsonReflector.Reflect(query.Execute())); }
private void Reply(object data) { SendImpl(ID, "data", JsonReflector.Reflect(data)); }