Beispiel #1
0
 private Result(JSONObject obj)
 {
     if (obj == null)
     {
         throw new ArgumentException("json object cannot be null");
     }
     _isArray = false;
     _json = obj;
     _array = null;
 }
Beispiel #2
0
 private Result(JSONArray obj)
 {
     if (obj == null)
     {
         throw new ArgumentException("json object cannot be null");
     }
     _isArray = true;
     _json = null;
     _array = obj;
 }
Beispiel #3
0
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------- REVISIONS ------------------------------
        // Date       Name                 Tracking #         Description
        // ---------  -------------------  -------------      ----------------------
        // 07JUN2009  James Shen                 	          Initial Creation
        ////////////////////////////////////////////////////////////////////////////
        /**
         * Scan the content following the named tag, attaching it to the context.
         * @param x       The JSONXMLTokener containing the source str.
         * @param context The JSONObject that will include the new material.
         * @param name    The tag name.
         * @return true if the close tag is processed.
         * @
         */
        private static bool Parse(JSONXMLTokener x, JSONObject context,
                                     string name)
        {
            JSONObject o;
            string s;

            // Test for and skip past these forms:
            //      <!-- ... -->
            //      <!   ...   >
            //      <![  ... ]]>
            //      <?   ...  ?>
            // Report errors for these forms:
            //      <>
            //      <=
            //      <<

            var t = x.NextToken();

            // <!

            if (t == (object)BANG)
            {
                var c = x.Next();
                if (c == '-')
                {
                    if (x.Next() == '-')
                    {
                        x.SkipPast("-->");
                        return false;
                    }
                    x.Back();
                }
                else if (c == '[')
                {
                    t = x.NextToken();
                    if (t.Equals("CDATA"))
                    {
                        if (x.Next() == '[')
                        {
                            s = x.NextCDATA();
                            if (s.Length > 0)
                            {
                                context.Accumulate("content", s);
                            }
                            return false;
                        }
                    }
                    throw x.SyntaxError("Expected 'CDATA['");
                }
                var i = 1;
                do
                {
                    t = x.NextMeta();
                    if (t == null)
                    {
                        throw x.SyntaxError("Missing '>' after '<!'.");
                    }
                    if (t == (object)LT)
                    {
                        i += 1;
                    }
                    else if (t == (object)GT)
                    {
                        i -= 1;
                    }
                } while (i > 0);
                return false;
            }
            if (t == (object)QUEST)
            {

                // <?

                x.SkipPast("?>");
                return false;
            }
            if (t == (object)SLASH)
            {

                // Close tag </

                if (name == null || !x.NextToken().Equals(name))
                {
                    throw x.SyntaxError("Mismatched close tag");
                }
                if (x.NextToken() != (object)GT)
                {
                    throw x.SyntaxError("Misshaped close tag");
                }
                return true;

            }
            if (t is char)
            {
                throw x.SyntaxError("Misshaped tag");

                // Open tag <

            }
            string n = (string)t;
            t = null;
            o = new JSONObject();
            for (; ; )
            {
                if (t == null)
                {
                    t = x.NextToken();
                }

                // attribute = value

                if (t is string)
                {
                    s = (string)t;
                    t = x.NextToken();
                    if (t == (object)EQ)
                    {
                        t = x.NextToken();
                        if (!(t is string))
                        {
                            throw x.SyntaxError("Missing value");
                        }
                        o.Accumulate(s, t);
                        t = null;
                    }
                    else
                    {
                        o.Accumulate(s, "");
                    }

                    // Empty tag <.../>

                }
                else if (t == (object)SLASH)
                {
                    if (x.NextToken() != (object)GT)
                    {
                        throw x.SyntaxError("Misshaped tag");
                    }
                    context.Accumulate(n, o);
                    return false;

                    // Content, between <...> and </...>

                }
                else if (t == (object)GT)
                {
                    for (; ; )
                    {
                        t = x.NextContent();
                        if (t == null)
                        {
                            if (name != null)
                            {
                                throw x.SyntaxError("Unclosed tag " + name);
                            }
                            return false;
                        }
                        if (t is string)
                        {
                            s = (string)t;
                            if (s.Length > 0)
                            {
                                o.Accumulate("content", s);
                            }

                            // Nested element

                        }
                        else if (t == (object)LT)
                        {
                            if (Parse(x, o, n))
                            {
                                if (o.Length() == 0)
                                {
                                    context.Accumulate(n, "");
                                }
                                else if (o.Length() == 1 &&
                                         o.Opt("content") != null)
                                {
                                    context.Accumulate(n, o.Opt("content"));
                                }
                                else
                                {
                                    context.Accumulate(n, o);
                                }
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    throw x.SyntaxError("Misshaped tag");
                }
            }
        }
Beispiel #4
0
 ////////////////////////////////////////////////////////////////////////////
 //--------------------------------- REVISIONS ------------------------------
 // Date       Name                 Tracking #         Description
 // ---------  -------------------  -------------      ----------------------
 // 07JUN2009  James Shen                 	          Initial Creation
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Convert a well-formed (but not necessarily valid) JSONXML str into a
  * JSONObject. Some information may be lost in this transformation
  * because JSON is a data format and JSONXML is a document format. JSONXML uses
  * elements, attributes, and content text, while JSON uses unordered
  * collections of name/value pairs and arrays of values. JSON does not
  * does not like to distinguish between elements and attributes.
  * Sequences of similar elements are represented as JSONArrays. Content
  * text may be placed in a "content" member. Comments, prologs, DTDs, and
  * <code>&lt;[ [ ]]></code> are ignored.
  * @param str The source str.
  * @return A JSONObject containing the structured data from the JSONXML str.
  * @
  */
 public static JSONObject ToJSONObject(string str)
 {
     var o = new JSONObject();
     var x = new JSONXMLTokener(str);
     while (x.More())
     {
         x.SkipPast("<");
         Parse(x, o, null);
     }
     return o;
 }
Beispiel #5
0
        internal static JSONObject Apply(JSONObject start, ArrayList tokens,
                 int firstToken)
        {
            if (start == null)
            {
                return null;
            }

            var nTokens = tokens.Count;
            if (firstToken >= nTokens)
            {
                return start;
            }

            for (var i = firstToken; i < nTokens; i++)
            {
                var tok1 = (string)tokens[i];
                if (tok1.Length == 1 && JSONPathTokenizer.IsDelimiter(tok1[0]))
                {
                    throw new JSONException("Syntax error: path cannot start with a delimiter: " + tok1);
                }

                if (i + 1 >= nTokens)
                {
                    return start;
                }
                var tok2 = (string)tokens[i + 1];
                var t2 = tok2[0];
                switch (t2)
                {
                    case SEPARATOR:
                        return Apply(start.OptJSONObject(tok1), tokens, i + 2);

                    case ARRAY_START:
                        if (i + 2 >= nTokens)
                        {
                            throw new JSONException("Syntax error: array must be followed by a dimension: " + tok1);
                        }
                        var tok3 = (string)tokens[i + 2];
                        int dim ;
                        try
                        {
                            dim = int.Parse(tok3);
                        }
                        catch (FormatException)
                        {
                            throw new JSONException("Syntax error: illegal array dimension: " + tok3);
                        }
                        if (i + 3 >= nTokens)
                        {
                            throw new JSONException("Syntax error: array dimension must be closed: " + tok3);
                        }
                        var tok4 = (string)tokens[i + 3];
                        if (tok4.Length != 1 && tok4[0] != ARRAY_END)
                        {
                            throw new JSONException("Syntax error: illegal close of array dimension: " + tok4);
                        }
                        if (i + 4 >= nTokens)
                        {
                            throw new JSONException("Syntax error: array close must be followed by a separator: " + tok1);
                        }
                        var tok5 = (string)tokens[i + 4];
                        if (tok5.Length != 1 && tok5[0] != SEPARATOR)
                        {
                            throw new JSONException("Syntax error: illegal separator after array: " + tok4);
                        }
                        i += 4;
                        var array = start.OptJSONArray(tok1);
                        return array == null ? null : Apply((JSONObject)array.Opt(dim), tokens, i + 1);
                }
            }

            return start;
        }
Beispiel #6
0
 ////////////////////////////////////////////////////////////////////////////
 //--------------------------------- REVISIONS ------------------------------
 // Date       Name                 Tracking #         Description
 // ---------  -------------------  -------------      ----------------------
 // 07JUN2009  James Shen                 	          Initial Creation
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Construct a JSONObject from a subset of another JSONObject.
  * An array of strings is used to identify the keys that should be copied.
  * Missing keys are ignored.
  * @param jo A JSONObject.
  * @param sa An array of strings.
  * @exception JSONException If a value is a non-finite number.
  */
 public JSONObject(JSONObject jo, string[] sa)
     : this()
 {
     for (var i = 0; i < sa.Length; i += 1)
     {
         PutOpt(sa[i], jo.Opt(sa[i]));
     }
 }
Beispiel #7
0
 ////////////////////////////////////////////////////////////////////////////
 //--------------------------------- REVISIONS ------------------------------
 // Date       Name                 Tracking #         Description
 // ---------  -------------------  -------------      ----------------------
 // 07JUN2009  James Shen                 	          Initial Creation
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Produce a JSONObject by combining a JSONArray of names with the values
  * of this JSONArray.
  * @param names A JSONArray containing a list of key strings. These will be
  * paired with the values.
  * @return A JSONObject, or null if there are no names or if this JSONArray
  * has no values.
  * @ If any of the names are null.
  */
 public JSONObject ToJSONObject(JSONArray names)
 {
     if (names == null || names.Length() == 0 || Length() == 0)
     {
         return null;
     }
     var jo = new JSONObject();
     for (int i = 0; i < names.Length(); i += 1)
     {
         jo.Put(names.GetString(i), Opt(i));
     }
     return jo;
 }