X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Futils%2FLOGJSONObject.java;h=0f95523406bef9a7ec62a22d5754c2339510fef1;hb=381d4ebc5e83d5fd5b62fff7e5a6fa6d582149d9;hp=f7e087489b2811dcffddf0b5f3042474b8c59a2a;hpb=15b5a700e4d2ea3572d38ccf1a8120040e23a038;p=dmaap%2Fdatarouter.git
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java
index f7e08748..0f955234 100644
--- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java
+++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java
@@ -28,11 +28,14 @@ import com.att.eelf.configuration.EELFManager;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
-import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.util.*;
-
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONString;
@@ -54,22 +57,22 @@ import org.json.JSONTokener;
* returns a value if one can be found, and throws an exception if one cannot be
* found. An opt
method returns a default value instead of throwing
* an exception, and so is useful for obtaining optional values.
- *
- * The generic get()
and opt()
methods return an
+ *
+ *
The generic get()
and opt()
methods return an
* object, which you can cast or query for type. There are also typed
* get
and opt
methods that do type checking and type
* coercion for you. The opt methods differ from the get methods in that they do
* not throw. Instead, they return a specified value, such as null.
- *
- * The put
methods add or replace values in an object. For example,
+ *
+ *
The put
methods add or replace values in an object. For example,
*
*
* myString = new JSONObject().put("JSON", "Hello, World!").toString(); *- *
- * produces the string {"JSON": "Hello, World"}
.
- *
- * The texts produced by the toString
methods strictly conform to
+ *
+ *
* produces the string {"JSON": "Hello, World"}
.
+ *
+ *
The texts produced by the toString
methods strictly conform to
* the JSON syntax rules. The constructors are more forgiving in the texts they
* will accept:
*
- * The general contract of {@code hashCode} is: + * + *
* The general contract of {@code hashCode} is: *
- * As much as is reasonably practical, the hashCode method defined by + * + *
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
@@ -186,21 +193,18 @@ public class LOGJSONObject {
}
}
-
/**
* The map where the JSONObject's properties are kept.
*/
private final Map
- * The key is formed by removing the * The key is formed by removing the
- * For example, if an object has a method named * For example, if an object has a method named
- * If only one value is accumulated that is not a JSONArray, then the
+ *
+ * * If only one value is accumulated that is not a JSONArray, then the
* result will be the same as using put. But if multiple values are
* accumulated, then the result will be like append.
*
@@ -434,15 +352,15 @@ public class LOGJSONObject {
* or if the key is null.
*/
public LOGJSONObject accumulate(
- String key,
- Object value
- ) throws JSONException {
+ String key,
+ Object value
+ ) {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key, value instanceof JSONArray
- ? new JSONArray().put(value)
- : value);
+ ? new JSONArray().put(value)
+ : value);
} else if (object instanceof JSONArray) {
((JSONArray) object).put(value);
} else {
@@ -451,7 +369,6 @@ public class LOGJSONObject {
return this;
}
-
/**
* Append values to the array under a key. If the key does not exist in the
* JSONObject, then the key is put in the JSONObject with its value being a
@@ -464,7 +381,7 @@ public class LOGJSONObject {
* @throws JSONException If the key is null or if the current value
* associated with the key is not a JSONArray.
*/
- public LOGJSONObject append(String key, Object value) throws JSONException {
+ public LOGJSONObject append(String key, Object value) {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
@@ -472,30 +389,29 @@ public class LOGJSONObject {
} else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value));
} else {
- throw new JSONException("JSONObject[" + key +
- "] is not a JSONArray.");
+ throw new JSONException(JSON_OBJECT_CONST + key
+ + "] is not a JSONArray.");
}
return this;
}
-
/**
* Produce a string from a double. The string "null" will be returned if
* the number is not finite.
*
- * @param d A double.
+ * @param dub A double.
* @return A String.
*/
- public static String doubleToString(double d) {
- if (Double.isInfinite(d) || Double.isNaN(d)) {
+ public static String doubleToString(double dub) {
+ if (Double.isInfinite(dub) || Double.isNaN(dub)) {
return "null";
}
-// Shave off trailing zeros and decimal point, if possible.
+ // Shave off trailing zeros and decimal point, if possible.
- String string = Double.toString(d);
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
- string.indexOf('E') < 0) {
+ String string = Double.toString(dub);
+ if (string.indexOf('.') > 0 && string.indexOf('e') < 0
+ && string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
@@ -506,7 +422,6 @@ public class LOGJSONObject {
return string;
}
-
/**
* Get the value object associated with a key.
*
@@ -514,19 +429,18 @@ public class LOGJSONObject {
* @return The object associated with the key.
* @throws JSONException if the key is not found.
*/
- public Object get(String key) throws JSONException {
+ public Object get(String key) {
if (key == null) {
throw new JSONException("Null key.");
}
Object object = this.opt(key);
if (object == null) {
- throw new JSONException("JSONObject[" + quote(key) +
- "] not found.");
+ throw new JSONException(JSON_OBJECT_CONST + quote(key)
+ + "] not found.");
}
return object;
}
-
/**
* Get the boolean value associated with a key.
*
@@ -534,22 +448,21 @@ public class LOGJSONObject {
* @return The truth.
* @throws JSONException if the value is not a Boolean or the String "true" or "false".
*/
- public boolean getBoolean(String key) throws JSONException {
+ public boolean getBoolean(String key) {
Object object = this.get(key);
- if (object.equals(Boolean.FALSE) ||
- (object instanceof String &&
- ((String) object).equalsIgnoreCase("false"))) {
+ if (object.equals(Boolean.FALSE)
+ || (object instanceof String
+ && "false".equalsIgnoreCase((String) object))) {
return false;
- } else if (object.equals(Boolean.TRUE) ||
- (object instanceof String &&
- ((String) object).equalsIgnoreCase("true"))) {
+ } else if (object.equals(Boolean.TRUE)
+ || (object instanceof String
+ && "true".equalsIgnoreCase((String) object))) {
return true;
}
- throw new JSONException("JSONObject[" + quote(key) +
- "] is not a Boolean.");
+ throw new JSONException(JSON_OBJECT_CONST + quote(key)
+ + "] is not a Boolean.");
}
-
/**
* Get the double value associated with a key.
*
@@ -562,15 +475,14 @@ public class LOGJSONObject {
Object object = this.get(key);
try {
return object instanceof Number
- ? ((Number) object).doubleValue()
- : Double.parseDouble((String) object);
+ ? ((Number) object).doubleValue()
+ : Double.parseDouble((String) object);
} catch (Exception e) {
- intlogger.error("JSONObject[" + quote(key) + "] is not a number.", e);
- throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");
+ intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not a number.", e);
+ throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not a number.");
}
}
-
/**
* Get the int value associated with a key.
*
@@ -583,15 +495,14 @@ public class LOGJSONObject {
Object object = this.get(key);
try {
return object instanceof Number
- ? ((Number) object).intValue()
- : Integer.parseInt((String) object);
+ ? ((Number) object).intValue()
+ : Integer.parseInt((String) object);
} catch (Exception e) {
- intlogger.error("JSONObject[" + quote(key) + "] is not an int.", e);
- throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
+ intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not an int.", e);
+ throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not an int.");
}
}
-
/**
* Get the JSONArray value associated with a key.
*
@@ -600,16 +511,15 @@ public class LOGJSONObject {
* @throws JSONException if the key is not found or
* if the value is not a JSONArray.
*/
- public JSONArray getJSONArray(String key) throws JSONException {
+ public JSONArray getJSONArray(String key) {
Object object = this.get(key);
if (object instanceof JSONArray) {
return (JSONArray) object;
}
- throw new JSONException("JSONObject[" + quote(key) +
- "] is not a JSONArray.");
+ throw new JSONException(JSON_OBJECT_CONST + quote(key)
+ + "] is not a JSONArray.");
}
-
/**
* Get the JSONObject value associated with a key.
*
@@ -618,16 +528,15 @@ public class LOGJSONObject {
* @throws JSONException if the key is not found or
* if the value is not a JSONObject.
*/
- public LOGJSONObject getJSONObject(String key) throws JSONException {
+ public LOGJSONObject getJSONObject(String key) {
Object object = this.get(key);
if (object instanceof LOGJSONObject) {
return (LOGJSONObject) object;
}
- throw new JSONException("JSONObject[" + quote(key) +
- "] is not a JSONObject.");
+ throw new JSONException(JSON_OBJECT_CONST + quote(key)
+ + "] is not a JSONObject.");
}
-
/**
* Get the long value associated with a key.
*
@@ -636,19 +545,18 @@ public class LOGJSONObject {
* @throws JSONException if the key is not found or if the value cannot
* be converted to a long.
*/
- public long getLong(String key) throws JSONException {
+ public long getLong(String key) {
Object object = this.get(key);
try {
return object instanceof Number
- ? ((Number) object).longValue()
- : Long.parseLong((String) object);
+ ? ((Number) object).longValue()
+ : Long.parseLong((String) object);
} catch (Exception e) {
- intlogger.error("JSONObject[" + quote(key) + "] is not a long.", e);
- throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
+ intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not a long.", e);
+ throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not a long.");
}
}
-
/**
* Get an array of field names from a JSONObject.
*
@@ -657,42 +565,18 @@ public class LOGJSONObject {
public static String[] getNames(LOGJSONObject jo) {
int length = jo.length();
if (length == 0) {
- return null;
+ return new String[]{};
}
IteratorNULL
object than to use Java's null
value.
* JSONObject.NULL.equals(null)
returns true
.
* JSONObject.NULL.toString()
returns "null"
.
*/
- public static final Object NULL = new Null();
-
+ private static final Object NULL = new Null();
/**
* Construct an empty JSONObject.
@@ -209,7 +213,6 @@ public class LOGJSONObject {
this.map = new LinkedHashMap<>();
}
-
/**
* Construct a JSONObject from a subset of another JSONObject.
* An array of strings is used to identify the keys that should be copied.
@@ -229,87 +232,84 @@ public class LOGJSONObject {
}
}
-
/**
* Construct a JSONObject from a JSONTokener.
*
- * @param x A JSONTokener object containing the source string.
+ * @param tokener A JSONTokener object containing the source string.
* @throws JSONException If there is a syntax error in the source string
* or a duplicated key.
*/
- public LOGJSONObject(JSONTokener x) {
+ public LOGJSONObject(JSONTokener tokener) {
this();
- char c;
+ char chr;
String key;
- if (x.nextClean() != '{') {
- throw x.syntaxError("A JSONObject text must begin with '{'");
+ if (tokener.nextClean() != '{') {
+ throw tokener.syntaxError("A JSONObject text must begin with '{'");
}
for (; ; ) {
- c = x.nextClean();
- switch (c) {
+ chr = tokener.nextClean();
+ switch (chr) {
case 0:
- throw x.syntaxError("A JSONObject text must end with '}'");
+ throw tokener.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
- x.back();
- key = x.nextValue().toString();
+ tokener.back();
+ key = tokener.nextValue().toString();
}
-// The key is followed by ':'. We will also tolerate '=' or '=>'.
+ // The key is followed by ':'. We will also tolerate '=' or '=>'.
- c = x.nextClean();
- if (c == '=') {
- if (x.next() != '>') {
- x.back();
+ chr = tokener.nextClean();
+ if (chr == '=') {
+ if (tokener.next() != '>') {
+ tokener.back();
}
- } else if (c != ':') {
- throw x.syntaxError("Expected a ':' after a key");
+ } else if (chr != ':') {
+ throw tokener.syntaxError("Expected a ':' after a key");
}
- this.putOnce(key, x.nextValue());
+ this.putOnce(key, tokener.nextValue());
-// Pairs are separated by ','. We will also tolerate ';'.
+ // Pairs are separated by ','. We will also tolerate ';'.
- switch (x.nextClean()) {
+ switch (tokener.nextClean()) {
case ';':
case ',':
- if (x.nextClean() == '}') {
+ if (tokener.nextClean() == '}') {
return;
}
- x.back();
+ tokener.back();
break;
case '}':
return;
default:
- throw x.syntaxError("Expected a ',' or '}'");
+ throw tokener.syntaxError("Expected a ',' or '}'");
}
}
}
-
/**
* Construct a JSONObject from a Map.
*
* @param map A map object that can be used to initialize the contents of
* the JSONObject.
- * @throws JSONException
+ * @throws JSONException json exception
*/
public LOGJSONObject(Map"get"
or "is"
followed by an uppercase letter,
* the method is invoked, and a key and the value returned from the getter method
* are put into the new JSONObject.
- * "get"
or "is"
prefix.
+ *
+ * "get"
or "is"
prefix.
* If the second remaining character is not upper case, then the first
* character is converted to lower case.
- * "getName"
, and
+ *
+ * "getName"
, and
* if the result of calling object.getName()
is "Larry Fine"
,
* then the JSONObject will contain "name": "Larry Fine"
.
*
@@ -334,96 +334,14 @@ public class LOGJSONObject {
this.populateMap(bean);
}
-
- /**
- * Construct a JSONObject from an Object, using reflection to find the
- * public members. The resulting JSONObject's keys will be the strings
- * from the names array, and the values will be the field values associated
- * with those keys in the object. If a key is not found or not visible,
- * then it will not be copied into the new JSONObject.
- *
- * @param object An object that has fields that should be used to make a
- * JSONObject.
- * @param names An array of strings, the names of the fields to be obtained
- * from the object.
- */
- public LOGJSONObject(Object object, String names[]) {
- this();
- Class extends Object> c = object.getClass();
- for (int i = 0; i < names.length; i += 1) {
- String name = names[i];
- try {
- this.putOpt(name, c.getField(name).get(object));
- } catch (Exception ignore) {
- }
- }
- }
-
-
- /**
- * Construct a JSONObject from a source JSON text string.
- * This is the most commonly used JSONObject constructor.
- *
- * @param source A string beginning
- * with {
(left brace) and ending
- * with }
(right brace).
- * @throws JSONException If there is a syntax error in the source
- * string or a duplicated key.
- */
- public LOGJSONObject(String source) throws JSONException {
- this(new JSONTokener(source));
- }
-
-
- /**
- * Construct a JSONObject from a ResourceBundle.
- *
- * @param baseName The ResourceBundle base name.
- * @param locale The Locale to load the ResourceBundle for.
- * @throws JSONException If any JSONExceptions are detected.
- */
- public LOGJSONObject(String baseName, Locale locale) throws JSONException {
- this();
- ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
- Thread.currentThread().getContextClassLoader());
-
-// Iterate through the keys in the bundle.
-
- Enumeration> keys = bundle.getKeys();
- while (keys.hasMoreElements()) {
- Object key = keys.nextElement();
- if (key instanceof String) {
-
-// Go through the path, ensuring that there is a nested JSONObject for each
-// segment except the last. Add the value using the last segment's name into
-// the deepest nested JSONObject.
-
- String[] path = ((String) key).split("\\.");
- int last = path.length - 1;
- LOGJSONObject target = this;
- for (int i = 0; i < last; i += 1) {
- String segment = path[i];
- LOGJSONObject nextTarget = target.optJSONObject(segment);
- if (nextTarget == null) {
- nextTarget = new LOGJSONObject();
- target.put(segment, nextTarget);
- }
- target = nextTarget;
- }
- target.put(path[last], bundle.getString((String) key));
- }
- }
- }
-
-
/**
* Accumulate values under a key. It is similar to the put method except
* that if there is already an object stored under the key then a
* JSONArray is stored under the key to hold all of the accumulated values.
* If there is already a JSONArray, then the new value is appended to it.
* In contrast, the put method replaces the previous value.
- *