Example #1
0
        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);
        }
Example #2
0
        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));
        }