id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
fae33cf4-6bf7-4ef2-acba-c2922cae8c2b | public JSONArray put(int index, double value) throws JSONException {
this.put(index, new Double(value));
return this;
} |
9e199f99-d630-4d0d-aaea-774713f64699 | public JSONArray put(int index, int value) throws JSONException {
this.put(index, new Integer(value));
return this;
} |
22df095e-c2d1-487a-9c88-be86f6de1ca7 | public JSONArray put(int index, long value) throws JSONException {
this.put(index, new Long(value));
return this;
} |
0731a835-90bb-4a75-b581-82850e561cc7 | public JSONArray put(int index, Map value) throws JSONException {
this.put(index, new JSONObject(value));
return this;
} |
814c75bb-045c-49b4-9ded-6179a0c81804 | public JSONArray put(int index, Object value) throws JSONException {
JSONObject.testValidity(value);
if (index < 0) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
if (index < this.length()) {
this.myArrayList.set(index, value);
} else {
while (index != this.length()) {
this.put(JSONObject.NULL);
}
this.put(value);
}
return this;
} |
b4469f06-6bf5-467f-814f-7a7e5af3aac4 | public Object remove(int index) {
Object o = this.opt(index);
this.myArrayList.remove(index);
return o;
} |
5ec3e982-ad82-4145-b008-b0128ca2a7ee | public JSONObject toJSONObject(JSONArray names) throws JSONException {
if (names == null || names.length() == 0 || this.length() == 0) {
return null;
}
JSONObject jo = new JSONObject();
for (int i = 0; i < names.length(); i += 1) {
jo.put(names.getString(i), this.opt(i));
}
return jo;
} |
5d87b038-c9c4-41b6-8cbb-3df4038d52cd | @Override
public String toString() {
try {
return this.toString(0);
} catch (JSONException e) {
return null;
}
} |
be283f70-c1b9-41dd-a825-2912e345d40c | public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
} |
d61c45b2-14d6-4469-89f3-1f0b412c3ea5 | public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
} |
954c78df-66fe-4c06-85c5-63aa74867691 | Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
int length = this.length();
writer.write('[');
if (length == 1) {
JSONObject.writeValue(writer, this.myArrayList.get(0),
indentFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
for (int i = 0; i < length; i += 1) {
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, newindent);
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, indent);
}
writer.write(']');
return writer;
} catch (IOException e) {
throw new JSONException(e);
}
} |
5c8c935e-e665-46c0-81df-f7e14a1a08bf | public String toJSONString(); |
c61cee3d-e0a3-4940-af27-6fa52e35bebc | public JSONException(String message) {
super(message);
} |
0ae6cab4-8cb3-4283-b98d-63142ff92bc8 | public JSONException(Throwable cause) {
super(cause.getMessage());
this.cause = cause;
} |
a63e5128-042d-4664-872e-19eed52cfdfd | @Override
public Throwable getCause() {
return this.cause;
} |
a71a9ed8-4a93-440e-817e-ae8c114280fc | protected final Object clone() {
return this;
} |
735e5e1f-8a86-4cb1-bfcb-805819a1cb90 | public boolean equals(Object object) {
return object == null || object == this;
} |
083bce1b-4810-4216-ba12-86c483eeb663 | public String toString() {
return "null";
} |
41bb3482-384e-40ed-bb35-0336035767be | public JSONObject() {
this.map = new HashMap();
} |
6a5b3ee7-2e13-43e6-9c75-01714d882aaf | public JSONObject(JSONObject jo, String[] names) {
this();
for (int i = 0; i < names.length; i += 1) {
try {
this.putOnce(names[i], jo.opt(names[i]));
} catch (Exception ignore) {
}
}
} |
9e703702-877a-4df3-a52c-a4fb0c5207ce | public JSONObject(JSONTokener x) throws JSONException {
this();
char c;
String key;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
}
for (;;) {
c = x.nextClean();
switch (c) {
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
x.back();
key = x.nextValue().toString();
}
// The key is followed by ':'.
c = x.nextClean();
if (c != ':') {
throw x.syntaxError("Expected a ':' after a key");
}
this.putOnce(key, x.nextValue());
// Pairs are separated by ','.
switch (x.nextClean()) {
case ';':
case ',':
if (x.nextClean() == '}') {
return;
}
x.back();
break;
case '}':
return;
default:
throw x.syntaxError("Expected a ',' or '}'");
}
}
} |
40b4caca-d0bc-4818-a4e5-e98f80f26d46 | public JSONObject(Map map) {
this.map = new HashMap();
if (map != null) {
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry) i.next();
Object value = e.getValue();
if (value != null) {
this.map.put(e.getKey(), wrap(value));
}
}
}
} |
e0cb3d2d-480a-4091-a2df-50c5439fe318 | public JSONObject(Object bean) {
this();
this.populateMap(bean);
} |
06e880fc-9cb8-43c2-9191-0a5b16945383 | public JSONObject(Object object, String names[]) {
this();
Class 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) {
}
}
} |
6270d167-181a-4fde-b436-06f0e7c3bc29 | public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
} |
71ccc9c6-7149-4f34-8906-33c25e5196e6 | public JSONObject(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;
JSONObject target = this;
for (int i = 0; i < last; i += 1) {
String segment = path[i];
JSONObject nextTarget = target.optJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject();
target.put(segment, nextTarget);
}
target = nextTarget;
}
target.put(path[last], bundle.getString((String) key));
}
}
} |
1566eb70-045b-49a7-ba01-3abbe70332f0 | public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key,
value instanceof JSONArray ? new JSONArray().put(value)
: value);
} else if (object instanceof JSONArray) {
((JSONArray) object).put(value);
} else {
this.put(key, new JSONArray().put(object).put(value));
}
return this;
} |
09120ecb-05b8-44f4-a1d0-fc6254487f18 | public JSONObject append(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key, new JSONArray().put(value));
} else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value));
} else {
throw new JSONException("JSONObject[" + key
+ "] is not a JSONArray.");
}
return this;
} |
9215e6fe-79c4-45f7-beae-c3e77fafab7a | public static String doubleToString(double d) {
if (Double.isInfinite(d) || Double.isNaN(d)) {
return "null";
}
// 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) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
}
return string;
} |
a37c0cf7-5467-42ea-b16a-c2c96f288267 | public Object get(String key) throws JSONException {
if (key == null) {
throw new JSONException("Null key.");
}
Object object = this.opt(key);
if (object == null) {
throw new JSONException("JSONObject[" + quote(key) + "] not found.");
}
return object;
} |
e5bfffdd-5604-4869-b29b-ccbeb03cc45e | public boolean getBoolean(String key) throws JSONException {
Object object = this.get(key);
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a Boolean.");
} |
568618c0-d892-4b78-90d8-a5dd487b48ab | public double getDouble(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object);
} catch (NumberFormatException e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.");
}
} |
14211203-7cd4-4af4-a0c7-2311f0755c0f | public int getInt(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (NumberFormatException e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int.");
}
} |
1dab7a33-7e42-442f-9481-db0ebc1bb104 | public JSONArray getJSONArray(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof JSONArray) {
return (JSONArray) object;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONArray.");
} |
9d9620ce-8989-4a1f-b756-f21bd18a2e02 | public JSONObject getJSONObject(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof JSONObject) {
return (JSONObject) object;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONObject.");
} |
c88dcb9e-7e91-4a8a-8ca8-9730c2333960 | public long getLong(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (NumberFormatException e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long.");
}
} |
5b9de1b6-3a2c-4ab4-b33f-9f12937c62a8 | public static String[] getNames(JSONObject jo) {
int length = jo.length();
if (length == 0) {
return null;
}
Iterator iterator = jo.keys();
String[] names = new String[length];
int i = 0;
while (iterator.hasNext()) {
names[i] = (String) iterator.next();
i += 1;
}
return names;
} |
a58f302c-03da-47b8-8746-7553e7179249 | public static String[] getNames(Object object) {
if (object == null) {
return null;
}
Class klass = object.getClass();
Field[] fields = klass.getFields();
int length = fields.length;
if (length == 0) {
return null;
}
String[] names = new String[length];
for (int i = 0; i < length; i += 1) {
names[i] = fields[i].getName();
}
return names;
} |
fac9e043-f8f1-45d6-a3ed-abbd8df2eb4e | public String getString(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof String) {
return (String) object;
}
throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
} |
3b1bf09b-1f1d-41f5-a184-b3429a8e2c00 | public boolean has(String key) {
return this.map.containsKey(key);
} |
6d637764-40eb-4343-85f4-6718896ce225 | public JSONObject increment(String key) throws JSONException {
Object value = this.opt(key);
if (value == null) {
this.put(key, 1);
} else if (value instanceof Integer) {
this.put(key, ((Integer) value).intValue() + 1);
} else if (value instanceof Long) {
this.put(key, ((Long) value).longValue() + 1);
} else if (value instanceof Double) {
this.put(key, ((Double) value).doubleValue() + 1);
} else if (value instanceof Float) {
this.put(key, ((Float) value).floatValue() + 1);
} else {
throw new JSONException("Unable to increment [" + quote(key) + "].");
}
return this;
} |
6c45fe1d-c0c4-4a90-b83b-e6de9fbf2da7 | public boolean isNull(String key) {
return JSONObject.NULL.equals(this.opt(key));
} |
b7e0504a-f7df-4f4d-878e-1ef9d02ff3ed | public Iterator keys() {
return this.keySet().iterator();
} |
974d4c3f-dabb-418c-bdb4-967d5e1189cd | public Set keySet() {
return this.map.keySet();
} |
dc4e21ae-3a25-4a17-91b1-2ee3a84322a2 | public int length() {
return this.map.size();
} |
bd3dbfbf-59d6-4983-9683-eb39778306df | public JSONArray names() {
JSONArray ja = new JSONArray();
Iterator keys = this.keys();
while (keys.hasNext()) {
ja.put(keys.next());
}
return ja.length() == 0 ? null : ja;
} |
a76b4b02-b598-4359-9d62-6e57da49b3ad | public static String numberToString(Number number) throws JSONException {
if (number == null) {
throw new JSONException("Null pointer");
}
testValidity(number);
// Shave off trailing zeros and decimal point, if possible.
String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
}
return string;
} |
b1c4d996-907c-485b-a54a-ad41f089a861 | public Object opt(String key) {
return key == null ? null : this.map.get(key);
} |
c066f093-ad67-4c3b-b43d-33c6cc6b7764 | public boolean optBoolean(String key) {
return this.optBoolean(key, false);
} |
6ea2b996-76af-489c-84f6-695c9168de59 | public boolean optBoolean(String key, boolean defaultValue) {
try {
return this.getBoolean(key);
} catch (JSONException e) {
return defaultValue;
}
} |
6a379067-7a06-4cb5-b3ac-8004de1a38c5 | public double optDouble(String key) {
return this.optDouble(key, Double.NaN);
} |
c04dd520-1ff3-41e3-b086-06274c7b5074 | public double optDouble(String key, double defaultValue) {
try {
return this.getDouble(key);
} catch (JSONException e) {
return defaultValue;
}
} |
b476e37d-d144-44ff-82c7-af6933a7dbb3 | public int optInt(String key) {
return this.optInt(key, 0);
} |
766a804a-7132-4d00-80f9-63f2deacb971 | public int optInt(String key, int defaultValue) {
try {
return this.getInt(key);
} catch (JSONException e) {
return defaultValue;
}
} |
af91c980-1427-4239-8497-ed4bc90b09eb | public JSONArray optJSONArray(String key) {
Object o = this.opt(key);
return o instanceof JSONArray ? (JSONArray) o : null;
} |
1902d617-92b3-4484-b666-e75f86591989 | public JSONObject optJSONObject(String key) {
Object object = this.opt(key);
return object instanceof JSONObject ? (JSONObject) object : null;
} |
17482782-b6d2-4d45-ba95-9ac9b51b2377 | public long optLong(String key) {
return this.optLong(key, 0);
} |
5ce5a260-c7b0-4cbc-8905-959c0fc15362 | public long optLong(String key, long defaultValue) {
try {
return this.getLong(key);
} catch (JSONException e) {
return defaultValue;
}
} |
5392b556-b695-460f-b29f-1a16d27f1c6c | public String optString(String key) {
return this.optString(key, "");
} |
37f30cfd-5e37-4ba8-83e3-fff0488c1598 | public String optString(String key, String defaultValue) {
Object object = this.opt(key);
return NULL.equals(object) ? defaultValue : object.toString();
} |
800b873b-318e-4452-9a2c-3d9f7947dc5c | private void populateMap(Object bean) {
Class klass = bean.getClass();
// If klass is a System class then set includeSuperClass to false.
boolean includeSuperClass = klass.getClassLoader() != null;
Method[] methods = includeSuperClass ? klass.getMethods() : klass
.getDeclaredMethods();
for (int i = 0; i < methods.length; i += 1) {
try {
Method method = methods[i];
if (Modifier.isPublic(method.getModifiers())) {
String name = method.getName();
String key = "";
if (name.startsWith("get")) {
if ("getClass".equals(name)
|| "getDeclaringClass".equals(name)) {
key = "";
} else {
key = name.substring(3);
}
} else if (name.startsWith("is")) {
key = name.substring(2);
}
if (key.length() > 0
&& Character.isUpperCase(key.charAt(0))
&& method.getParameterTypes().length == 0) {
if (key.length() == 1) {
key = key.toLowerCase();
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase()
+ key.substring(1);
}
Object result = method.invoke(bean, (Object[]) null);
if (result != null) {
this.map.put(key, wrap(result));
}
}
}
} catch (Exception ignore) {
}
}
} |
3b46ec8e-c31f-4183-9ac5-148ca694f48a | public JSONObject put(String key, boolean value) throws JSONException {
this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
return this;
} |
1d700ff8-935a-4f6f-9782-39f95610d5db | public JSONObject put(String key, Collection value) throws JSONException {
this.put(key, new JSONArray(value));
return this;
} |
88a7d754-daf3-4f39-97db-73211359d7cf | public JSONObject put(String key, double value) throws JSONException {
this.put(key, new Double(value));
return this;
} |
19f97679-d38a-4d13-8cf3-bc1707d9db85 | public JSONObject put(String key, int value) throws JSONException {
this.put(key, new Integer(value));
return this;
} |
e648a882-9e56-45cc-b897-5041310f07fa | public JSONObject put(String key, long value) throws JSONException {
this.put(key, new Long(value));
return this;
} |
9e68acec-1853-45ba-9dbd-8aa45529f978 | public JSONObject put(String key, Map value) throws JSONException {
this.put(key, new JSONObject(value));
return this;
} |
c06eb089-06aa-43ab-b108-d16a7d2f6c76 | public JSONObject put(String key, Object value) throws JSONException {
String pooled;
if (key == null) {
throw new NullPointerException("Null key.");
}
if (value != null) {
testValidity(value);
pooled = (String) keyPool.get(key);
if (pooled == null) {
if (keyPool.size() >= keyPoolSize) {
keyPool = new HashMap(keyPoolSize);
}
keyPool.put(key, key);
} else {
key = pooled;
}
this.map.put(key, value);
} else {
this.remove(key);
}
return this;
} |
6f0aa6e6-4114-4a19-b718-66a211af3967 | public JSONObject putOnce(String key, Object value) throws JSONException {
if (key != null && value != null) {
if (this.opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\"");
}
this.put(key, value);
}
return this;
} |
cf35a895-75fa-4f73-9385-ae3fd96e9e05 | public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) {
this.put(key, value);
}
return this;
} |
622a922c-159a-4405-8f64-3f60f51ef6ef | public static String quote(String string) {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
try {
return quote(string, sw).toString();
} catch (IOException ignored) {
// will never happen - we are writing to a string writer
return "";
}
}
} |
d9a5df0b-403b-4625-9c87-5e5f241703e6 | public static Writer quote(String string, Writer w) throws IOException {
if (string == null || string.length() == 0) {
w.write("\"\"");
return w;
}
char b;
char c = 0;
String hhhh;
int i;
int len = string.length();
w.write('"');
for (i = 0; i < len; i += 1) {
b = c;
c = string.charAt(i);
switch (c) {
case '\\':
case '"':
w.write('\\');
w.write(c);
break;
case '/':
if (b == '<') {
w.write('\\');
}
w.write(c);
break;
case '\b':
w.write("\\b");
break;
case '\t':
w.write("\\t");
break;
case '\n':
w.write("\\n");
break;
case '\f':
w.write("\\f");
break;
case '\r':
w.write("\\r");
break;
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|| (c >= '\u2000' && c < '\u2100')) {
w.write("\\u");
hhhh = Integer.toHexString(c);
w.write("0000", 0, 4 - hhhh.length());
w.write(hhhh);
} else {
w.write(c);
}
}
}
w.write('"');
return w;
} |
b51c61ee-af57-4426-911f-84b9d728104d | public Object remove(String key) {
return this.map.remove(key);
} |
e6120de0-5030-42c7-ab1a-9e3221c2465c | public static Object stringToValue(String string) {
Double d;
if (string.equals("")) {
return string;
}
if (string.equalsIgnoreCase("true")) {
return Boolean.TRUE;
}
if (string.equalsIgnoreCase("false")) {
return Boolean.FALSE;
}
if (string.equalsIgnoreCase("null")) {
return JSONObject.NULL;
}
/*
* If it might be a number, try converting it. If a number cannot be
* produced, then the value will just be a string.
*/
char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '-') {
try {
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1) {
d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = new Long(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return new Integer(myLong.intValue());
} else {
return myLong;
}
}
}
} catch (NumberFormatException ignore) {
}
}
return string;
} |
9cd39fff-5709-4bfc-9d7e-6a9fa2afee6f | public static void testValidity(Object o) throws JSONException {
if (o != null) {
if (o instanceof Double) {
if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
throw new JSONException(
"JSON does not allow non-finite numbers.");
}
} else if (o instanceof Float) {
if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
throw new JSONException(
"JSON does not allow non-finite numbers.");
}
}
}
} |
2cd9278f-dad8-4eaa-85fe-41579a6c2071 | public JSONArray toJSONArray(JSONArray names) throws JSONException {
if (names == null || names.length() == 0) {
return null;
}
JSONArray ja = new JSONArray();
for (int i = 0; i < names.length(); i += 1) {
ja.put(this.opt(names.getString(i)));
}
return ja;
} |
5754d09a-bbf3-4b5e-990d-888815b2c111 | public String toString() {
try {
return this.toString(0);
} catch (JSONException e) {
return null;
}
} |
c147b3b8-5e27-412d-9051-ed1466b577f7 | public String toString(int indentFactor) throws JSONException {
StringWriter w = new StringWriter();
synchronized (w.getBuffer()) {
return this.write(w, indentFactor, 0).toString();
}
} |
92f3527e-6db0-4eb2-bca8-9227d8b3df4d | public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
if (value instanceof JSONString) {
Object object;
try {
object = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if (object instanceof String) {
return (String) object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (value instanceof Number) {
return numberToString((Number) value);
}
if (value instanceof Boolean || value instanceof JSONObject
|| value instanceof JSONArray) {
return value.toString();
}
if (value instanceof Map) {
return new JSONObject((Map) value).toString();
}
if (value instanceof Collection) {
return new JSONArray((Collection) value).toString();
}
if (value.getClass().isArray()) {
return new JSONArray(value).toString();
}
return quote(value.toString());
} |
f33348ad-e196-4fbd-b05d-1ba3ab321aec | public static Object wrap(Object object) {
try {
if (object == null) {
return NULL;
}
if (object instanceof JSONObject || object instanceof JSONArray
|| NULL.equals(object) || object instanceof JSONString
|| object instanceof Byte || object instanceof Character
|| object instanceof Short || object instanceof Integer
|| object instanceof Long || object instanceof Boolean
|| object instanceof Float || object instanceof Double
|| object instanceof String) {
return object;
}
if (object instanceof Collection) {
return new JSONArray((Collection) object);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
return new JSONObject((Map) object);
}
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
.getName() : "";
if (objectPackageName.startsWith("java.")
|| objectPackageName.startsWith("javax.")
|| object.getClass().getClassLoader() == null) {
return object.toString();
}
return new JSONObject(object);
} catch (JSONException exception) {
return null;
}
} |
5c5b9748-18c1-419c-823c-fcbd1c81471c | public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
} |
af636495-6352-4965-baab-c8c88e2d001c | static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONObject) {
((JSONObject) value).write(writer, indentFactor, indent);
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
new JSONObject((Map) value).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
new JSONArray((Collection) value).write(writer, indentFactor,
indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
} else if (value instanceof Number) {
writer.write(numberToString((Number) value));
} else if (value instanceof Boolean) {
writer.write(value.toString());
} else if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
} else {
quote(value.toString(), writer);
}
return writer;
} |
90ef5834-3b1c-4c13-b2c3-7cdd3bd5cf71 | static final void indent(Writer writer, int indent) throws IOException {
for (int i = 0; i < indent; i += 1) {
writer.write(' ');
}
} |
eb908368-9d4e-435b-ac5c-2ea253c04501 | Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
final int length = this.length();
Iterator keys = this.keys();
writer.write('{');
if (length == 1) {
Object key = keys.next();
writer.write(quote(key.toString()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
while (keys.hasNext()) {
Object key = keys.next();
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
indent(writer, newindent);
writer.write(quote(key.toString()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor,
newindent);
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
indent(writer, indent);
}
writer.write('}');
return writer;
} catch (IOException exception) {
throw new JSONException(exception);
}
} |
e229a650-7c04-4b60-8d4a-33635a5fff1f | public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
} |
0a0395c9-792f-42f6-a928-5f8e77ef14dc | public JSONTokener(InputStream inputStream) throws JSONException {
this(new InputStreamReader(inputStream));
} |
003912ee-0250-403d-a48b-53f92b6fa027 | public JSONTokener(String s) {
this(new StringReader(s));
} |
04e6cc1c-d03b-4266-9e70-db0c578c6db3 | public void back() throws JSONException {
if (this.usePrevious || this.index <= 0) {
throw new JSONException("Stepping back two steps is not supported");
}
this.index -= 1;
this.character -= 1;
this.usePrevious = true;
this.eof = false;
} |
f0d49c91-be55-44c0-9825-b45d4b7badcf | public static int dehexchar(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'A' && c <= 'F') {
return c - ('A' - 10);
}
if (c >= 'a' && c <= 'f') {
return c - ('a' - 10);
}
return -1;
} |
1477c855-ea3c-44fd-9e49-73d72cb77fd7 | public boolean end() {
return this.eof && !this.usePrevious;
} |
10348d2c-1123-47d9-aa92-23c837fd8403 | public boolean more() throws JSONException {
this.next();
if (this.end()) {
return false;
}
this.back();
return true;
} |
23390ec5-aae3-410e-8226-b27892d90546 | public char next() throws JSONException {
int c;
if (this.usePrevious) {
this.usePrevious = false;
c = this.previous;
} else {
try {
c = this.reader.read();
} catch (IOException exception) {
throw new JSONException(exception);
}
if (c <= 0) { // End of stream
this.eof = true;
c = 0;
}
}
this.index += 1;
if (this.previous == '\r') {
this.line += 1;
this.character = c == '\n' ? 0 : 1;
} else if (c == '\n') {
this.line += 1;
this.character = 0;
} else {
this.character += 1;
}
this.previous = (char) c;
return this.previous;
} |
3a9d5171-e3e7-4082-83be-231bdb29e675 | public char next(char c) throws JSONException {
char n = this.next();
if (n != c) {
throw this.syntaxError("Expected '" + c + "' and instead saw '"
+ n + "'");
}
return n;
} |
87bd0e2d-e494-4746-8199-46c3c378717a | public String next(int n) throws JSONException {
if (n == 0) {
return "";
}
char[] chars = new char[n];
int pos = 0;
while (pos < n) {
chars[pos] = this.next();
if (this.end()) {
throw this.syntaxError("Substring bounds error");
}
pos += 1;
}
return new String(chars);
} |
7495cbcb-e4c9-4479-a147-64f59d523916 | public char nextClean() throws JSONException {
for (;;) {
char c = this.next();
if (c == 0 || c > ' ') {
return c;
}
}
} |
f44fd593-2cb5-4047-83a3-30a86b94ab1a | public String nextString(char quote) throws JSONException {
char c;
StringBuilder sb = new StringBuilder();
for (;;) {
c = this.next();
switch (c) {
case 0:
case '\n':
case '\r':
throw this.syntaxError("Unterminated string");
case '\\':
c = this.next();
switch (c) {
case 'b':
sb.append('\b');
break;
case 't':
sb.append('\t');
break;
case 'n':
sb.append('\n');
break;
case 'f':
sb.append('\f');
break;
case 'r':
sb.append('\r');
break;
case 'u':
sb.append((char) Integer.parseInt(this.next(4), 16));
break;
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape.");
}
break;
default:
if (c == quote) {
return sb.toString();
}
sb.append(c);
}
}
} |
878f46d5-8ed3-4fa8-92f5-8dae984aba86 | public String nextTo(char delimiter) throws JSONException {
StringBuilder sb = new StringBuilder();
for (;;) {
char c = this.next();
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
if (c != 0) {
this.back();
}
return sb.toString().trim();
}
sb.append(c);
}
} |
8d67ee2b-637b-4b73-8615-d66aa90f48e0 | public String nextTo(String delimiters) throws JSONException {
char c;
StringBuilder sb = new StringBuilder();
for (;;) {
c = this.next();
if (delimiters.indexOf(c) >= 0 || c == 0
|| c == '\n' || c == '\r') {
if (c != 0) {
this.back();
}
return sb.toString().trim();
}
sb.append(c);
}
} |
895d03d1-8d6c-4775-b1e3-a2861d4fb126 | public Object nextValue() throws JSONException {
char c = this.nextClean();
String string;
switch (c) {
case '"':
case '\'':
return this.nextString(c);
case '{':
this.back();
return new JSONObject(this);
case '[':
this.back();
return new JSONArray(this);
}
StringBuilder sb = new StringBuilder();
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
sb.append(c);
c = this.next();
}
this.back();
string = sb.toString().trim();
if ("".equals(string)) {
throw this.syntaxError("Missing value");
}
return JSONObject.stringToValue(string);
} |
d2bf5601-567a-45b2-8e0d-1da8b7d30320 | public char skipTo(char to) throws JSONException {
char c;
try {
long startIndex = this.index;
long startCharacter = this.character;
long startLine = this.line;
this.reader.mark(1000000);
do {
c = this.next();
if (c == 0) {
this.reader.reset();
this.index = startIndex;
this.character = startCharacter;
this.line = startLine;
return c;
}
} while (c != to);
} catch (IOException exc) {
throw new JSONException(exc);
}
this.back();
return c;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.