+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START==================================================\r
- * * org.onap.dmaap\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.json;\r
-\r
-/*\r
-Copyright (c) 2002 JSON.org\r
-\r
-Permission is hereby granted, free of charge, to any person obtaining a copy\r
-of this software and associated documentation files (the "Software"), to deal\r
-in the Software without restriction, including without limitation the rights\r
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-copies of the Software, and to permit persons to whom the Software is\r
-furnished to do so, subject to the following conditions:\r
-\r
-The above copyright notice and this permission notice shall be included in all\r
-copies or substantial portions of the Software.\r
-\r
-The Software shall be used for Good, not Evil.\r
-\r
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
-SOFTWARE.\r
-*/\r
-\r
-import java.io.IOException;\r
-import java.io.StringWriter;\r
-import java.io.Writer;\r
-import java.lang.reflect.Field;\r
-import java.lang.reflect.Method;\r
-import java.lang.reflect.Modifier;\r
-import java.util.Collection;\r
-import java.util.Enumeration;\r
-import java.util.LinkedHashMap;\r
-import java.util.Iterator;\r
-import java.util.Locale;\r
-import java.util.Map;\r
-import java.util.ResourceBundle;\r
-import java.util.Set;\r
-\r
-/**\r
- * A JSONObject is an unordered collection of name/value pairs. Its external\r
- * form is a string wrapped in curly braces with colons between the names and\r
- * values, and commas between the values and names. The internal form is an\r
- * object having <code>get</code> and <code>opt</code> methods for accessing the\r
- * values by name, and <code>put</code> methods for adding or replacing values\r
- * by name. The values can be any of these types: <code>Boolean</code>,\r
- * <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,\r
- * <code>String</code>, or the <code>JSONObject.NULL</code> object. A JSONObject\r
- * constructor can be used to convert an external form JSON text into an\r
- * internal form whose values can be retrieved with the <code>get</code> and\r
- * <code>opt</code> methods, or to convert values into a JSON text using the\r
- * <code>put</code> and <code>toString</code> methods. A <code>get</code> method\r
- * returns a value if one can be found, and throws an exception if one cannot be\r
- * found. An <code>opt</code> method returns a default value instead of throwing\r
- * an exception, and so is useful for obtaining optional values.\r
- * <p>\r
- * The generic <code>get()</code> and <code>opt()</code> methods return an\r
- * object, which you can cast or query for type. There are also typed\r
- * <code>get</code> and <code>opt</code> methods that do type checking and type\r
- * coercion for you. The opt methods differ from the get methods in that they do\r
- * not throw. Instead, they return a specified value, such as null.\r
- * <p>\r
- * The <code>put</code> methods add or replace values in an object. For example,\r
- *\r
- * <pre>\r
- * myString = new JSONObject().put("JSON", "Hello, World!").toString();\r
- * </pre>\r
- *\r
- * produces the string <code>{"JSON": "Hello, World"}</code>.\r
- * <p>\r
- * The texts produced by the <code>toString</code> methods strictly conform to\r
- * the JSON syntax rules. The constructors are more forgiving in the texts they\r
- * will accept:\r
- * <ul>\r
- * <li>An extra <code>,</code> <small>(comma)</small> may appear just\r
- * before the closing brace.</li>\r
- * <li>Strings may be quoted with <code>'</code> <small>(single\r
- * quote)</small>.</li>\r
- * <li>Strings do not need to be quoted at all if they do not begin with a quote\r
- * or single quote, and if they do not contain leading or trailing spaces, and\r
- * if they do not contain any of these characters:\r
- * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and\r
- * if they are not the reserved words <code>true</code>, <code>false</code>, or\r
- * <code>null</code>.</li>\r
- * <li>Keys can be followed by <code>=</code> or <code>=></code> as well as by\r
- * <code>:</code>.</li>\r
- * <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as\r
- * well as by <code>,</code> <small>(comma)</small>.</li>\r
- * </ul>\r
- *\r
- * @author JSON.org\r
- * @version 2012-12-01\r
- */\r
-public class LOGJSONObject {\r
- /**\r
- * The maximum number of keys in the key pool.\r
- */\r
- private static final int keyPoolSize = 100;\r
-\r
- /**\r
- * Key pooling is like string interning, but without permanently tying up\r
- * memory. To help conserve memory, storage of duplicated key strings in\r
- * JSONObjects will be avoided by using a key pool to manage unique key\r
- * string objects. This is used by JSONObject.put(string, object).\r
- */\r
- private static Map<String,Object> keyPool = new LinkedHashMap<String,Object>(keyPoolSize);\r
-\r
- /**\r
- * JSONObject.NULL is equivalent to the value that JavaScript calls null,\r
- * whilst Java's null is equivalent to the value that JavaScript calls\r
- * undefined.\r
- */\r
- private static final class Null {\r
-\r
- /**\r
- * There is only intended to be a single instance of the NULL object,\r
- * so the clone method returns itself.\r
- * @return NULL.\r
- */\r
- protected final Object clone() {\r
- return this;\r
- }\r
-\r
- /**\r
- * A Null object is equal to the null value and to itself.\r
- * @param object An object to test for nullness.\r
- * @return true if the object parameter is the JSONObject.NULL object\r
- * or null.\r
- */\r
- public boolean equals(Object object) {\r
- return object == null || object == this;\r
- }\r
-\r
- /**\r
- * Get the "null" string value.\r
- * @return The string "null".\r
- */\r
- public String toString() {\r
- return "null";\r
- }\r
- }\r
-\r
-\r
- /**\r
- * The map where the JSONObject's properties are kept.\r
- */\r
- private final Map<String,Object> map;\r
-\r
-\r
- /**\r
- * It is sometimes more convenient and less ambiguous to have a\r
- * <code>NULL</code> object than to use Java's <code>null</code> value.\r
- * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.\r
- * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.\r
- */\r
- public static final Object NULL = new Null();\r
-\r
-\r
- /**\r
- * Construct an empty JSONObject.\r
- */\r
- public LOGJSONObject() {\r
- this.map = new LinkedHashMap<String,Object>();\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from a subset of another JSONObject.\r
- * An array of strings is used to identify the keys that should be copied.\r
- * Missing keys are ignored.\r
- * @param jo A JSONObject.\r
- * @param names An array of strings.\r
- * @throws JSONException\r
- * @exception JSONException If a value is a non-finite number or if a name is duplicated.\r
- */\r
- public LOGJSONObject(LOGJSONObject jo, String[] names) {\r
- this();\r
- for (int i = 0; i < names.length; i += 1) {\r
- try {\r
- this.putOnce(names[i], jo.opt(names[i]));\r
- } catch (Exception ignore) {\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from a JSONTokener.\r
- * @param x A JSONTokener object containing the source string.\r
- * @throws JSONException If there is a syntax error in the source string\r
- * or a duplicated key.\r
- */\r
- public LOGJSONObject(JSONTokener x) throws JSONException {\r
- this();\r
- char c;\r
- String key;\r
-\r
- if (x.nextClean() != '{') {\r
- throw x.syntaxError("A JSONObject text must begin with '{'");\r
- }\r
- for (;;) {\r
- c = x.nextClean();\r
- switch (c) {\r
- case 0:\r
- throw x.syntaxError("A JSONObject text must end with '}'");\r
- case '}':\r
- return;\r
- default:\r
- x.back();\r
- key = x.nextValue().toString();\r
- }\r
-\r
-// The key is followed by ':'. We will also tolerate '=' or '=>'.\r
-\r
- c = x.nextClean();\r
- if (c == '=') {\r
- if (x.next() != '>') {\r
- x.back();\r
- }\r
- } else if (c != ':') {\r
- throw x.syntaxError("Expected a ':' after a key");\r
- }\r
- this.putOnce(key, x.nextValue());\r
-\r
-// Pairs are separated by ','. We will also tolerate ';'.\r
-\r
- switch (x.nextClean()) {\r
- case ';':\r
- case ',':\r
- if (x.nextClean() == '}') {\r
- return;\r
- }\r
- x.back();\r
- break;\r
- case '}':\r
- return;\r
- default:\r
- throw x.syntaxError("Expected a ',' or '}'");\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from a Map.\r
- *\r
- * @param map A map object that can be used to initialize the contents of\r
- * the JSONObject.\r
- * @throws JSONException\r
- */\r
- public LOGJSONObject(Map<String,Object> map) {\r
- this.map = new LinkedHashMap<String,Object>();\r
- if (map != null) {\r
- Iterator<Map.Entry<String,Object>> i = map.entrySet().iterator();\r
- while (i.hasNext()) {\r
- Map.Entry<String,Object> e = i.next();\r
- Object value = e.getValue();\r
- if (value != null) {\r
- this.map.put(e.getKey(), wrap(value));\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from an Object using bean getters.\r
- * It reflects on all of the public methods of the object.\r
- * For each of the methods with no parameters and a name starting\r
- * with <code>"get"</code> or <code>"is"</code> followed by an uppercase letter,\r
- * the method is invoked, and a key and the value returned from the getter method\r
- * are put into the new JSONObject.\r
- *\r
- * The key is formed by removing the <code>"get"</code> or <code>"is"</code> prefix.\r
- * If the second remaining character is not upper case, then the first\r
- * character is converted to lower case.\r
- *\r
- * For example, if an object has a method named <code>"getName"</code>, and\r
- * if the result of calling <code>object.getName()</code> is <code>"Larry Fine"</code>,\r
- * then the JSONObject will contain <code>"name": "Larry Fine"</code>.\r
- *\r
- * @param bean An object that has getter methods that should be used\r
- * to make a JSONObject.\r
- */\r
- public LOGJSONObject(Object bean) {\r
- this();\r
- this.populateMap(bean);\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from an Object, using reflection to find the\r
- * public members. The resulting JSONObject's keys will be the strings\r
- * from the names array, and the values will be the field values associated\r
- * with those keys in the object. If a key is not found or not visible,\r
- * then it will not be copied into the new JSONObject.\r
- * @param object An object that has fields that should be used to make a\r
- * JSONObject.\r
- * @param names An array of strings, the names of the fields to be obtained\r
- * from the object.\r
- */\r
- public LOGJSONObject(Object object, String names[]) {\r
- this();\r
- Class<? extends Object> c = object.getClass();\r
- for (int i = 0; i < names.length; i += 1) {\r
- String name = names[i];\r
- try {\r
- this.putOpt(name, c.getField(name).get(object));\r
- } catch (Exception ignore) {\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from a source JSON text string.\r
- * This is the most commonly used JSONObject constructor.\r
- * @param source A string beginning\r
- * with <code>{</code> <small>(left brace)</small> and ending\r
- * with <code>}</code> <small>(right brace)</small>.\r
- * @exception JSONException If there is a syntax error in the source\r
- * string or a duplicated key.\r
- */\r
- public LOGJSONObject(String source) throws JSONException {\r
- this(new JSONTokener(source));\r
- }\r
-\r
-\r
- /**\r
- * Construct a JSONObject from a ResourceBundle.\r
- * @param baseName The ResourceBundle base name.\r
- * @param locale The Locale to load the ResourceBundle for.\r
- * @throws JSONException If any JSONExceptions are detected.\r
- */\r
- public LOGJSONObject(String baseName, Locale locale) throws JSONException {\r
- this();\r
- ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,\r
- Thread.currentThread().getContextClassLoader());\r
-\r
-// Iterate through the keys in the bundle.\r
-\r
- Enumeration<?> keys = bundle.getKeys();\r
- while (keys.hasMoreElements()) {\r
- Object key = keys.nextElement();\r
- if (key instanceof String) {\r
-\r
-// Go through the path, ensuring that there is a nested JSONObject for each\r
-// segment except the last. Add the value using the last segment's name into\r
-// the deepest nested JSONObject.\r
-\r
- String[] path = ((String)key).split("\\.");\r
- int last = path.length - 1;\r
- LOGJSONObject target = this;\r
- for (int i = 0; i < last; i += 1) {\r
- String segment = path[i];\r
- LOGJSONObject nextTarget = target.optJSONObject(segment);\r
- if (nextTarget == null) {\r
- nextTarget = new LOGJSONObject();\r
- target.put(segment, nextTarget);\r
- }\r
- target = nextTarget;\r
- }\r
- target.put(path[last], bundle.getString((String)key));\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Accumulate values under a key. It is similar to the put method except\r
- * that if there is already an object stored under the key then a\r
- * JSONArray is stored under the key to hold all of the accumulated values.\r
- * If there is already a JSONArray, then the new value is appended to it.\r
- * In contrast, the put method replaces the previous value.\r
- *\r
- * If only one value is accumulated that is not a JSONArray, then the\r
- * result will be the same as using put. But if multiple values are\r
- * accumulated, then the result will be like append.\r
- * @param key A key string.\r
- * @param value An object to be accumulated under the key.\r
- * @return this.\r
- * @throws JSONException If the value is an invalid number\r
- * or if the key is null.\r
- */\r
- public LOGJSONObject accumulate(\r
- String key,\r
- Object value\r
- ) throws JSONException {\r
- testValidity(value);\r
- Object object = this.opt(key);\r
- if (object == null) {\r
- this.put(key, value instanceof JSONArray\r
- ? new JSONArray().put(value)\r
- : value);\r
- } else if (object instanceof JSONArray) {\r
- ((JSONArray)object).put(value);\r
- } else {\r
- this.put(key, new JSONArray().put(object).put(value));\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Append values to the array under a key. If the key does not exist in the\r
- * JSONObject, then the key is put in the JSONObject with its value being a\r
- * JSONArray containing the value parameter. If the key was already\r
- * associated with a JSONArray, then the value parameter is appended to it.\r
- * @param key A key string.\r
- * @param value An object to be accumulated under the key.\r
- * @return this.\r
- * @throws JSONException If the key is null or if the current value\r
- * associated with the key is not a JSONArray.\r
- */\r
- public LOGJSONObject append(String key, Object value) throws JSONException {\r
- testValidity(value);\r
- Object object = this.opt(key);\r
- if (object == null) {\r
- this.put(key, new JSONArray().put(value));\r
- } else if (object instanceof JSONArray) {\r
- this.put(key, ((JSONArray)object).put(value));\r
- } else {\r
- throw new JSONException("JSONObject[" + key +\r
- "] is not a JSONArray.");\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Produce a string from a double. The string "null" will be returned if\r
- * the number is not finite.\r
- * @param d A double.\r
- * @return A String.\r
- */\r
- public static String doubleToString(double d) {\r
- if (Double.isInfinite(d) || Double.isNaN(d)) {\r
- return "null";\r
- }\r
-\r
-// Shave off trailing zeros and decimal point, if possible.\r
-\r
- String string = Double.toString(d);\r
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&\r
- string.indexOf('E') < 0) {\r
- while (string.endsWith("0")) {\r
- string = string.substring(0, string.length() - 1);\r
- }\r
- if (string.endsWith(".")) {\r
- string = string.substring(0, string.length() - 1);\r
- }\r
- }\r
- return string;\r
- }\r
-\r
-\r
- /**\r
- * Get the value object associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return The object associated with the key.\r
- * @throws JSONException if the key is not found.\r
- */\r
- public Object get(String key) throws JSONException {\r
- if (key == null) {\r
- throw new JSONException("Null key.");\r
- }\r
- Object object = this.opt(key);\r
- if (object == null) {\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] not found.");\r
- }\r
- return object;\r
- }\r
-\r
-\r
- /**\r
- * Get the boolean value associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return The truth.\r
- * @throws JSONException\r
- * if the value is not a Boolean or the String "true" or "false".\r
- */\r
- public boolean getBoolean(String key) throws JSONException {\r
- Object object = this.get(key);\r
- if (object.equals(Boolean.FALSE) ||\r
- (object instanceof String &&\r
- ((String)object).equalsIgnoreCase("false"))) {\r
- return false;\r
- } else if (object.equals(Boolean.TRUE) ||\r
- (object instanceof String &&\r
- ((String)object).equalsIgnoreCase("true"))) {\r
- return true;\r
- }\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not a Boolean.");\r
- }\r
-\r
-\r
- /**\r
- * Get the double value associated with a key.\r
- * @param key A key string.\r
- * @return The numeric value.\r
- * @throws JSONException if the key is not found or\r
- * if the value is not a Number object and cannot be converted to a number.\r
- */\r
- public double getDouble(String key) throws JSONException {\r
- Object object = this.get(key);\r
- try {\r
- return object instanceof Number\r
- ? ((Number)object).doubleValue()\r
- : Double.parseDouble((String)object);\r
- } catch (Exception e) {\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not a number.");\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get the int value associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return The integer value.\r
- * @throws JSONException if the key is not found or if the value cannot\r
- * be converted to an integer.\r
- */\r
- public int getInt(String key) throws JSONException {\r
- Object object = this.get(key);\r
- try {\r
- return object instanceof Number\r
- ? ((Number)object).intValue()\r
- : Integer.parseInt((String)object);\r
- } catch (Exception e) {\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not an int.");\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get the JSONArray value associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return A JSONArray which is the value.\r
- * @throws JSONException if the key is not found or\r
- * if the value is not a JSONArray.\r
- */\r
- public JSONArray getJSONArray(String key) throws JSONException {\r
- Object object = this.get(key);\r
- if (object instanceof JSONArray) {\r
- return (JSONArray)object;\r
- }\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not a JSONArray.");\r
- }\r
-\r
-\r
- /**\r
- * Get the JSONObject value associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return A JSONObject which is the value.\r
- * @throws JSONException if the key is not found or\r
- * if the value is not a JSONObject.\r
- */\r
- public LOGJSONObject getJSONObject(String key) throws JSONException {\r
- Object object = this.get(key);\r
- if (object instanceof LOGJSONObject) {\r
- return (LOGJSONObject)object;\r
- }\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not a JSONObject.");\r
- }\r
-\r
-\r
- /**\r
- * Get the long value associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return The long value.\r
- * @throws JSONException if the key is not found or if the value cannot\r
- * be converted to a long.\r
- */\r
- public long getLong(String key) throws JSONException {\r
- Object object = this.get(key);\r
- try {\r
- return object instanceof Number\r
- ? ((Number)object).longValue()\r
- : Long.parseLong((String)object);\r
- } catch (Exception e) {\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] is not a long.");\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get an array of field names from a JSONObject.\r
- *\r
- * @return An array of field names, or null if there are no names.\r
- */\r
- public static String[] getNames(LOGJSONObject jo) {\r
- int length = jo.length();\r
- if (length == 0) {\r
- return null;\r
- }\r
- Iterator<String> iterator = jo.keys();\r
- String[] names = new String[length];\r
- int i = 0;\r
- while (iterator.hasNext()) {\r
- names[i] = iterator.next();\r
- i += 1;\r
- }\r
- return names;\r
- }\r
-\r
-\r
- /**\r
- * Get an array of field names from an Object.\r
- *\r
- * @return An array of field names, or null if there are no names.\r
- */\r
- public static String[] getNames(Object object) {\r
- if (object == null) {\r
- return null;\r
- }\r
- Class<? extends Object> klass = object.getClass();\r
- Field[] fields = klass.getFields();\r
- int length = fields.length;\r
- if (length == 0) {\r
- return null;\r
- }\r
- String[] names = new String[length];\r
- for (int i = 0; i < length; i += 1) {\r
- names[i] = fields[i].getName();\r
- }\r
- return names;\r
- }\r
-\r
-\r
- /**\r
- * Get the string associated with a key.\r
- *\r
- * @param key A key string.\r
- * @return A string which is the value.\r
- * @throws JSONException if there is no string value for the key.\r
- */\r
- public String getString(String key) throws JSONException {\r
- Object object = this.get(key);\r
- if (object instanceof String) {\r
- return (String)object;\r
- }\r
- throw new JSONException("JSONObject[" + quote(key) +\r
- "] not a string.");\r
- }\r
-\r
-\r
- /**\r
- * Determine if the JSONObject contains a specific key.\r
- * @param key A key string.\r
- * @return true if the key exists in the JSONObject.\r
- */\r
- public boolean has(String key) {\r
- return this.map.containsKey(key);\r
- }\r
-\r
-\r
- /**\r
- * Increment a property of a JSONObject. If there is no such property,\r
- * create one with a value of 1. If there is such a property, and if\r
- * it is an Integer, Long, Double, or Float, then add one to it.\r
- * @param key A key string.\r
- * @return this.\r
- * @throws JSONException If there is already a property with this name\r
- * that is not an Integer, Long, Double, or Float.\r
- */\r
- public LOGJSONObject increment(String key) throws JSONException {\r
- Object value = this.opt(key);\r
- if (value == null) {\r
- this.put(key, 1);\r
- } else if (value instanceof Integer) {\r
- this.put(key, ((Integer)value).intValue() + 1);\r
- } else if (value instanceof Long) {\r
- this.put(key, ((Long)value).longValue() + 1);\r
- } else if (value instanceof Double) {\r
- this.put(key, ((Double)value).doubleValue() + 1);\r
- } else if (value instanceof Float) {\r
- this.put(key, ((Float)value).floatValue() + 1);\r
- } else {\r
- throw new JSONException("Unable to increment [" + quote(key) + "].");\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Determine if the value associated with the key is null or if there is\r
- * no value.\r
- * @param key A key string.\r
- * @return true if there is no value associated with the key or if\r
- * the value is the JSONObject.NULL object.\r
- */\r
- public boolean isNull(String key) {\r
- return LOGJSONObject.NULL.equals(this.opt(key));\r
- }\r
-\r
-\r
- /**\r
- * Get an enumeration of the keys of the JSONObject.\r
- *\r
- * @return An iterator of the keys.\r
- */\r
- public Iterator<String> keys() {\r
- return this.keySet().iterator();\r
- }\r
-\r
-\r
- /**\r
- * Get a set of keys of the JSONObject.\r
- *\r
- * @return A keySet.\r
- */\r
- public Set<String> keySet() {\r
- return this.map.keySet();\r
- }\r
-\r
-\r
- /**\r
- * Get the number of keys stored in the JSONObject.\r
- *\r
- * @return The number of keys in the JSONObject.\r
- */\r
- public int length() {\r
- return this.map.size();\r
- }\r
-\r
-\r
- /**\r
- * Produce a JSONArray containing the names of the elements of this\r
- * JSONObject.\r
- * @return A JSONArray containing the key strings, or null if the JSONObject\r
- * is empty.\r
- */\r
- public JSONArray names() {\r
- JSONArray ja = new JSONArray();\r
- Iterator<String> keys = this.keys();\r
- while (keys.hasNext()) {\r
- ja.put(keys.next());\r
- }\r
- return ja.length() == 0 ? null : ja;\r
- }\r
-\r
- /**\r
- * Produce a string from a Number.\r
- * @param number A Number\r
- * @return A String.\r
- * @throws JSONException If n is a non-finite number.\r
- */\r
- public static String numberToString(Number number)\r
- throws JSONException {\r
- if (number == null) {\r
- throw new JSONException("Null pointer");\r
- }\r
- testValidity(number);\r
-\r
-// Shave off trailing zeros and decimal point, if possible.\r
-\r
- String string = number.toString();\r
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&\r
- string.indexOf('E') < 0) {\r
- while (string.endsWith("0")) {\r
- string = string.substring(0, string.length() - 1);\r
- }\r
- if (string.endsWith(".")) {\r
- string = string.substring(0, string.length() - 1);\r
- }\r
- }\r
- return string;\r
- }\r
-\r
-\r
- /**\r
- * Get an optional value associated with a key.\r
- * @param key A key string.\r
- * @return An object which is the value, or null if there is no value.\r
- */\r
- public Object opt(String key) {\r
- return key == null ? null : this.map.get(key);\r
- }\r
-\r
-\r
- /**\r
- * Get an optional boolean associated with a key.\r
- * It returns false if there is no such key, or if the value is not\r
- * Boolean.TRUE or the String "true".\r
- *\r
- * @param key A key string.\r
- * @return The truth.\r
- */\r
- public boolean optBoolean(String key) {\r
- return this.optBoolean(key, false);\r
- }\r
-\r
-\r
- /**\r
- * Get an optional boolean associated with a key.\r
- * It returns the defaultValue if there is no such key, or if it is not\r
- * a Boolean or the String "true" or "false" (case insensitive).\r
- *\r
- * @param key A key string.\r
- * @param defaultValue The default.\r
- * @return The truth.\r
- */\r
- public boolean optBoolean(String key, boolean defaultValue) {\r
- try {\r
- return this.getBoolean(key);\r
- } catch (Exception e) {\r
- return defaultValue;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get an optional double associated with a key,\r
- * or NaN if there is no such key or if its value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A string which is the key.\r
- * @return An object which is the value.\r
- */\r
- public double optDouble(String key) {\r
- return this.optDouble(key, Double.NaN);\r
- }\r
-\r
-\r
- /**\r
- * Get an optional double associated with a key, or the\r
- * defaultValue if there is no such key or if its value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A key string.\r
- * @param defaultValue The default.\r
- * @return An object which is the value.\r
- */\r
- public double optDouble(String key, double defaultValue) {\r
- try {\r
- return this.getDouble(key);\r
- } catch (Exception e) {\r
- return defaultValue;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get an optional int value associated with a key,\r
- * or zero if there is no such key or if the value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A key string.\r
- * @return An object which is the value.\r
- */\r
- public int optInt(String key) {\r
- return this.optInt(key, 0);\r
- }\r
-\r
-\r
- /**\r
- * Get an optional int value associated with a key,\r
- * or the default if there is no such key or if the value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A key string.\r
- * @param defaultValue The default.\r
- * @return An object which is the value.\r
- */\r
- public int optInt(String key, int defaultValue) {\r
- try {\r
- return this.getInt(key);\r
- } catch (Exception e) {\r
- return defaultValue;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get an optional JSONArray associated with a key.\r
- * It returns null if there is no such key, or if its value is not a\r
- * JSONArray.\r
- *\r
- * @param key A key string.\r
- * @return A JSONArray which is the value.\r
- */\r
- public JSONArray optJSONArray(String key) {\r
- Object o = this.opt(key);\r
- return o instanceof JSONArray ? (JSONArray)o : null;\r
- }\r
-\r
-\r
- /**\r
- * Get an optional JSONObject associated with a key.\r
- * It returns null if there is no such key, or if its value is not a\r
- * JSONObject.\r
- *\r
- * @param key A key string.\r
- * @return A JSONObject which is the value.\r
- */\r
- public LOGJSONObject optJSONObject(String key) {\r
- Object object = this.opt(key);\r
- return object instanceof LOGJSONObject ? (LOGJSONObject)object : null;\r
- }\r
-\r
-\r
- /**\r
- * Get an optional long value associated with a key,\r
- * or zero if there is no such key or if the value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A key string.\r
- * @return An object which is the value.\r
- */\r
- public long optLong(String key) {\r
- return this.optLong(key, 0);\r
- }\r
-\r
-\r
- /**\r
- * Get an optional long value associated with a key,\r
- * or the default if there is no such key or if the value is not a number.\r
- * If the value is a string, an attempt will be made to evaluate it as\r
- * a number.\r
- *\r
- * @param key A key string.\r
- * @param defaultValue The default.\r
- * @return An object which is the value.\r
- */\r
- public long optLong(String key, long defaultValue) {\r
- try {\r
- return this.getLong(key);\r
- } catch (Exception e) {\r
- return defaultValue;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get an optional string associated with a key.\r
- * It returns an empty string if there is no such key. If the value is not\r
- * a string and is not null, then it is converted to a string.\r
- *\r
- * @param key A key string.\r
- * @return A string which is the value.\r
- */\r
- public String optString(String key) {\r
- return this.optString(key, "");\r
- }\r
-\r
-\r
- /**\r
- * Get an optional string associated with a key.\r
- * It returns the defaultValue if there is no such key.\r
- *\r
- * @param key A key string.\r
- * @param defaultValue The default.\r
- * @return A string which is the value.\r
- */\r
- public String optString(String key, String defaultValue) {\r
- Object object = this.opt(key);\r
- return NULL.equals(object) ? defaultValue : object.toString();\r
- }\r
-\r
-\r
- private void populateMap(Object bean) {\r
- Class<? extends Object> klass = bean.getClass();\r
-\r
-// If klass is a System class then set includeSuperClass to false.\r
-\r
- boolean includeSuperClass = klass.getClassLoader() != null;\r
-\r
- Method[] methods = includeSuperClass\r
- ? klass.getMethods()\r
- : klass.getDeclaredMethods();\r
- for (int i = 0; i < methods.length; i += 1) {\r
- try {\r
- Method method = methods[i];\r
- if (Modifier.isPublic(method.getModifiers())) {\r
- String name = method.getName();\r
- String key = "";\r
- if (name.startsWith("get")) {\r
- if ("getClass".equals(name) ||\r
- "getDeclaringClass".equals(name)) {\r
- key = "";\r
- } else {\r
- key = name.substring(3);\r
- }\r
- } else if (name.startsWith("is")) {\r
- key = name.substring(2);\r
- }\r
- if (key.length() > 0 &&\r
- Character.isUpperCase(key.charAt(0)) &&\r
- method.getParameterTypes().length == 0) {\r
- if (key.length() == 1) {\r
- key = key.toLowerCase();\r
- } else if (!Character.isUpperCase(key.charAt(1))) {\r
- key = key.substring(0, 1).toLowerCase() +\r
- key.substring(1);\r
- }\r
-\r
- Object result = method.invoke(bean, (Object[])null);\r
- if (result != null) {\r
- this.map.put(key, wrap(result));\r
- }\r
- }\r
- }\r
- } catch (Exception ignore) {\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Put a key/boolean pair in the JSONObject.\r
- *\r
- * @param key A key string.\r
- * @param value A boolean which is the value.\r
- * @return this.\r
- * @throws JSONException If the key is null.\r
- */\r
- public LOGJSONObject put(String key, boolean value) throws JSONException {\r
- this.put(key, value ? Boolean.TRUE : Boolean.FALSE);\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/value pair in the JSONObject, where the value will be a\r
- * JSONArray which is produced from a Collection.\r
- * @param key A key string.\r
- * @param value A Collection value.\r
- * @return this.\r
- * @throws JSONException\r
- */\r
- public LOGJSONObject put(String key, Collection<Object> value) throws JSONException {\r
- this.put(key, new JSONArray(value));\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/double pair in the JSONObject.\r
- *\r
- * @param key A key string.\r
- * @param value A double which is the value.\r
- * @return this.\r
- * @throws JSONException If the key is null or if the number is invalid.\r
- */\r
- public LOGJSONObject put(String key, double value) throws JSONException {\r
- this.put(key, new Double(value));\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/int pair in the JSONObject.\r
- *\r
- * @param key A key string.\r
- * @param value An int which is the value.\r
- * @return this.\r
- * @throws JSONException If the key is null.\r
- */\r
- public LOGJSONObject put(String key, int value) throws JSONException {\r
- this.put(key, new Integer(value));\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/long pair in the JSONObject.\r
- *\r
- * @param key A key string.\r
- * @param value A long which is the value.\r
- * @return this.\r
- * @throws JSONException If the key is null.\r
- */\r
- public LOGJSONObject put(String key, long value) throws JSONException {\r
- this.put(key, new Long(value));\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/value pair in the JSONObject, where the value will be a\r
- * JSONObject which is produced from a Map.\r
- * @param key A key string.\r
- * @param value A Map value.\r
- * @return this.\r
- * @throws JSONException\r
- */\r
- public LOGJSONObject put(String key, Map<String, Object> value) throws JSONException {\r
- this.put(key, new LOGJSONObject(value));\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/value pair in the JSONObject. If the value is null,\r
- * then the key will be removed from the JSONObject if it is present.\r
- * @param key A key string.\r
- * @param value An object which is the value. It should be of one of these\r
- * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,\r
- * or the JSONObject.NULL object.\r
- * @return this.\r
- * @throws JSONException If the value is non-finite number\r
- * or if the key is null.\r
- */\r
- public LOGJSONObject put(String key, Object value) throws JSONException {\r
- String pooled;\r
- if (key == null) {\r
- throw new JSONException("Null key.");\r
- }\r
- if (value != null) {\r
- testValidity(value);\r
- pooled = (String)keyPool.get(key);\r
- if (pooled == null) {\r
- if (keyPool.size() >= keyPoolSize) {\r
- keyPool = new LinkedHashMap<String, Object>(keyPoolSize);\r
- }\r
- keyPool.put(key, key);\r
- } else {\r
- key = pooled;\r
- }\r
- this.map.put(key, value);\r
- } else {\r
- this.remove(key);\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/value pair in the JSONObject, but only if the key and the\r
- * value are both non-null, and only if there is not already a member\r
- * with that name.\r
- * @param key\r
- * @param value\r
- * @return his.\r
- * @throws JSONException if the key is a duplicate\r
- */\r
- public LOGJSONObject putOnce(String key, Object value) throws JSONException {\r
- if (key != null && value != null) {\r
- if (this.opt(key) != null) {\r
- throw new JSONException("Duplicate key \"" + key + "\"");\r
- }\r
- this.put(key, value);\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Put a key/value pair in the JSONObject, but only if the\r
- * key and the value are both non-null.\r
- * @param key A key string.\r
- * @param value An object which is the value. It should be of one of these\r
- * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,\r
- * or the JSONObject.NULL object.\r
- * @return this.\r
- * @throws JSONException If the value is a non-finite number.\r
- */\r
- public LOGJSONObject putOpt(String key, Object value) throws JSONException {\r
- if (key != null && value != null) {\r
- this.put(key, value);\r
- }\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Produce a string in double quotes with backslash sequences in all the\r
- * right places. A backslash will be inserted within </, producing <\/,\r
- * allowing JSON text to be delivered in HTML. In JSON text, a string\r
- * cannot contain a control character or an unescaped quote or backslash.\r
- * @param string A String\r
- * @return A String correctly formatted for insertion in a JSON text.\r
- */\r
- public static String quote(String string) {\r
- StringWriter sw = new StringWriter();\r
- synchronized (sw.getBuffer()) {\r
- try {\r
- return quote(string, sw).toString();\r
- } catch (IOException ignored) {\r
- // will never happen - we are writing to a string writer\r
- return "";\r
- }\r
- }\r
- }\r
-\r
- public static Writer quote(String string, Writer w) throws IOException {\r
- if (string == null || string.length() == 0) {\r
- w.write("\"\"");\r
- return w;\r
- }\r
-\r
- char b;\r
- char c = 0;\r
- String hhhh;\r
- int i;\r
- int len = string.length();\r
-\r
- w.write('"');\r
- for (i = 0; i < len; i += 1) {\r
- b = c;\r
- c = string.charAt(i);\r
- switch (c) {\r
- case '\\':\r
- case '"':\r
- w.write('\\');\r
- w.write(c);\r
- break;\r
- case '/':\r
- if (b == '<') {\r
- w.write('\\');\r
- }\r
- w.write(c);\r
- break;\r
- case '\b':\r
- w.write("\\b");\r
- break;\r
- case '\t':\r
- w.write("\\t");\r
- break;\r
- case '\n':\r
- w.write("\\n");\r
- break;\r
- case '\f':\r
- w.write("\\f");\r
- break;\r
- case '\r':\r
- w.write("\\r");\r
- break;\r
- default:\r
- if (c < ' ' || (c >= '\u0080' && c < '\u00a0')\r
- || (c >= '\u2000' && c < '\u2100')) {\r
- w.write("\\u");\r
- hhhh = Integer.toHexString(c);\r
- w.write("0000", 0, 4 - hhhh.length());\r
- w.write(hhhh);\r
- } else {\r
- w.write(c);\r
- }\r
- }\r
- }\r
- w.write('"');\r
- return w;\r
- }\r
-\r
- /**\r
- * Remove a name and its value, if present.\r
- * @param key The name to be removed.\r
- * @return The value that was associated with the name,\r
- * or null if there was no value.\r
- */\r
- public Object remove(String key) {\r
- return this.map.remove(key);\r
- }\r
-\r
- /**\r
- * Try to convert a string into a number, boolean, or null. If the string\r
- * can't be converted, return the string.\r
- * @param string A String.\r
- * @return A simple JSON value.\r
- */\r
- public static Object stringToValue(String string) {\r
- Double d;\r
- if (string.equals("")) {\r
- return string;\r
- }\r
- if (string.equalsIgnoreCase("true")) {\r
- return Boolean.TRUE;\r
- }\r
- if (string.equalsIgnoreCase("false")) {\r
- return Boolean.FALSE;\r
- }\r
- if (string.equalsIgnoreCase("null")) {\r
- return LOGJSONObject.NULL;\r
- }\r
-\r
- /*\r
- * If it might be a number, try converting it.\r
- * If a number cannot be produced, then the value will just\r
- * be a string. Note that the plus and implied string\r
- * conventions are non-standard. A JSON parser may accept\r
- * non-JSON forms as long as it accepts all correct JSON forms.\r
- */\r
-\r
- char b = string.charAt(0);\r
- if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {\r
- try {\r
- if (string.indexOf('.') > -1 ||\r
- string.indexOf('e') > -1 || string.indexOf('E') > -1) {\r
- d = Double.valueOf(string);\r
- if (!d.isInfinite() && !d.isNaN()) {\r
- return d;\r
- }\r
- } else {\r
- Long myLong = new Long(string);\r
- if (myLong.longValue() == myLong.intValue()) {\r
- return new Integer(myLong.intValue());\r
- } else {\r
- return myLong;\r
- }\r
- }\r
- } catch (Exception ignore) {\r
- }\r
- }\r
- return string;\r
- }\r
-\r
-\r
- /**\r
- * Throw an exception if the object is a NaN or infinite number.\r
- * @param o The object to test.\r
- * @throws JSONException If o is a non-finite number.\r
- */\r
- public static void testValidity(Object o) throws JSONException {\r
- if (o != null) {\r
- if (o instanceof Double) {\r
- if (((Double)o).isInfinite() || ((Double)o).isNaN()) {\r
- throw new JSONException(\r
- "JSON does not allow non-finite numbers.");\r
- }\r
- } else if (o instanceof Float) {\r
- if (((Float)o).isInfinite() || ((Float)o).isNaN()) {\r
- throw new JSONException(\r
- "JSON does not allow non-finite numbers.");\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Produce a JSONArray containing the values of the members of this\r
- * JSONObject.\r
- * @param names A JSONArray containing a list of key strings. This\r
- * determines the sequence of the values in the result.\r
- * @return A JSONArray of values.\r
- * @throws JSONException If any of the values are non-finite numbers.\r
- */\r
- public JSONArray toJSONArray(JSONArray names) throws JSONException {\r
- if (names == null || names.length() == 0) {\r
- return null;\r
- }\r
- JSONArray ja = new JSONArray();\r
- for (int i = 0; i < names.length(); i += 1) {\r
- ja.put(this.opt(names.getString(i)));\r
- }\r
- return ja;\r
- }\r
-\r
- /**\r
- * Make a JSON text of this JSONObject. For compactness, no whitespace\r
- * is added. If this would not result in a syntactically correct JSON text,\r
- * then null will be returned instead.\r
- * <p>\r
- * Warning: This method assumes that the data structure is acyclical.\r
- *\r
- * @return a printable, displayable, portable, transmittable\r
- * representation of the object, beginning\r
- * with <code>{</code> <small>(left brace)</small> and ending\r
- * with <code>}</code> <small>(right brace)</small>.\r
- */\r
- public String toString() {\r
- try {\r
- return this.toString(0);\r
- } catch (Exception e) {\r
- return null;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Make a prettyprinted JSON text of this JSONObject.\r
- * <p>\r
- * Warning: This method assumes that the data structure is acyclical.\r
- * @param indentFactor The number of spaces to add to each level of\r
- * indentation.\r
- * @return a printable, displayable, portable, transmittable\r
- * representation of the object, beginning\r
- * with <code>{</code> <small>(left brace)</small> and ending\r
- * with <code>}</code> <small>(right brace)</small>.\r
- * @throws JSONException If the object contains an invalid number.\r
- */\r
- public String toString(int indentFactor) throws JSONException {\r
- StringWriter w = new StringWriter();\r
- synchronized (w.getBuffer()) {\r
- return this.write(w, indentFactor, 0).toString();\r
- }\r
- }\r
-\r
- /**\r
- * Make a JSON text of an Object value. If the object has an\r
- * value.toJSONString() method, then that method will be used to produce\r
- * the JSON text. The method is required to produce a strictly\r
- * conforming text. If the object does not contain a toJSONString\r
- * method (which is the most common case), then a text will be\r
- * produced by other means. If the value is an array or Collection,\r
- * then a JSONArray will be made from it and its toJSONString method\r
- * will be called. If the value is a MAP, then a JSONObject will be made\r
- * from it and its toJSONString method will be called. Otherwise, the\r
- * value's toString method will be called, and the result will be quoted.\r
- *\r
- * <p>\r
- * Warning: This method assumes that the data structure is acyclical.\r
- * @param value The value to be serialized.\r
- * @return a printable, displayable, transmittable\r
- * representation of the object, beginning\r
- * with <code>{</code> <small>(left brace)</small> and ending\r
- * with <code>}</code> <small>(right brace)</small>.\r
- * @throws JSONException If the value is or contains an invalid number.\r
- */\r
- @SuppressWarnings("unchecked")\r
- public static String valueToString(Object value) throws JSONException {\r
- if (value == null || value.equals(null)) {\r
- return "null";\r
- }\r
- if (value instanceof JSONString) {\r
- Object object;\r
- try {\r
- object = ((JSONString)value).toJSONString();\r
- } catch (Exception e) {\r
- throw new JSONException(e);\r
- }\r
- if (object instanceof String) {\r
- return (String)object;\r
- }\r
- throw new JSONException("Bad value from toJSONString: " + object);\r
- }\r
- if (value instanceof Number) {\r
- return numberToString((Number) value);\r
- }\r
- if (value instanceof Boolean || value instanceof LOGJSONObject ||\r
- value instanceof JSONArray) {\r
- return value.toString();\r
- }\r
- if (value instanceof Map) {\r
- return new LOGJSONObject((Map<String, Object>)value).toString();\r
- }\r
- if (value instanceof Collection) {\r
- return new JSONArray((Collection<Object>)value).toString();\r
- }\r
- if (value.getClass().isArray()) {\r
- return new JSONArray(value).toString();\r
- }\r
- return quote(value.toString());\r
- }\r
-\r
- /**\r
- * Wrap an object, if necessary. If the object is null, return the NULL\r
- * object. If it is an array or collection, wrap it in a JSONArray. If\r
- * it is a map, wrap it in a JSONObject. If it is a standard property\r
- * (Double, String, et al) then it is already wrapped. Otherwise, if it\r
- * comes from one of the java packages, turn it into a string. And if\r
- * it doesn't, try to wrap it in a JSONObject. If the wrapping fails,\r
- * then null is returned.\r
- *\r
- * @param object The object to wrap\r
- * @return The wrapped value\r
- */\r
- @SuppressWarnings("unchecked")\r
- public static Object wrap(Object object) {\r
- try {\r
- if (object == null) {\r
- return NULL;\r
- }\r
- if (object instanceof LOGJSONObject || object instanceof JSONArray ||\r
- NULL.equals(object) || object instanceof JSONString ||\r
- object instanceof Byte || object instanceof Character ||\r
- object instanceof Short || object instanceof Integer ||\r
- object instanceof Long || object instanceof Boolean ||\r
- object instanceof Float || object instanceof Double ||\r
- object instanceof String) {\r
- return object;\r
- }\r
-\r
- if (object instanceof Collection) {\r
- return new JSONArray((Collection<Object>)object);\r
- }\r
- if (object.getClass().isArray()) {\r
- return new JSONArray(object);\r
- }\r
- if (object instanceof Map) {\r
- return new LOGJSONObject((Map<String, Object>)object);\r
- }\r
- Package objectPackage = object.getClass().getPackage();\r
- String objectPackageName = objectPackage != null\r
- ? objectPackage.getName()\r
- : "";\r
- if (\r
- objectPackageName.startsWith("java.") ||\r
- objectPackageName.startsWith("javax.") ||\r
- object.getClass().getClassLoader() == null\r
- ) {\r
- return object.toString();\r
- }\r
- return new LOGJSONObject(object);\r
- } catch(Exception exception) {\r
- return null;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Write the contents of the JSONObject as JSON text to a writer.\r
- * For compactness, no whitespace is added.\r
- * <p>\r
- * Warning: This method assumes that the data structure is acyclical.\r
- *\r
- * @return The writer.\r
- * @throws JSONException\r
- */\r
- public Writer write(Writer writer) throws JSONException {\r
- return this.write(writer, 0, 0);\r
- }\r
-\r
-\r
- @SuppressWarnings("unchecked")\r
- static final Writer writeValue(Writer writer, Object value,\r
- int indentFactor, int indent) throws JSONException, IOException {\r
- if (value == null || value.equals(null)) {\r
- writer.write("null");\r
- } else if (value instanceof LOGJSONObject) {\r
- ((LOGJSONObject) value).write(writer, indentFactor, indent);\r
- } else if (value instanceof JSONArray) {\r
- ((JSONArray) value).write(writer, indentFactor, indent);\r
- } else if (value instanceof Map) {\r
- new LOGJSONObject((Map<String, Object>) value).write(writer, indentFactor, indent);\r
- } else if (value instanceof Collection) {\r
- new JSONArray((Collection<Object>) value).write(writer, indentFactor,\r
- indent);\r
- } else if (value.getClass().isArray()) {\r
- new JSONArray(value).write(writer, indentFactor, indent);\r
- } else if (value instanceof Number) {\r
- writer.write(numberToString((Number) value));\r
- } else if (value instanceof Boolean) {\r
- writer.write(value.toString());\r
- } else if (value instanceof JSONString) {\r
- Object o;\r
- try {\r
- o = ((JSONString) value).toJSONString();\r
- } catch (Exception e) {\r
- throw new JSONException(e);\r
- }\r
- writer.write(o != null ? o.toString() : quote(value.toString()));\r
- } else {\r
- quote(value.toString(), writer);\r
- }\r
- return writer;\r
- }\r
-\r
- static final void indent(Writer writer, int indent) throws IOException {\r
- for (int i = 0; i < indent; i += 1) {\r
- writer.write(' ');\r
- }\r
- }\r
-\r
- /**\r
- * Write the contents of the JSONObject as JSON text to a writer. For\r
- * compactness, no whitespace is added.\r
- * <p>\r
- * Warning: This method assumes that the data structure is acyclical.\r
- *\r
- * @return The writer.\r
- * @throws JSONException\r
- */\r
- Writer write(Writer writer, int indentFactor, int indent)\r
- throws JSONException {\r
- try {\r
- boolean commanate = false;\r
- final int length = this.length();\r
- Iterator<String> keys = this.keys();\r
- writer.write('{');\r
-\r
- if (length == 1) {\r
- Object key = keys.next();\r
- writer.write(quote(key.toString()));\r
- writer.write(':');\r
- if (indentFactor > 0) {\r
- writer.write(' ');\r
- }\r
- writeValue(writer, this.map.get(key), indentFactor, indent);\r
- } else if (length != 0) {\r
- final int newindent = indent + indentFactor;\r
- while (keys.hasNext()) {\r
- Object key = keys.next();\r
- if (commanate) {\r
- writer.write(',');\r
- }\r
- if (indentFactor > 0) {\r
- writer.write('\n');\r
- }\r
- indent(writer, newindent);\r
- writer.write(quote(key.toString()));\r
- writer.write(':');\r
- if (indentFactor > 0) {\r
- writer.write(' ');\r
- }\r
- writeValue(writer, this.map.get(key), indentFactor,\r
- newindent);\r
- commanate = true;\r
- }\r
- if (indentFactor > 0) {\r
- writer.write('\n');\r
- }\r
- indent(writer, indent);\r
- }\r
- writer.write('}');\r
- return writer;\r
- } catch (IOException exception) {\r
- throw new JSONException(exception);\r
- }\r
- }\r
-}\r