12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
/*******************************************************************************

        Copyright: Copyright (C) 2008 Aaron Craelius & Kris Bell
                   All rights reserved

        License:   BSD style: $(LICENSE)

        version:   July 2008: Initial release

        Authors:   Aaron, Kris

*******************************************************************************/

module tango.text.json.Json;

private import tango.core.Vararg;

private import tango.io.model.IConduit;

private import tango.text.json.JsonEscape;

private import tango.text.json.JsonParser;

private import Float = tango.text.convert.Float;

/*******************************************************************************

        Parse json text into a set of inter-related structures. Typical 
        usage is as follows:
        ---
        auto json = new Json!(char);
        json.parse (`{"t": true, "n":null, "array":["world", [4, 5]]}`);    
        ---   

        Converting back to text format employs a delegate. This one emits 
        document content to the console:
        ---
        json.print ((char[] s) {Stdout(s);}); 
        ---

        Constructing json within your code leverages a handful of factories 
        within a document instance. This example creates a document from an 
        array of values:
        ---
        auto json = new Json!(char);

        // [true, false, null, "text"]
        with (json)
              value = array (true, false, null, "text");
        ---

        Setting the document to contain a simple object instead:
        ---
        // {"a" : 10}
        with (json)
              value = object (pair("a", value(10)));
        ---

        Objects may be constructed with multiple attribute pairs like so:
        ---
        // {"a" : 10, "b" : true}
        with (json)
              value = object (pair("a", value(10)), pair("b", value(true)));
        ---

        Substitute arrays, or other objects as values where appropriate:
        ---
        // {"a" : [10, true, {"b" : null}]}
        with (json)
              value = object (pair("a", array(10, true, object(pair("b")))));
        ---

        TODO: document how to extract content

        Big thanks to dhasenan for suggesting the construction notation. We
        can't make effective use of operator-overloading, due to the use of
        struct pointers, so this syntax turned out to be the next best thing.

*******************************************************************************/

class Json(T) : private JsonParser!(T)
{
                     /// use these types for external references
        public alias JsonValue*  Value;
        public alias NameValue*  Attribute;
        public alias JsonObject* Composite;

                    /// enumerates the seven acceptable JSON value types
        public enum Type {Null, String, RawString, Number, Object, Array, True, False};

        private Value root;

        /***********************************************************************
        
                Construct a json instance, with a default value of null

        ***********************************************************************/
        
        this ()
        {
                arrays.length = 16;
                parse (null);
        }

        /***********************************************************************
        
                Parse the given text and return a resultant Value type. Also
                sets the document value. 

        ***********************************************************************/
        
        final Value parse (T[] json)
        {
                nesting = 0;
                attrib.reset;
                values.reset;
                objects.reset;
                foreach (ref p; arrays)
                         p.index = 0;

                root = createValue;
                if (super.reset (json))
                    if (curType is Token.BeginObject)
                        root.set (parseObject);
                    else
                       if (curType is Token.BeginArray)
                           root.set (parseArray);
                       else
                          exception ("invalid json document");

                return root;
        }

        /***********************************************************************
        
                Return a text representation of this document

        ***********************************************************************/

        final T[] toString (T[] space=null, int decimals=2)
        {
                return root.print (space, decimals);
        }

        /***********************************************************************
        
                Returns the root value of this document

        ***********************************************************************/
        
        final Value value ()
        {
                return root;
        }

        /***********************************************************************
        
                Set the root value of this document

        ***********************************************************************/
        
        final Value value (Value v)
        {
                return root = v;
        }

        /***********************************************************************
        
                Create a text value

        ***********************************************************************/
        
        final Value value (T[] v)
        {
                return createValue.set (v);
        }

        /***********************************************************************
        
                Create a boolean value

        ***********************************************************************/
        
        final Value value (bool v)
        {
                return createValue.set (v);
        }

        /***********************************************************************
        
                Create a numeric value

        ***********************************************************************/
        
        final Value value (double v)
        {
                return createValue.set (v);
        }

         /***********************************************************************
         
                 Create a single Value from an array of Values

         ***********************************************************************/

         final Value value (Value[] vals)
         {
                 return createValue.set (vals);
         }

        /***********************************************************************
        
                Create an array of values

        ***********************************************************************/

        final Value array (...)
        {
                return createValue.set (this, _arguments, _argptr);
        }

        /***********************************************************************
        
                Create an attribute/value pair, where value defaults to 
                null

        ***********************************************************************/
        
        Attribute pair (T[] name, Value value = null)
        {
                if (value is null)
                    value = createValue;
                return createAttribute.set (name, value);
        }

        /***********************************************************************
        
                Create a composite from zero or more pairs, and return as 
                a value

        ***********************************************************************/
        
        final Value object (Attribute set[]...)
        {
                return createValue.set (createObject.add (set));
        }

        /***********************************************************************
        
                Internal factory to create values

        ***********************************************************************/
        
        private Value createValue ()
        {
                return values.allocate.reset;
        }

        /***********************************************************************
        
                Internal factory to create composites

        ***********************************************************************/
        
        private Composite createObject ()
        {
                return objects.allocate.reset;
        }

        /***********************************************************************
        
                Internal factory to create attributes

        ***********************************************************************/
       
        private Attribute createAttribute ()
        {
                return attrib.allocate;
        }

        /***********************************************************************
        
                Throw a generic exception

        ***********************************************************************/
        
        private void exception (char[] msg)
        {
                throw new Exception (msg);
        }

        /***********************************************************************
        
                Parse an instance of a value

        ***********************************************************************/
        
        private Value parseValue ()
        {
                auto v = values.allocate;

                switch (super.curType)
                       {
                       case Token.True:
                            v.set (Type.True);
                            break;

                       case Token.False:
                            v.set (Type.False);
                            break;

                       case Token.Null:
                            v.set (Type.Null);
                            break;

                       case Token.BeginObject:
                            v.set (parseObject);
                            break;

                       case Token.BeginArray:
                            v.set (parseArray);
                            break;

                       case Token.String:
                            v.set (super.value, true);
                            break;

                       case Token.Number:
                            v.set (Float.parse (super.value));
                            break;

                       default:
                            v.set (Type.Null);
                            break;
                       }

                return v;
        }

        /***********************************************************************
        
                Parse an object declaration

        ***********************************************************************/
        
        private Composite parseObject ()
        {
                auto o = objects.allocate.reset;

                while (super.next) 
                      {
                      if (super.curType is Token.EndObject)
                          return o;

                      if (super.curType != Token.Name)
                          super.expected ("an attribute-name", super.str.ptr);
                        
                      auto name = super.value;
                        
                      if (! super.next)
                            super.expected ("an attribute-value", super.str.ptr);
                        
                      o.append (attrib.allocate.set (name, parseValue));
                      }

                return o;
        }
        
        /***********************************************************************
        
                Parse an array declaration

        ***********************************************************************/
        
        private Value[] parseArray ()
        {
                if (nesting >= arrays.length)
                    exception ("array nesting too deep within document");

                auto array = &arrays[nesting++];
                auto start = array.index;

                while (super.next && super.curType != Token.EndArray) 
                      {
                      if (array.index >= array.content.length)
                          array.content.length = array.content.length + 300;

                      array.content [array.index++] = parseValue;
                      }

                if (super.curType != Token.EndArray)
                    exception ("malformed array");

                --nesting;
                return array.content [start .. array.index];
        }

        /***********************************************************************
        
                Represents an attribute/value pair. Aliased as Attribute

        ***********************************************************************/
        
        struct NameValue
        {
                private Attribute       next;
                public  T[]             name;
                public  Value           value;

                /***************************************************************
        
                        Set a name and a value for this attribute

                        Returns itself, for use with Composite.add()

                ***************************************************************/
        
                Attribute set (T[] key, Value val)
                {
                        name = key;
                        value = val;
                        return this;
                }
        }

        /***********************************************************************

                Represents a single json Object (a composite of named 
                attribute/value pairs).

                This is aliased as Composite

        ***********************************************************************/
        
        struct JsonObject
        {
                private Attribute head,
                                  tail;
                
                /***************************************************************
        
                ***************************************************************/
        
                Composite reset ()
                {
                        head = tail = null;
                        return this;
                }

                /***************************************************************
        
                        Append an attribute/value pair

                ***************************************************************/
        
                Composite append (Attribute a)
                {
                        if (tail)
                            tail.next = a, tail = a;
                        else
                           head = tail = a;
                        return this;
                }

                /***************************************************************
        
                        Add a set of attribute/value pairs

                ***************************************************************/
        
                Composite add (Attribute[] set...)
                {
                        foreach (attr; set)
                                 append (attr);
                        return this;
                }

                /***************************************************************
                        
                        Construct and return a hashmap of Object attributes.
                        This will be a fairly costly operation, so consider 
                        alternatives where appropriate

                ***************************************************************/
        
                Value[T[]] hashmap ()
                {
                        Value[T[]] members;

                        auto a = head;
                        while (a)
                              {
                              members[a.name] = a.value;
                              a = a.next;
                              }

                        return members;
                }
        
                /***************************************************************
        
                        Return a corresponding value for the given attribute 
                        name. Does a linear lookup across the attribute set

                ***************************************************************/
        
                Value value (T[] name)
                {
                        auto a = head;
                        while (a)
                               if (name == a.name)
                                   return a.value;
                               else
                                  a = a.next;

                        return null;
                }
        
                /***************************************************************
        
                        Iterate over our attribute names and values

                ***************************************************************/
        
                Iterator attributes ()
                {
                        Iterator i = {head};
                        return i;
                }

                /***************************************************************
        
                        Iterate over our attribute names. Note that we 
                        use a Fruct to handle this, since foreach does
                        not operate cleanly with pointers (it doesn't 
                        automatically dereference them), whereas using 
                        x.attributes() does. 
                        
                        We may also use this to do some name filtering

                ***************************************************************/
        
                static struct Iterator
                {
                        private Attribute head;
        
                        int opApply (int delegate(ref T[] key, ref Value val) dg)
                        {
                                int res;
        
                                auto a = head;
                                while (a)
                                      {
                                      if ((res = dg (a.name, a.value)) != 0) 
                                           break;
                                      a = a.next;
                                      }
                               return res;
                        }
                }
        }
        
        /***********************************************************************
        
                Represents a json value that is one of the seven types 
                specified via the Json.Type enum 

        ***********************************************************************/
        
        struct JsonValue
        {
                private union
                {
                        Value[]         array;
                        real            number;
                        T[]             string;
                        Composite       object;
                }
        
                public Type type;               /// the type of this node
                alias reset set;                /// alternate name for reset

                /***************************************************************
        
                        return true if this node is of the given type

                ***************************************************************/
        
                bool opEquals (Type t) 
                {
                        return type is t;
                }
                
                /***************************************************************
        
                        Return true if this value represent True

                ***************************************************************/
        
                bool toBool ()
                {
                        return (type is Type.True);
                }

                /***************************************************************
                        
                        Return the string content. Returns null if this
                        value is not a string.

                        Uses dst for escape conversion where possible.

                ***************************************************************/
        
                T[] toString (T[] dst = null)
                {
                        if (type is Type.RawString)
                            return string;

                        if (type is Type.String)
                            return unescape (string, dst);

                        return null;
                }
                
                /***************************************************************
        
                        Emit the string content to the given delegate, with
                        escape conversion as required.

                        Returns false if this is not a String value
                      
                ***************************************************************/
        
                bool toString (void delegate(T[]) dg)
                {
                        if (type is Type.RawString)
                            dg(string);
                        else
                           if (type is Type.String)
                               unescape (string, dg);
                           else
                              return false;
                        return true;
                }

                /***************************************************************
        
                        Return the content as a Composite/Object. Returns null
                        if this value is not a Composite.

                ***************************************************************/
        
                Composite toObject ()
                {
                        return type is Type.Object ? object : null;
                }
                
                /***************************************************************
        
                        Return the content as a double. Returns nan where
                        the value is not numeric.

                ***************************************************************/
        
                real toNumber ()
                {
                        return type is Type.Number ? number : real.nan;
                }
                
                /***************************************************************
        
                        Return the content as an array. Returns null where
                        the value is not an array.

                ***************************************************************/
        
                Value[] toArray ()
                {
                        return (type is Type.Array) ? array : null;
                }
                
                /***************************************************************
        
                        Set this value to represent a string. If 'escaped' 
                        is set, the string is assumed to have pre-converted
                        escaping of reserved characters (such as \t).

                ***************************************************************/
        
                Value set (T[] str, bool escaped = false)
                {
                        type = escaped ? Type.String : Type.RawString;
                        string = str;
                        return this;
                }
                
                /***************************************************************
        
                        Set this value to represent an object.

                ***************************************************************/
        
                Value set (Composite obj)
                {
                        type = Type.Object;
                        object = obj;
                        return this;
                }
                
                /***************************************************************
        
                        Set this value to represent a number.

                ***************************************************************/
        
                Value set (real num)
                {
                        type = Type.Number;
                        number = num;
                        return this;
                }
                
                /***************************************************************
        
                        Set this value to represent a boolean.

                ***************************************************************/
        
                Value set (bool b)
                {
                        type = b ? Type.True : Type.False;             
                        return this;
                }
                
                /***************************************************************
        
                        Set this value to represent an array of values.

                ***************************************************************/
        
                Value set (Value[] a)
                {
                        type = Type.Array;
                        array = a;
                        return this;
                }
                
                /***************************************************************
        
                        Set this value to represent null

                ***************************************************************/
        
                Value reset ()
                {
                        type = Type.Null;
                        return this;
                }
                
                /***************************************************************
        
                        Return a text representation of this value

                ***************************************************************/

                T[] print (T[] space=null, int decimals=2)
                {
                        T[] tmp;
                        void append (T[] s) {tmp ~= s;}
                        print (&append, space, decimals);
                        return tmp;
                }

                /***************************************************************
        
                        Emit a text representation of this value to the 
                        given OutputStream

                ***************************************************************/

                Value print (OutputStream s, T[] space=null, int decimals=2)
                {
                        return print ((T[] t){s.write(t);}, space, decimals);
                }

                /***************************************************************
                        
                        Emit a text representation of this value to the
                        provided delegate

                ***************************************************************/
                
                Value print (void delegate(T[]) append, T[] space=null, int decimals=2)
                {
                        auto indent = 0;
        
                        void newline ()
                        {
                                if (space.length)
                                   {
                                   append ("\n");
                                   for (auto i=0; i < indent; i++)
                                        append (space);
                                   }
                        }
        
                        void printValue (Value val)
                        {
                                void printObject (Composite obj)
                                {
                                        if (obj is null) 
                                            return;
                                        
                                        bool first = true;
                                        append ("{");
                                        indent++;
        
                                        foreach (k, v; obj.attributes)
                                                {
                                                if (!first)  
                                                     append (",");
                                                newline;
                                                append (`"`), append(k), append(`":`);
                                                printValue (v);
                                                first = false;
                                                }
                                        indent--;
                                        newline;
                                        append ("}");
                                }
                                
                                void printArray (Value[] arr)
                                {
                                        bool first = true;
                                        append ("[");
                                        indent++;
                                        foreach (v; arr)
                                                {
                                                if (!first) 
                                                     append (", ");
                                                newline;
                                                printValue (v);
                                                first = false;
                                                }
                                        indent--;
                                        newline;
                                        append ("]");
                                }
        
        
                                if (val is null) 
                                    return;
                                
                                switch (val.type)
                                       {
                                       T[64] tmp = void;
        
                                       case Type.String:
                                            append (`"`), append(val.string), append(`"`);
                                            break;
        
                                       case Type.RawString:
                                            append (`"`), escape(val.string, append), append(`"`);
                                            break;
        
                                       case Type.Number:
                                            append (Float.format (tmp, val.toNumber, decimals));
                                            break;
        
                                       case Type.Object:
                                            auto obj = val.toObject;
                                            debug assert(obj !is null);
                                            printObject (val.toObject);
                                            break;
        
                                       case Type.Array:
                                            printArray (val.toArray);
                                            break;
        
                                       case Type.True:
                                            append ("true");
                                            break;
        
                                       case Type.False:
                                            append ("false");
                                            break;
        
                                       default:
                                       case Type.Null:
                                            append ("null");
                                            break;
                                       }
                        }
                        
                        printValue (this);
                        return this;
                }

                /***************************************************************
        
                        Set to a specified type

                ***************************************************************/
        
                private Value set (Type type)
                {
                        this.type = type;
                        return this;
                }

                /***************************************************************
        
                        Set a variety of values into an array type

                ***************************************************************/
        
                private Value set (Json host, TypeInfo[] info, va_list args)
                {
                        Value[] list;

                        foreach (type; info)
                                {
                                Value v;
                                if (type is typeid(Value))
                                    v = va_arg!(Value)(args);
                                else
                                   {
                                   v = host.createValue;
                                   if (type is typeid(double))
                                       v.set (va_arg!(double)(args));
                                   else
                                   if (type is typeid(int))
                                       v.set (va_arg!(int)(args));
                                   else
                                   if (type is typeid(bool))
                                       v.set (va_arg!(bool)(args));
                                   else
                                   if (type is typeid(long))
                                       v.set (va_arg!(long)(args));
                                   else
                                   if (type is typeid(Composite))
                                       v.set (va_arg!(Composite)(args));
                                   else
                                   if (type is typeid(T[]))
                                       v.set (va_arg!(T[])(args));
                                   else
                                   if (type is typeid(void*))
                                       va_arg!(void*)(args);
                                   else
                                      host.exception ("JsonValue.set :: unexpected type: "~type.toString);
                                   }
                                list ~= v;
                                }
                        return set (list);
                }
        }

        /***********************************************************************
        
                Internal allocation mechanism

        ***********************************************************************/
        
        private struct Allocator(T)
        {
                private T[]     list;
                private T[][]   lists;
                private int     index,
                                block;

                void reset ()
                {
                        // discard since prior lists are not initialized
                        lists.length = 0;       
                        block = -1;
                        newlist;
                }

                T* allocate ()
                {
                        if (index >= list.length)
                            newlist;
        
                        auto p = &list [index++];
                        return p;
                }
        
                private void newlist ()
                {
                        index = 0;
                        if (++block >= lists.length)
                           {
                           lists.length = lists.length + 1;
                           lists[$-1] = new T[256];
                           }
                        list = lists [block];
                }
        }

        /***********************************************************************
            
                Internal use for parsing array values
                    
        ***********************************************************************/
        
        private struct Array
        {
                uint            index;
                Value[]         content;
        }

        /***********************************************************************
        
                Internal document representation

        ***********************************************************************/
        
        private alias Allocator!(NameValue)     Attrib;
        private alias Allocator!(JsonValue)     Values;
        private alias Allocator!(JsonObject)    Objects;

        private Attrib                          attrib;
        private Values                          values;
        private Array[]                         arrays;
        private Objects                         objects;
        private uint                            nesting;
}



/*******************************************************************************

*******************************************************************************/

debug (UnitTest)
{
        unittest
        {
        with (new Json!(char))
             {
             root = object
                  (
                  pair ("edgar", value("friendly")),
                  pair ("count", value(11.5)),
                  pair ("array", value(array(1, 2)))
                  );

             char[] value = toString();
             assert (value == `{"edgar":"friendly","count":11.5,"array":[1, 2]}`, value);
             }
        }
        
        unittest
        {
        // check with a separator of the tab character
        with (new Json!(char))
             {
             root = object
                  (
                  pair ("edgar", value("friendly")),
                  pair ("count", value(11.5)),
                  pair ("array", value(array(1, 2)))
                  );

             char[] value = toString ("\t");
             assert (value == "{\n\t\"edgar\":\"friendly\",\n\t\"count\":11.5,\n\t\"array\":[\n\t\t1, \n\t\t2\n\t]\n}", value);
             }
        }
        
        unittest
        {
        // check with a separator of five spaces
        with (new Json!(dchar))
             {
             root = object
                  ( 
                  pair ("edgar", value("friendly")),
                  pair ("count", value(11.5)),
                  pair ("array", value(array(1, 2)))
                  );

             dchar[] value = toString ("     ");
             assert (value == "{\n     \"edgar\":\"friendly\",\n     \"count\":11.5,\n     \"array\":[\n          1, \n          2\n     ]\n}");
             }
        }
}
        
/*******************************************************************************

*******************************************************************************/

debug (Json)
{
        import tango.io.Stdout;
        import tango.io.device.File;
        import tango.time.StopWatch;
                
        void main()
        {
                void loop (JsonParser!(char) parser, char[] json, int n)
                {
                        for (uint i = 0; i < n; ++i)
                            {
                            parser.reset (json);
                            while (parser.next) {}
                            }
                }
        
                void test (char[] filename, char[] txt)
                {
                        uint n = (300 * 1024 * 1024) / txt.length;
                        auto parser = new JsonParser!(char);
                        
                        StopWatch watch;
                        watch.start;
                        loop (parser, txt, n);
                        auto t = watch.stop;
                        auto mb = (txt.length * n) / (1024 * 1024);
                        Stdout.formatln("{} {} iterations, {} seconds: {} MB/s", filename, n, t, mb/t);
                }
        
                void test1 (char[] filename, char[] txt)
                {
                        uint n = (200 * 1024 * 1024) / txt.length;
                        auto parser = new Json!(char);
                        
                        StopWatch watch;
                        watch.start;
                        for (uint i = 0; i < n; ++i)
                             parser.parse (txt);
        
                        auto t = watch.stop;
                        auto mb = (txt.length * n) / (1024 * 1024);
                        Stdout.formatln("{} {} iterations, {} seconds: {} MB/s", filename, n, t, mb/t);
                }
        
                char[] load (char[] file)
                {
                        return cast(char[]) File.get(file);
                }
        
                //test("test1.json", load("test1.json"));
                //test("test2.json", load("test2.json"));
                //test("test3.json", load("test3.json"));
                        
                //test1("test1.json", load("test1.json"));
                //test1("test2.json", load("test2.json"));
                //test1("test3.json", load("test3.json"));
                
                auto p = new Json!(char);
                auto v = p.parse (`{"t": true, "f":false, "n":null, "hi":["world", "big", 123, [4, 5, ["foo"]]]}`);       
                Stdout.formatln ("{}", p.toString);
        
                with (p)
                      value = object(pair("a", array(null, true, false, 30, object(pair("foo")))), pair("b", value(10)));
        
                Stdout.formatln ("{}", p.toString);

                p.parse ("[-1]");
                Stdout.formatln ("{}", p.toString(null));

                p.parse ("[11.23477]");
                Stdout.formatln ("{}", p.toString(null, 4));

                p.parse(`["foo"]`);
                Stdout.formatln ("{}", p.toString);

                p.parse(`{"foo": {"ff" : "ffff"}`);
                Stdout.formatln ("{}", p.toString);

                with (new Json!(char))
                     {
                     root = object(pair("array", array(null)));
                     Stdout.formatln ("{}", toString());
                     }
        }
}