From e78bde556bdd03df20fd02b1140f4092116b1651 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 8 Mar 2018 13:28:28 -0800 Subject: [PATCH] Implement own Json --- Ooui/JsonConvert.cs | 161 ++++++++++++++++++++++++++++++++++++++++++++ Ooui/Message.cs | 152 ++++++++++++++++++----------------------- Ooui/UI.cs | 5 +- 3 files changed, 230 insertions(+), 88 deletions(-) create mode 100644 Ooui/JsonConvert.cs diff --git a/Ooui/JsonConvert.cs b/Ooui/JsonConvert.cs new file mode 100644 index 0000000..fdeeed2 --- /dev/null +++ b/Ooui/JsonConvert.cs @@ -0,0 +1,161 @@ +using System; + +namespace Ooui +{ + class JsonConvert + { + static void WriteJsonString (System.IO.TextWriter w, string s) + { + w.Write ('\"'); + for (var i = 0; i < s.Length; i++) { + var c = s[i]; + if (c == '\"') { + w.Write ("\\\""); + } + else if (c == '\r') { + w.Write ("\\r"); + } + else if (c == '\n') { + w.Write ("\\n"); + } + else if (c == '\t') { + w.Write ("\\t"); + } + else if (c == '\b') { + w.Write ("\\b"); + } + else if (c == '\\') { + w.Write ("\\"); + } + else { + w.Write (c); + } + } + w.Write ('\"'); + } + + public static void WriteJsonValue (System.IO.TextWriter w, object value) + { + if (value == null) { + w.Write ("null"); + return; + } + var s = value as string; + if (s != null) { + WriteJsonString (w, s); + return; + } + + var a = value as Array; + if (a != null) { + w.Write ('['); + var head = ""; + foreach (var o in a) { + w.Write (head); + WriteJsonValue (w, o); + head = ","; + } + w.Write (']'); + return; + } + + var e = value as EventTarget; + if (e != null) { + w.Write ('\"'); + w.Write (e.Id); + w.Write ('\"'); + return; + } + + if (value is Color) { + WriteJsonString (w, ((Color)value).ToString ()); + return; + } + + var icult = System.Globalization.CultureInfo.InvariantCulture; + + if (value is double) { + w.Write (((double)value).ToString (icult)); + } + + if (value is int) { + w.Write (((int)value).ToString (icult)); + } + + if (value is float) { + w.Write (((float)value).ToString (icult)); + } + + WriteJsonString (w, Convert.ToString (value, icult)); + } + + public static string SerializeObject (object value) + { + using (var sw = new System.IO.StringWriter ()) { + WriteJsonValue (sw, value); + return sw.ToString (); + } + } + + static object ReadJsonArray (string j, ref int i) + { + throw new NotImplementedException (); + } + + static object ReadJsonObject (string json, ref int i) + { + var e = json.Length; + while (i < e) { + while (i < e && char.IsWhiteSpace (json[i])) + i++; + if (i >= e) + throw new Exception ("JSON Unexpected end"); + var n = e - i; + i++; + } + throw new NotImplementedException (); + } + + static object ReadJsonString (string j, ref int i) + { + throw new NotImplementedException (); + } + + static object ReadJsonNumber (string j, ref int i) + { + throw new NotImplementedException (); + } + + static object ReadJsonValue (string json, ref int i) + { + var e = json.Length; + while (i < e && char.IsWhiteSpace (json[i])) + i++; + if (i >= e) + throw new Exception ("JSON Unexpected end"); + var n = e - i; + switch (json[i]) { + case '[': + return ReadJsonArray (json, ref i); + case '{': + return ReadJsonObject (json, ref i); + case '\"': + return ReadJsonString (json, ref i); + case 'f': + i += 5; + return false; + case 't': + i += 4; + return true; + default: + return ReadJsonNumber (json, ref i); + } + } + + public static object ReadJsonValue (string json, int startIndex) + { + var i = startIndex; + return ReadJsonValue (json, ref i); + } + } +} diff --git a/Ooui/Message.cs b/Ooui/Message.cs index 7254abd..0ba8798 100644 --- a/Ooui/Message.cs +++ b/Ooui/Message.cs @@ -36,91 +36,6 @@ namespace Ooui Value = value, }; - static void WriteJsonString (System.IO.TextWriter w, string s) - { - w.Write ('\"'); - for (var i = 0; i < s.Length; i++) { - var c = s[i]; - if (c == '\"') { - w.Write ("\\\""); - } - else if (c == '\r') { - w.Write ("\\r"); - } - else if (c == '\n') { - w.Write ("\\n"); - } - else if (c == '\t') { - w.Write ("\\t"); - } - else if (c == '\b') { - w.Write ("\\b"); - } - else if (c == '\\') { - w.Write ("\\"); - } - else { - w.Write (c); - } - } - w.Write ('\"'); - } - - static void WriteJsonValue (System.IO.TextWriter w, object value) - { - if (value == null) { - w.Write ("null"); - return; - } - var s = value as string; - if (s != null) { - WriteJsonString (w, s); - return; - } - - var a = value as Array; - if (a != null) { - w.Write ('['); - var head = ""; - foreach (var o in a) { - w.Write (head); - WriteJsonValue (w, o); - head = ","; - } - w.Write (']'); - return; - } - - var e = value as EventTarget; - if (e != null) { - w.Write ('\"'); - w.Write (e.Id); - w.Write ('\"'); - return; - } - - if (value is Color) { - WriteJsonString (w, ((Color)value).ToString ()); - return; - } - - var icult = System.Globalization.CultureInfo.InvariantCulture; - - if (value is double) { - w.Write (((double)value).ToString (icult)); - } - - if (value is int) { - w.Write (((int)value).ToString (icult)); - } - - if (value is float) { - w.Write (((float)value).ToString (icult)); - } - - WriteJsonString (w, Convert.ToString (value, icult)); - } - public void WriteJson (System.IO.TextWriter w) { w.Write ('{'); @@ -139,7 +54,7 @@ namespace Ooui w.Write (Key); if (Value != null) { w.Write ("\",\"v\":"); - WriteJsonValue (w, Value); + JsonConvert.WriteJsonValue (w, Value); w.Write ('}'); } else { @@ -154,6 +69,71 @@ namespace Ooui return sw.ToString (); } } + + public static Message FromJson (string json) + { + var m = new Message (); + + var i = 0; + var e = json.Length; + + while (i < e) { + while (i < e && (json[i]==',' || json[i]=='{' || char.IsWhiteSpace (json[i]))) + i++; + if (i >= e) + throw new Exception ("JSON Unexpected end"); + var n = e - i; + if (json[i] == '}') + break; + if (n > 4 && json[i] == '\"' && json[i+2] == '\"' && json[i+3] == ':') { + switch (json[i + 1]) { + case 'm': + if (json[i + 4] == '\"' && json[i + 5] == 'e') { + m.MessageType = MessageType.Event; + } + i += 5; + while (i < e && json[i] != '\"') i++; + i++; + break; + case 'k': { + i += 5; + var se = i; + while (se < e && json[se] != '\"') + se++; + m.Key = json.Substring (i, se - i); + i = se + 1; + } + break; + case 'v': + m.Value = JsonConvert.ReadJsonValue (json, i + 4); + break; + } + } + else if (n > 5 && json[i] == '\"' && json[i + 3] == '\"' && json[i + 4] == ':' && json[i+5] == '\"') { + switch (json[i + 1]) { + case 'i': { + i += 6; + var se = i; + while (se < e && json[se] != '\"') + se++; + m.TargetId = json.Substring (i, se - i); + i = se + 1; + } + break; + } + } + else { + throw new Exception ("JSON Expected property"); + } + } + + return m; + } + + public override string ToString () + { + return ToJson (); + } } [JsonConverter (typeof (StringEnumConverter))] diff --git a/Ooui/UI.cs b/Ooui/UI.cs index aff0c38..893ef63 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -464,8 +464,9 @@ namespace Ooui public static byte[] GetData (object obj) { - var r = Newtonsoft.Json.JsonConvert.SerializeObject (obj); - return System.Text.Encoding.UTF8.GetBytes (r); + var r = Ooui.JsonConvert.SerializeObject (obj); + var e = new UTF8Encoding (false); + return e.GetBytes (r); } public override void Respond (HttpListenerContext listenerContext, CancellationToken token)