final JSONTokener x = new JSONTokener(body);
char c;
String key;
if (x.nextClean() != '{') {
throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
body));
}
for (;;) {
c = x.nextClean();
switch (c) {
case 0:
throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
body));
case '}':
return Collections.unmodifiableMap(parameters);
default:
x.back();
key = x.nextValue().toString();
}
/*
* The key is followed by ':'. We will also tolerate '=' or '=>'.
*/
c = x.nextClean();
if (c == '=') {
if (x.next() != '>') {
x.back();
}
} else if (c != ':') {
throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
body, key));
}
Object value = x.nextValue();
// guard from null values
if (value != null) {
if (value instanceof JSONArray) { // only plain simple arrays in this version
JSONArray array = (JSONArray) value;
String[] values = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
values[i] = String.valueOf(array.get(i));
}
parameters.put(key, values);
} else {
parameters.put(key, new String[]{ String.valueOf(value) });
}
}
/*
* Pairs are separated by ','. We will also tolerate ';'.
*/
switch (x.nextClean()) {
case ';':
case ',':
if (x.nextClean() == '}') {
return Collections.unmodifiableMap(parameters);
}
x.back();
break;
case '}':
return Collections.unmodifiableMap(parameters);
default:
throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, Expected a ',' or '}",
body));
}
}
}