use of com.google.gson.stream.JsonReader 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();
}
use of com.google.gson.stream.JsonReader in project json-android-compare by martinadamek.
the class GsonJson method parsePublicTimeline.
public List<Map> parsePublicTimeline(InputStream inputStream) {
List<Map> result = new ArrayList<Map>();
try {
Map map;
String name;
String name2;
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
reader.beginArray();
while (reader.hasNext()) {
map = new HashMap();
reader.beginObject();
while (reader.hasNext()) {
name = reader.nextName();
if ("user".equals(name)) {
reader.beginObject();
while (reader.hasNext()) {
name2 = reader.nextName();
map.put("user." + name2, getValue(reader));
}
reader.endObject();
} else {
map.put(name, getValue(reader));
}
}
reader.endObject();
result.add(map);
}
reader.endArray();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of com.google.gson.stream.JsonReader in project mortar by square.
the class GsonParceler method decode.
private Object decode(String json) throws IOException {
JsonReader reader = new JsonReader(new StringReader(json));
try {
reader.beginObject();
Class<?> type = Class.forName(reader.nextName());
return gson.fromJson(reader, type);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
reader.close();
}
}
use of com.google.gson.stream.JsonReader in project intellij-community by JetBrains.
the class RestService method createJsonReader.
@NotNull
protected static JsonReader createJsonReader(@NotNull FullHttpRequest request) {
JsonReader reader = new JsonReader(new InputStreamReader(new ByteBufInputStream(request.content()), CharsetToolkit.UTF8_CHARSET));
reader.setLenient(true);
return reader;
}
use of com.google.gson.stream.JsonReader in project intellij-community by JetBrains.
the class PluginsAdvertiser method retrieve.
public static List<Plugin> retrieve(UnknownFeature unknownFeature) {
final String featureType = unknownFeature.getFeatureType();
final String implementationName = unknownFeature.getImplementationName();
final String buildNumber = ApplicationInfo.getInstance().getApiVersion();
final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType=" + featureType + "&implementationName=" + implementationName.replaceAll("#", "%23") + "&build=" + buildNumber;
return HttpRequests.request(pluginRepositoryUrl).connect(new HttpRequests.RequestProcessor<List<Plugin>>() {
@Override
public List<Plugin> process(@NotNull HttpRequests.Request request) throws IOException {
final JsonReader jsonReader = new JsonReader(request.getReader());
jsonReader.setLenient(true);
final JsonElement jsonRootElement = new JsonParser().parse(jsonReader);
final List<Plugin> result = new ArrayList<>();
for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) {
final JsonObject jsonObject = jsonElement.getAsJsonObject();
final JsonElement pluginId = jsonObject.get("pluginId");
final JsonElement pluginName = jsonObject.get("pluginName");
final JsonElement bundled = jsonObject.get("bundled");
result.add(new Plugin(PluginId.getId(StringUtil.unquoteString(pluginId.toString())), pluginName != null ? StringUtil.unquoteString(pluginName.toString()) : null, Boolean.parseBoolean(StringUtil.unquoteString(bundled.toString()))));
}
return result;
}
}, null, LOG);
}
Aggregations