use of com.google.gson.stream.JsonToken in project J2ME-Loader by nikita36078.
the class SparseIntArrayAdapter method read.
@Override
public SparseIntArray read(JsonReader in) throws IOException {
JsonToken peek = in.peek();
if (peek == JsonToken.NULL) {
in.nextNull();
return null;
}
SparseIntArray array = new SparseIntArray();
if (peek == JsonToken.STRING) {
String s = in.nextString();
try {
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
array.put(item.getInt("key"), item.getInt("value"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return array;
}
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
int key = Integer.parseInt(name);
int value = in.nextInt();
array.put(key, value);
}
in.endObject();
return array;
}
use of com.google.gson.stream.JsonToken in project intellij-community by JetBrains.
the class JsonReaderEx method subReader.
@Nullable
public JsonReaderEx subReader() {
JsonToken nextToken = peek();
switch(nextToken) {
case BEGIN_ARRAY:
case BEGIN_OBJECT:
case STRING:
case NUMBER:
case BOOLEAN:
break;
case NULL:
// just return null
return null;
default:
throw createParseError("Cannot create sub reader, next token " + nextToken + " is not value");
}
JsonReaderEx subReader = new JsonReaderEx(in, position, Arrays.copyOf(stack, stack.length));
subReader.stackSize = stackSize;
subReader.peeked = peeked;
subReader.peekedLong = peekedLong;
subReader.peekedNumberLength = peekedNumberLength;
subReader.peekedString = peekedString;
return subReader;
}
use of com.google.gson.stream.JsonToken in project sling by apache.
the class GetNodeContentCommand method execute.
@Override
public Result<ResourceProxy> execute() {
GetMethod get = new GetMethod(getPath());
try {
httpClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
int responseStatus = httpClient.executeMethod(get);
// return EncodingUtil.getString(rawdata, m.getResponseCharSet());
if (!isSuccessStatus(responseStatus))
return failureResultForStatusCode(responseStatus);
ResourceProxy resource = new ResourceProxy(path);
try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
JsonToken token = jsonReader.peek();
if (token == JsonToken.STRING) {
resource.addProperty(name, jsonReader.nextString());
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
return AbstractResult.success(resource);
} catch (Exception e) {
return AbstractResult.failure(new RepositoryException(e));
} finally {
get.releaseConnection();
}
}
use of com.google.gson.stream.JsonToken in project json-android-compare by martinadamek.
the class GsonJson method getValue.
static Object getValue(JsonReader r) throws IOException {
Object value = null;
JsonToken token = r.peek();
switch(token) {
case NULL:
r.nextNull();
break;
case BOOLEAN:
value = r.nextBoolean();
break;
default:
value = r.nextString();
}
return value;
}
use of com.google.gson.stream.JsonToken in project useful-java-links by Vedenin.
the class StreamingAPI method readJson.
/**
* Example to readJson using StreamingAPI
*/
private static void readJson() throws IOException {
String str = "{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}";
InputStream in = new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8")));
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
while (reader.hasNext()) {
JsonToken jsonToken = reader.peek();
if (jsonToken == JsonToken.BEGIN_OBJECT) {
reader.beginObject();
} else if (jsonToken == JsonToken.END_OBJECT) {
reader.endObject();
}
if (jsonToken == JsonToken.STRING) {
// print Hi World!
System.out.print(reader.nextString() + " ");
} else {
reader.skipValue();
}
}
reader.close();
}
Aggregations