Beispiel #1
0
        /**
         * Encodes this object as a human readable JSON string for debugging, such
         * as:
         * <pre>
         * {
         *     "query": "Pizza",
         *     "locations": [
         *         94043,
         *         90210
         *     ]
         * }</pre>
         *
         * @param indentSpaces the number of spaces to indent for each level of
         *     nesting.
         */
        public string ToString(int indentSpaces)
        {
            JSONStringer stringer = new JSONStringer(indentSpaces);

            WriteTo(stringer);
            return(stringer.ToString());
        }
Beispiel #2
0
 public void WriteTo(JSONStringer stringer)
 {
     stringer.Obj();
     foreach (var entry in nameValuePairs)
     {
         stringer.Key(entry.Key).Value(entry.Value);
     }
     stringer.EndObject();
 }
Beispiel #3
0
 public void WriteTo(JSONStringer stringer)
 {
     stringer.Array();
     foreach (object value in values)
     {
         stringer.Value(value);
     }
     stringer.EndArray();
 }
Beispiel #4
0
 /**
  * Encodes this object as a compact JSON string, such as:
  * <pre>{"query":"Pizza","locations":[94043,90210]}</pre>
  */
 public override string ToString()
 {
     try
     {
         JSONStringer stringer = new JSONStringer();
         WriteTo(stringer);
         return(stringer.ToString());
     }
     catch (JSONException e)
     {
         return(null);
     }
 }
Beispiel #5
0
        /**
         * Returns a new string by alternating this array's values with {@code
         * separator}. This array's string values are quoted and have their special
         * characters escaped. For example, the array containing the strings '12"
         * pizza', 'taco' and 'soda' joined on '+' returns this:
         * <pre>"12\" pizza"+"taco"+"soda"</pre>
         */
        public string Join(string separator)
        {
            JSONStringer stringer = new JSONStringer();

            stringer.Open(JSONStringer.Scope.NULL, "");
            for (int i = 0, size = values.Count; i < size; ++i)
            {
                if (i > 0)
                {
                    stringer.sbOut.Append(separator);
                }
                stringer.Value(values[i]);
            }
            stringer.Close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
            return(stringer.sbOut.ToString());
        }
Beispiel #6
0
 /**
  * Encodes {@code data} as a JSON string. This applies quotes and any
  * necessary character escaping.
  *
  * @param data the string to encode. Null will be interpreted as an empty
  *     string.
  */
 public static string Quote(string data)
 {
     if (data == null)
     {
         return("\"\"");
     }
     try
     {
         JSONStringer stringer = new JSONStringer();
         stringer.Open(JSONStringer.Scope.NULL, "");
         stringer.Value(data);
         stringer.Close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
         return(stringer.ToString());
     }
     catch (JSONException e)
     {
         throw e;
     }
 }