changed `WriteJsonValue` to switch with pattern
This commit is contained in:
parent
07bc6bcf14
commit
2d678fdc0a
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Ooui
|
||||
{
|
||||
using static System.FormattableString;
|
||||
|
||||
class JsonConvert
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (value == null) {
|
||||
w.Write ("null");
|
||||
return;
|
||||
}
|
||||
var s = value as string;
|
||||
if (s != null) {
|
||||
switch(value) {
|
||||
case string s:
|
||||
WriteJsonString (w, s);
|
||||
return;
|
||||
}
|
||||
|
||||
var a = value as Array;
|
||||
if (a != null) {
|
||||
break;
|
||||
case Array a:
|
||||
w.Write ('[');
|
||||
var head = "";
|
||||
foreach (var o in a) {
|
||||
|
@ -56,40 +51,31 @@ namespace Ooui
|
|||
head = ",";
|
||||
}
|
||||
w.Write (']');
|
||||
return;
|
||||
}
|
||||
|
||||
var e = value as EventTarget;
|
||||
if (e != null) {
|
||||
break;
|
||||
case EventTarget e:
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static string SerializeObject (object value)
|
||||
|
|
Loading…
Reference in New Issue