Merge pull request #129 from loicwolff/master

Merge minor changes to JsonConvert.cs
This commit is contained in:
Frank A. Krueger 2018-04-15 11:37:47 -07:00 committed by GitHub
commit d5cb647e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 54 deletions

View File

@ -2,7 +2,7 @@
namespace Ooui namespace Ooui
{ {
class JsonConvert public class JsonConvert
{ {
public static void WriteJsonString (System.IO.TextWriter w, string s) public static void WriteJsonString (System.IO.TextWriter w, string s)
{ {
@ -36,18 +36,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 +49,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 (d.ToString (System.Globalization.CultureInfo.InvariantCulture));
} break;
case int i:
var icult = System.Globalization.CultureInfo.InvariantCulture; w.Write (i.ToString (System.Globalization.CultureInfo.InvariantCulture));
break;
if (value is double) { case float f:
w.Write (((double)value).ToString (icult)); w.Write (f.ToString (System.Globalization.CultureInfo.InvariantCulture));
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)

View File

@ -11,6 +11,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ooui; using Ooui;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Text;
namespace Tests namespace Tests
{ {
@ -50,5 +51,95 @@ namespace Tests
"{\"m\":\"call\",\"id\":\"⦙\",\"k\":\"insertBefore\",\"v\":[\"⦙\",null]}" + "{\"m\":\"call\",\"id\":\"⦙\",\"k\":\"insertBefore\",\"v\":[\"⦙\",null]}" +
"{\"m\":\"listen\",\"id\":\"⦙\",\"k\":\"click\"}", NoId (sw.ToString ())); "{\"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;
}
} }
} }