Search in sources :

Example 11 with JsonParser

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);
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 12 with JsonParser

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());
            }
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Example 13 with JsonParser

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 14 with JsonParser

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;
}
Also used : Response(com.squareup.okhttp.Response) JsonElement(com.google.gson.JsonElement) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParser(com.google.gson.JsonParser)

Example 15 with JsonParser

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");
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParser (com.google.gson.JsonParser)323 JsonObject (com.google.gson.JsonObject)263 JsonElement (com.google.gson.JsonElement)88 JsonArray (com.google.gson.JsonArray)49 IOException (java.io.IOException)39 Gson (com.google.gson.Gson)31 InputStreamReader (java.io.InputStreamReader)24 HashMap (java.util.HashMap)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)16 JsonReader (com.google.gson.stream.JsonReader)11 Test (org.junit.Test)11 AssetManager (android.content.res.AssetManager)10 InputStream (java.io.InputStream)10 Type (java.lang.reflect.Type)10 URL (java.net.URL)9 Test (org.testng.annotations.Test)9 UserType (com.glitchcog.fontificator.bot.UserType)8 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 JsonParseException (com.google.gson.JsonParseException)8