use of com.google.gson.JsonParser in project useful-java-links by Vedenin.
the class TreeModel method readJson.
/**
* Example to readJson using TreeModel
*/
private static void readJson() throws IOException {
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse("{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}");
JsonObject rootObject = jsonElement.getAsJsonObject();
// get property "message"
String message = rootObject.get("message").getAsString();
// get place object
JsonObject childObject = rootObject.getAsJsonObject("place");
// get property "name"
String place = childObject.get("name").getAsString();
// print "Hi World!"*/
System.out.println(message + " " + place);
}
use of com.google.gson.JsonParser in project malmo by Microsoft.
the class ObservationFromServer method writeObservationsToJSON.
@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
String jsonstring = "";
synchronized (this.latestJsonStats) {
jsonstring = this.latestJsonStats;
}
if (// "{}" is the empty JSON string.
jsonstring.length() > 2) {
// Parse the string into json:
JsonParser parser = new JsonParser();
JsonElement root = parser.parse(jsonstring);
// Now copy the children of root into the provided json object:
if (root.isJsonObject()) {
JsonObject rootObj = root.getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : rootObj.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
}
}
}
use of com.google.gson.JsonParser in project MinecraftForge by MinecraftForge.
the class MetadataCollection method from.
public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
if (inputStream == null) {
return new MetadataCollection();
}
InputStreamReader reader = new InputStreamReader(inputStream);
try {
MetadataCollection collection;
Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
JsonParser parser = new JsonParser();
JsonElement rootElement = parser.parse(reader);
if (rootElement.isJsonArray()) {
collection = new MetadataCollection();
JsonArray jsonList = rootElement.getAsJsonArray();
collection.modList = new ModMetadata[jsonList.size()];
int i = 0;
for (JsonElement mod : jsonList) {
collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
}
} else {
collection = gson.fromJson(rootElement, MetadataCollection.class);
}
collection.parseModMetadataList();
return collection;
} catch (JsonParseException e) {
FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
return new MetadataCollection();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of com.google.gson.JsonParser in project remusic by aa112901.
the class HttpUtil method getResposeJsonObject.
public static JsonObject getResposeJsonObject(String action1) {
try {
mOkHttpClient.setConnectTimeout(3000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(3000, TimeUnit.MINUTES);
Request request = new Request.Builder().url(action1).build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String c = response.body().string();
// FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/" + System.currentTimeMillis() + ".txt");
// fileOutputStream.write(c.getBytes());
// fileOutputStream.close();
JsonParser parser = new JsonParser();
JsonElement el = parser.parse(c);
return el.getAsJsonObject();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.google.gson.JsonParser in project Minechem by iopleke.
the class ElementHandler method readFromStream.
private static void readFromStream(InputStream stream) {
JsonReader jReader = new JsonReader(new InputStreamReader(stream));
JsonParser parser = new JsonParser();
Set<Map.Entry<String, JsonElement>> elementsSet = parser.parse(jReader).getAsJsonObject().entrySet();
int count = 0;
for (Map.Entry<String, JsonElement> elementEntry : elementsSet) {
if (!elementEntry.getValue().isJsonObject()) {
continue;
}
JsonObject elementObject = elementEntry.getValue().getAsJsonObject();
ElementRegistry.getInstance().registerElement(Integer.parseInt(elementEntry.getKey()), elementObject.get("longName").getAsString(), elementObject.get("shortName").getAsString(), elementObject.get("form").getAsString(), elementObject.get("type").getAsString(), Integer.parseInt(elementObject.get("neutrons").getAsString()));
count++;
}
LogHelper.info("Total of " + count + " elements registered");
}
Aggregations