Android Open Source - Android-JSONCompare Gson Json






From Project

Back to project page Android-JSONCompare.

License

The source code is released under:

Apache License

If you think the Android project Android-JSONCompare listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.devspark.jsoncompare;
//  w  w w.j  a v a2  s .  co  m
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @author e.shishkin
 *
 */
public class GsonJson implements JsonHandler {

    public String getName() {
        return "Gson";
    }

    public List<Map<String, Object>> parsePublicTimeline(InputStream inputStream) {

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

        try {
            JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));

            reader.beginArray();
            while (reader.hasNext()) {
                Map<String, Object> map = new HashMap<String, Object>();

                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if ("user".equals(name)) {
                        reader.beginObject();
                        while (reader.hasNext()) {
                            String 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;
    }

    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;
    }

}




Java Source Code List

com.devspark.jsoncompare.AndroidJson.java
com.devspark.jsoncompare.GsonJson.java
com.devspark.jsoncompare.JacksonJson.java
com.devspark.jsoncompare.JsonHandler.java
com.devspark.jsoncompare.MainActivity.java
com.devspark.jsoncompare.SimpleJson.java