From 2d678fdc0a013d0e2d8e43c15987bfd1346e3277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Wolff?= Date: Sun, 8 Apr 2018 19:32:19 +0200 Subject: [PATCH 1/3] changed `WriteJsonValue` to switch with pattern --- Ooui/JsonConvert.cs | 92 +++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/Ooui/JsonConvert.cs b/Ooui/JsonConvert.cs index 1eddd06..8646262 100644 --- a/Ooui/JsonConvert.cs +++ b/Ooui/JsonConvert.cs @@ -2,6 +2,8 @@ namespace Ooui { + using static System.FormattableString; + class JsonConvert { public static void WriteJsonString (System.IO.TextWriter w, string s) @@ -36,60 +38,44 @@ namespace Ooui public static void WriteJsonValue (System.IO.TextWriter w, object value) { - if (value == null) { - w.Write ("null"); - return; + switch(value) { + case string s: + WriteJsonString (w, s); + break; + case Array a: + w.Write ('['); + var head = ""; + foreach (var o in a) { + w.Write (head); + WriteJsonValue (w, o); + head = ","; + } + w.Write (']'); + break; + case EventTarget e: + w.Write ('\"'); + w.Write (e.Id); + w.Write ('\"'); + break; + case Color c: + WriteJsonString (w, c.ToString()); + break; + case double d: + w.Write (Invariant ($"{d}")); + break; + case int i: + w.Write (Invariant ($"{i}")); + break; + case float f: + w.Write (Invariant ($"{f}")); + break; + case null: + w.Write ("null"); + break; + default: + w.Write (Newtonsoft.Json.JsonConvert.SerializeObject (value)); + break; } - 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)); - return; - } - - if (value is int) { - w.Write (((int)value).ToString (icult)); - return; - } - - if (value is float) { - w.Write (((float)value).ToString (icult)); - return; - } - - w.Write (Newtonsoft.Json.JsonConvert.SerializeObject (value)); } public static string SerializeObject (object value) From 837d5ff5233cb3d9930ff25d1457e4900758937f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Wolff?= Date: Sun, 8 Apr 2018 20:59:02 +0200 Subject: [PATCH 2/3] Added unit tests --- Ooui/JsonConvert.cs | 2 +- Tests/JsonTests.cs | 91 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/Ooui/JsonConvert.cs b/Ooui/JsonConvert.cs index 8646262..faa5932 100644 --- a/Ooui/JsonConvert.cs +++ b/Ooui/JsonConvert.cs @@ -4,7 +4,7 @@ namespace Ooui { using static System.FormattableString; - class JsonConvert + public class JsonConvert { public static void WriteJsonString (System.IO.TextWriter w, string s) { diff --git a/Tests/JsonTests.cs b/Tests/JsonTests.cs index 37c637a..5ac373f 100644 --- a/Tests/JsonTests.cs +++ b/Tests/JsonTests.cs @@ -11,6 +11,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Ooui; using System.IO; using System.Text.RegularExpressions; +using System.Text; namespace Tests { @@ -50,5 +51,95 @@ namespace Tests "{\"m\":\"call\",\"id\":\"⦙\",\"k\":\"insertBefore\",\"v\":[\"⦙\",null]}" + "{\"m\":\"listen\",\"id\":\"⦙\",\"k\":\"click\"}", NoId (sw.ToString ())); } + + [TestMethod] + public void JsonConvertValueNull () + { + TextWriter w = new InMemoryTextWriter (); + JsonConvert.WriteJsonValue (w, null); + Assert.AreEqual ("null", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueString () + { + TextWriter w = new InMemoryTextWriter (); + JsonConvert.WriteJsonValue (w, "string"); + Assert.AreEqual ("\"string\"", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueArray () + { + TextWriter w = new InMemoryTextWriter(); + JsonConvert.WriteJsonValue (w, new[] { 1, 2, 3 }); + Assert.AreEqual("[1,2,3]", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueEventTarget () + { + TextWriter w = new InMemoryTextWriter (); + var textNode = new TextNode (); + JsonConvert.WriteJsonValue (w, textNode); + Assert.AreEqual ($"\"{textNode.Id}\"", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueColor () + { + TextWriter w = new InMemoryTextWriter (); + var color = new Color (255, 255, 255, 0); + JsonConvert.WriteJsonValue (w, color); + Assert.AreEqual ("\"rgba(255,255,255,0)\"", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueDouble () + { + TextWriter w = new InMemoryTextWriter (); + double d = 4.5; + JsonConvert.WriteJsonValue (w, d); + Assert.AreEqual ("4.5", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueInt () + { + TextWriter w = new InMemoryTextWriter (); + int i = 45; + JsonConvert.WriteJsonValue (w, i); + Assert.AreEqual ("45", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueFloat () + { + TextWriter w = new InMemoryTextWriter (); + float f = 45.5f; + JsonConvert.WriteJsonValue (w, f); + Assert.AreEqual ("45.5", w.ToString ()); + } + + [TestMethod] + public void JsonConvertValueOther () + { + TextWriter w = new InMemoryTextWriter (); + JsonConvert.WriteJsonValue (w, new { foo = "bar" }); + Assert.AreEqual ("{\"foo\":\"bar\"}", w.ToString ()); + } + + class InMemoryTextWriter : TextWriter + { + private StringBuilder builder = new StringBuilder (); + + public InMemoryTextWriter() { } + + public override void Write(char value) => builder.Append(value); + + public override string ToString() => builder.ToString(); + + public override Encoding Encoding => Encoding.UTF8; + } } } From 0eb74015f4c0ce682405bc8a38a35662b2ae0010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Wolff?= Date: Sun, 15 Apr 2018 20:13:57 +0200 Subject: [PATCH 3/3] Changed Invariant calls to good ol' ToString --- Ooui/JsonConvert.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Ooui/JsonConvert.cs b/Ooui/JsonConvert.cs index faa5932..0fd22b4 100644 --- a/Ooui/JsonConvert.cs +++ b/Ooui/JsonConvert.cs @@ -2,8 +2,6 @@ namespace Ooui { - using static System.FormattableString; - public class JsonConvert { public static void WriteJsonString (System.IO.TextWriter w, string s) @@ -58,16 +56,16 @@ namespace Ooui w.Write ('\"'); break; case Color c: - WriteJsonString (w, c.ToString()); + WriteJsonString (w, c.ToString ()); break; case double d: - w.Write (Invariant ($"{d}")); + w.Write (d.ToString (System.Globalization.CultureInfo.InvariantCulture)); break; case int i: - w.Write (Invariant ($"{i}")); + w.Write (i.ToString (System.Globalization.CultureInfo.InvariantCulture)); break; case float f: - w.Write (Invariant ($"{f}")); + w.Write (f.ToString (System.Globalization.CultureInfo.InvariantCulture)); break; case null: w.Write ("null");