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
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Produce a JSONArray containing the values of the members of this
  * JSONObject.
  * @param names A JSONArray containing a list of key strings. This
  * determines the sequence of the values in the result.
  * @return A JSONArray of values.
  * @throws JSONException If any of the values are non-finite numbers.
  */
 public JSONArray ToJSONArray(JSONArray names)
 {
     if (names == null || names.Length() == 0)
     {
         return null;
     }
     var ja = new JSONArray();
     for (var i = 0; i < names.Length(); i += 1)
     {
         ja.Put(Opt(names.GetString(i)));
     }
     return ja;
 }
Beispiel #4
0
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------- REVISIONS ------------------------------
        // Date       Name                 Tracking #         Description
        // ---------  -------------------  -------------      ----------------------
        // 07JUN2009  James Shen                 	          Initial Creation
        ////////////////////////////////////////////////////////////////////////////
        /**
         * Produce a JSONArray containing the names of the elements of this
         * JSONObject.
         * @return A JSONArray containing the key strings, or null if the JSONObject
         * is empty.
         */
        public JSONArray Names()
        {
            var ja = new JSONArray();
            var keyset = Keys();
            foreach (var o in keyset)
            {
                ja.Put(o);
            }

            return ja.Length() == 0 ? null : ja;
        }
Beispiel #5
0
        internal static JSONObject Apply(JSONArray start, ArrayList tokens,
                 int firstToken)
        {
            if (start == null)
            {
                return null;
            }

            var nTokens = tokens.Count;
            for (var i = firstToken; i < nTokens; )
            {
                var tok1 = (string)tokens[i];
                var t1 = tok1[0];
                switch (t1)
                {
                    case SEPARATOR:
                        throw new JSONException("Syntax error: must start with an "
                                + "array: " + tok1);

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

                    default:
                        throw new JSONException("Syntax error: unknown" +
                                " delimiter: " + tok1);
                }
            }

            return null;
        }
Beispiel #6
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;
 }