public void Format_ReturnsFormattedLiteral() { // Arrange IJsonObject j0 = JsonObject.FromString("{foo: 'fooval', bar :[], baz: { baz2: [0,1,2, {}] }}"); IJsonObject j1 = JsonObject.Builder().Build(); IJsonObject j2 = JsonObject.OfNull(); IJsonFormatOptions opts = JsonFormatOptions.Builder().Indent(true).NewLine("\n").Build(); // Act string s0 = j0.Format(opts); string s1 = j1.Format(opts); string s2 = j2.Format(opts); // Assert Assert.AreEqual("{\n" + "\t\"foo\" : \"fooval\",\n" + "\t\"bar\" : [],\n" + "\t\"baz\" : {\n" + "\t\t\"baz2\" : [\n" + "\t\t\t0,\n" + "\t\t\t1,\n" + "\t\t\t2,\n" + "\t\t\t{}\n" + "\t\t]\n" + "\t}\n" + "}", s0); Assert.AreEqual("{}", s1); Assert.AreEqual("null", s2); }
private static void FormatHelperNewLine(IJsonFormatOptions opts, StringBuilder buff) { if (!opts.Indent) { return; } buff.Append(opts.NewLine); }
/// <summary> /// このJSONノードの整形されたリテラル表現を返します. /// </summary> /// <param name="opts">整形オプション.</param> public string Format(IJsonFormatOptions opts) { if (opts.Indent) { StringBuilder buff = new StringBuilder(); FormatHelper(this, opts, buff, 0); return(buff.ToString().TrimStart()); } else { return(ToString()); } }
public static void Main(string[] args) { // 1. Read JSON // JsonObject.FromXxxx methods read JSON from String, File and Stream. IJsonObject j1 = JsonObject.FromString("{foo: 123, bar: true, baz: \"hello\"}"); // IJsonObject#GetProperty() accesses JSON object's property. // NOTE: GetPropery() and XxxxValue() methods have another version // that can be specified fallback value. Console.WriteLine("j1 = {0}", j1); Console.WriteLine("foo = {0}", j1.GetProperty("foo").AsNumber()); Console.WriteLine("bar = {0}", j1.GetProperty("bar").AsBoolean()); Console.WriteLine("baz = {0}", j1.GetProperty("baz").AsString()); Console.WriteLine("baa exists? = {0}", j1.HasProperty("baa")); // 2. Build JSON // JsonObject.Builder() returns new builder instance. IJsonObject j2 = JsonObject .Builder() .Append("foo", "hello") .Append("bar", "hello", "bonjour", "こんにちは") .Append("baz", (b) => { // Lambda's argument is new builder // instance for nested object. b.Append("bazProp1", 1); b.Append("bazProp2", 2); }) .Build(); // The builder can be initialized with existed JSON. IJsonObject j3 = JsonObject .Builder(j2).Append("baa", 123).Build(); Console.WriteLine("j2 = {0}", j2); Console.WriteLine("j3 = {0}", j3); // 3. Format JSON IJsonFormatOptions opts = JsonFormatOptions .Builder() .Indent(true).SoftTabs(true).TabWidth(2) .Build(); Console.WriteLine("j3' = {0}", j3.Format(opts)); }
private static void FormatHelperIndent(IJsonFormatOptions opts, StringBuilder buff, int depth) { if (!opts.Indent || depth == 0) { return; } if (opts.SoftTabs) { for (int i = 0; i < opts.TabWidth * depth; i++) { buff.Append(' '); } } else { for (int i = 0; i < depth; i++) { buff.Append('\t'); } } }
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('}'); } }