public void TestFormatMultipleFields() { var o = new JsonObject(); o.AddField("foo", "happy"); o.AddField("bar", 73); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": \"happy\", \"bar\": 73 }")); }
public void TestFormatComplex() { var f1 = new JsonObject(); f1.AddField("bar", 1234); f1.AddField("bool", true); var f2 = new JsonObject(); f2.AddField("bar", 4321); f2.AddField("bool", false); var o = new JsonObject(); o.AddField("foo", new[] { f1, f2 }); o.AddField("label", "happy"); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": [ { \"bar\": 1234, \"bool\": true }, { \"bar\": 4321, \"bool\": false } ], \"label\": \"happy\" }")); }
public void TestFormatFalseField() { var o = new JsonObject(); o.AddField("foo", false); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": false }")); }
public void TestFormatNullField() { var o = new JsonObject(); o.AddField("foo", (string)null); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": null }")); }
public void TestFormatIntegerField() { var o = new JsonObject(); o.AddField("foo", 93); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": 93 }")); }
public void TestFormatDecimalField() { var o = new JsonObject(); o.AddField("foo", 3.14159); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": 3.14159 }")); }
public void TestFormatStringFieldQuote() { var o = new JsonObject(); o.AddField("foo", "1\"2\"3"); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": \"1\\\"2\\\"3\" }")); }
public void TestFormatStringFieldNewLine() { var o = new JsonObject(); o.AddField("foo", "1\r\n2"); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": \"1\\n2\" }")); }
public void TestFormatStringField() { var o = new JsonObject(); o.AddField("foo", "bar"); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": \"bar\" }")); }
public void TestFormatNestedObject() { var o = new JsonObject(); var f = new JsonObject(); f.AddField("bar", 1234); o.AddField("foo", f); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": { \"bar\": 1234 } }")); }
public void TestFormatNestedArray() { var o = new JsonObject(); var f1 = new JsonObject(); var f2 = new JsonObject(); f1.AddField("bar", 1234); f2.AddField("bar", 4321); o.AddField("foo", new[] { f1, f2 }); var str = o.ToString(); Assert.That(str, Is.EqualTo("{ \"foo\": [ { \"bar\": 1234 }, { \"bar\": 4321 } ] }")); }
static void MergeRecur(JsonObject left, JsonObject right) { if (right.type == JsonObject.Type.OBJECT) { for (int i = 0; i < right.list.Count; i++) { if (right.keys[i] != null) { string key = (string)right.keys[i]; JsonObject val = (JsonObject)right.list[i]; if (val.type == JsonObject.Type.ARRAY || val.type == JsonObject.Type.OBJECT) { if (left.HasField(key)) { MergeRecur(left[key], val); } else { left.AddField(key, val); } } else { if (left.HasField(key)) { left.SetField(key, val); } else { left.AddField(key, val); } } } } } // else left.list.Add(right.list); }
/// <summary> /// Serializes the value from <paramref name="model"/> into <paramref name="jsonObject"/>. /// </summary> public void Serialize(JsonObject jsonObject, ModelBase model) { var value = GetValue(model); if (_hasIgnoreValue && Object.Equals(value, _ignoreValue)) { return; } if (_converter != null) { var result = _converter.Convert(ref value); if (!String.IsNullOrEmpty(result)) { throw new InvalidOperationException("Cannot convert " + Property.PropertyName); } } jsonObject.AddField(JsonFieldName, Type, value); }
public static void Export(string filename) { var json = new JsonObject(); foreach (Color color in Enum.GetValues(typeof(Color))) { if (color == Color.None) { continue; } var value = GetColor(color); var colorValue = String.Format("#{0:X2}{1:X2}{2:X2}", value.R, value.G, value.B); json.AddField(color.ToString(), colorValue); } using (var file = File.OpenWrite(filename)) { using (var writer = new StreamWriter(file)) { writer.Write(json.ToString(2)); } } }
static void MergeRecur(JsonObject left, JsonObject right) { if(right.type == JsonObject.Type.OBJECT) { for(int i = 0; i < right.list.Count; i++) { if(right.keys[i] != null) { string key = (string)right.keys[i]; JsonObject val = (JsonObject)right.list[i]; if(val.type == JsonObject.Type.ARRAY || val.type == JsonObject.Type.OBJECT) { if(left.HasField(key)) MergeRecur(left[key], val); else left.AddField(key, val); } else { if(left.HasField(key)) left.SetField(key, val); else left.AddField(key, val); } } } }// else left.list.Add(right.list); }