public void AsArray1_ReturnsFallback() { // Arrange IJsonObject json = JsonObject.Of(true); // Act // Assert Assert.That(json.AsArray(JsonObject.Of("foo", "bar").AsArray()).Count, Is.EqualTo(2)); }
public void AsArray_ThrowsException() { // Arrange IJsonObject json = JsonObject.Of(true); // Act // Assert Assert.Throws <ApplicationException>(() => { json.AsArray(); }); }
public void Parse_WhenApplyToArrayLiteral_ReturnsArrayJsonObject() { // Arrange var p = new JsonParser(); // Act IJsonObject r0 = p.Parse(Input.FromString("[0.0, true]")); IJsonObject r1 = p.Parse(Input.FromString(" [0.0 ,true] ")); IJsonObject r2 = p.Parse(Input.FromString("[ null,'foo' ]")); IJsonObject r3 = p.Parse(Input.FromString(" [{} , \"foo\"] ")); // Assert Assert.That(r0.AsArray().Count, Is.EqualTo(2)); Assert.That(r0.AsArray()[0].AsNumber(), Is.EqualTo(0.0)); Assert.That(r0.AsArray()[1].AsBoolean(), Is.True); Assert.That(r1.AsArray().Count, Is.EqualTo(2)); Assert.That(r2.AsArray().Count, Is.EqualTo(2)); Assert.That(r3.AsArray().Count, Is.EqualTo(2)); Assert.That(r3.AsArray()[0].TypeIs(JsonObjectType.Object), Is.True); Assert.That(r3.AsArray()[1].AsString(), Is.EqualTo("foo")); }
public void AsArray1_IgnoresFallback() { // Arrange IJsonObject json = JsonObject.Of(2, 3, 4); // Act // Assert Assert.That(json .AsArray() .Select((arg) => arg.AsNumber()) .ToArray(), Is.EqualTo(new long[] { 2, 3, 4 })); }
private static void FormatHelper(IJsonObject json, IJsonFormatOptions opts, StringBuilder buff, int depth) { if (json.Type != JsonObjectType.Array && !json.TypeIs(JsonObjectType.Object)) { buff.Append(json.ToString()); return; } if (json.Type == JsonObjectType.Array) { buff.Append('['); bool empty = true; foreach (IJsonObject item in json.AsArray()) { if (empty) { empty = false; } else { buff.Append(','); } FormatHelperNewLine(opts, buff); FormatHelperIndent(opts, buff, depth + 1); FormatHelper(item, opts, buff, depth + 1); } if (!empty) { FormatHelperNewLine(opts, buff); FormatHelperIndent(opts, buff, depth); } buff.Append(']'); } else if (json.Type == JsonObjectType.Object) { buff.Append('{'); bool empty = true; foreach (IJsonProperty p in json.Properties) { if (empty) { empty = false; } else { buff.Append(','); } FormatHelperNewLine(opts, buff); FormatHelperIndent(opts, buff, depth + 1); buff.Append(Quotes(p.Name)).Append(' ').Append(':').Append(' '); FormatHelper(p.Value, opts, buff, depth + 1); } if (!empty) { FormatHelperNewLine(opts, buff); FormatHelperIndent(opts, buff, depth); } buff.Append('}'); } }