Search in sources :

Example 81 with JsonReader

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();
}
Also used : JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken)

Example 82 with JsonReader

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 83 with JsonReader

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();
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Example 84 with JsonReader

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonReader(com.google.gson.stream.JsonReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with JsonReader

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

Aggregations

JsonReader (com.google.gson.stream.JsonReader)95 StringReader (java.io.StringReader)36 JsonElement (com.google.gson.JsonElement)30 Test (org.junit.Test)19 JsonObject (com.google.gson.JsonObject)17 IOException (java.io.IOException)17 InputStreamReader (java.io.InputStreamReader)17 JsonParser (com.google.gson.JsonParser)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)9 TypeToken (com.google.gson.reflect.TypeToken)8 JsonWriter (com.google.gson.stream.JsonWriter)8 Map (java.util.Map)7 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 BufferedReader (java.io.BufferedReader)4