changed `WriteJsonValue` to switch with pattern

This commit is contained in:
Loïc Wolff 2018-04-08 19:32:19 +02:00
parent 07bc6bcf14
commit 2d678fdc0a
1 changed files with 39 additions and 53 deletions

View File

@ -2,6 +2,8 @@
namespace Ooui namespace Ooui
{ {
using static System.FormattableString;
class JsonConvert class JsonConvert
{ {
public static void WriteJsonString (System.IO.TextWriter w, string s) public static void WriteJsonString (System.IO.TextWriter w, string s)
@ -36,18 +38,11 @@ namespace Ooui
public static void WriteJsonValue (System.IO.TextWriter w, object value) public static void WriteJsonValue (System.IO.TextWriter w, object value)
{ {
if (value == null) { switch(value) {
w.Write ("null"); case string s:
return;
}
var s = value as string;
if (s != null) {
WriteJsonString (w, s); WriteJsonString (w, s);
return; break;
} case Array a:
var a = value as Array;
if (a != null) {
w.Write ('['); w.Write ('[');
var head = ""; var head = "";
foreach (var o in a) { foreach (var o in a) {
@ -56,40 +51,31 @@ namespace Ooui
head = ","; head = ",";
} }
w.Write (']'); w.Write (']');
return; break;
} case EventTarget e:
var e = value as EventTarget;
if (e != null) {
w.Write ('\"'); w.Write ('\"');
w.Write (e.Id); w.Write (e.Id);
w.Write ('\"'); w.Write ('\"');
return; break;
} case Color c:
WriteJsonString (w, c.ToString());
if (value is Color) { break;
WriteJsonString (w, ((Color)value).ToString ()); case double d:
return; w.Write (Invariant ($"{d}"));
} break;
case int i:
var icult = System.Globalization.CultureInfo.InvariantCulture; w.Write (Invariant ($"{i}"));
break;
if (value is double) { case float f:
w.Write (((double)value).ToString (icult)); w.Write (Invariant ($"{f}"));
return; break;
} case null:
w.Write ("null");
if (value is int) { break;
w.Write (((int)value).ToString (icult)); default:
return;
}
if (value is float) {
w.Write (((float)value).ToString (icult));
return;
}
w.Write (Newtonsoft.Json.JsonConvert.SerializeObject (value)); w.Write (Newtonsoft.Json.JsonConvert.SerializeObject (value));
break;
}
} }
public static string SerializeObject (object value) public static string SerializeObject (object value)