Example usage for com.google.gson.stream JsonReader nextName

List of usage examples for com.google.gson.stream JsonReader nextName

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextName.

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:BundleTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from w w w .  j  a  v a2 s .com
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
    if (!Bundle.class.isAssignableFrom(type.getRawType())) {
        return null;
    }
    return (TypeAdapter<T>) new TypeAdapter<Bundle>() {
        @Override
        public void write(JsonWriter out, Bundle bundle) throws IOException {
            if (bundle == null) {
                out.nullValue();
                return;
            }
            out.beginObject();
            for (String key : bundle.keySet()) {
                out.name(key);
                Object value = bundle.get(key);
                if (value == null) {
                    out.nullValue();
                } else {
                    gson.toJson(value, value.getClass(), out);
                }
            }
            out.endObject();
        }

        @Override
        public Bundle read(JsonReader in) throws IOException {
            switch (in.peek()) {
            case NULL:
                in.nextNull();
                return null;
            case BEGIN_OBJECT:
                return toBundle(readObject(in));
            default:
                throw new IOException("expecting object: " + in.getPath());
            }
        }

        private Bundle toBundle(List<Pair<String, Object>> values) throws IOException {
            Bundle bundle = new Bundle();
            for (Pair<String, Object> entry : values) {
                String key = entry.first;
                Object value = entry.second;
                if (value instanceof String) {
                    bundle.putString(key, (String) value);
                } else if (value instanceof Integer) {
                    bundle.putInt(key, ((Integer) value).intValue());
                } else if (value instanceof Long) {
                    bundle.putLong(key, ((Long) value).longValue());
                } else if (value instanceof Double) {
                    bundle.putDouble(key, ((Double) value).doubleValue());
                } else if (value instanceof Parcelable) {
                    bundle.putParcelable(key, (Parcelable) value);
                } else if (value instanceof List) {
                    List<Pair<String, Object>> objectValues = (List<Pair<String, Object>>) value;
                    Bundle subBundle = toBundle(objectValues);
                    bundle.putParcelable(key, subBundle);
                } else {
                    throw new IOException("Unparcelable key, value: " + key + ", " + value);
                }
            }
            return bundle;
        }

        private List<Pair<String, Object>> readObject(JsonReader in) throws IOException {
            List<Pair<String, Object>> object = new ArrayList<Pair<String, Object>>();
            in.beginObject();
            while (in.peek() != JsonToken.END_OBJECT) {
                switch (in.peek()) {
                case NAME:
                    String name = in.nextName();
                    Object value = readValue(in);
                    object.add(new Pair<String, Object>(name, value));
                    break;
                case END_OBJECT:
                    break;
                default:
                    throw new IOException("expecting object: " + in.getPath());
                }
            }
            in.endObject();
            return object;
        }

        private Object readValue(JsonReader in) throws IOException {
            switch (in.peek()) {
            case BEGIN_ARRAY:
                return readArray(in);
            case BEGIN_OBJECT:
                return readObject(in);
            case BOOLEAN:
                return in.nextBoolean();
            case NULL:
                in.nextNull();
                return null;
            case NUMBER:
                return readNumber(in);
            case STRING:
                return in.nextString();
            default:
                throw new IOException("expecting value: " + in.getPath());
            }
        }

        private Object readNumber(JsonReader in) throws IOException {
            double doubleValue = in.nextDouble();
            if (doubleValue - Math.ceil(doubleValue) == 0) {
                long longValue = (long) doubleValue;
                if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
                    return (int) longValue;
                }
                return longValue;
            }
            return doubleValue;
        }

        @SuppressWarnings("rawtypes")
        private List readArray(JsonReader in) throws IOException {
            List list = new ArrayList();
            in.beginArray();
            while (in.peek() != JsonToken.END_ARRAY) {
                Object element = readValue(in);
                list.add(element);
            }
            in.endArray();
            return list;
        }
    };
}

From source file:at.univie.sensorium.preferences.Preferences.java

License:Open Source License

private void loadPrefsFromStream(InputStream input) {
    List<BasicNameValuePair> preferencelist = new LinkedList<BasicNameValuePair>();
    try {/*from  ww w  .j av a 2 s .  com*/
        InputStreamReader isreader = new InputStreamReader(input);
        JsonReader reader = new JsonReader(isreader);
        //         String jsonVersion = "";

        reader.beginArray(); // do we have an array or just a single object?
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            String value = reader.nextString();
            if (name.equalsIgnoreCase(PREFERENCES_VERSION))
                currentPrefVersion = Integer.valueOf(value);
            BasicNameValuePair kv = new BasicNameValuePair(name, value);
            preferencelist.add(kv);
        }
        reader.endObject();
        reader.endArray();
        reader.close();

        if (newerPrefsAvailable()) {
            Log.d(SensorRegistry.TAG, "Newer preferences available in json, overwriting existing.");
            for (BasicNameValuePair kv : preferencelist) {
                putPreference(kv.getName(), kv.getValue());
            }
            // also reset the welcome screen
            putBoolean(WELCOME_SCREEN_SHOWN, false);
        } else {
            Log.d(SensorRegistry.TAG, "Preferences are recent, not overwriting.");
        }

    } catch (FileNotFoundException e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        Log.d(SensorRegistry.TAG, sw.toString());
    } catch (IOException e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        Log.d(SensorRegistry.TAG, sw.toString());
    }
}

From source file:bytehala.flowmortarexample.GsonParceler.java

License:Apache License

private Path decode(String json) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(json));

    try {/*  w ww .ja v  a2  s  . c o  m*/
        reader.beginObject();

        Class<?> type = Class.forName(reader.nextName());
        return gson.fromJson(reader, type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        reader.close();
    }
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

@Override
public Usage read(JsonReader in) throws IOException {

    if (in.peek() == JsonToken.STRING) {
        String val = in.nextString();
        Asserts.assertEquals("NoUsage", val, "Invalid JSON. Expected 'NoUsage', but found '" + val + "'.");
        return new NoUsage();
    }//from   ww w . j  a va 2  s.co m

    Query q = new Query();
    q.setAllCallsites(null);

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (TYPE.equals(name)) {
            q.setType(CoReTypeName.get(in.nextString()));
        } else if (CLASS_CTX.equals(name)) {
            q.setClassContext(CoReTypeName.get(in.nextString()));
        } else if (METHOD_CTX.equals(name)) {
            q.setMethodContext(CoReMethodName.get(in.nextString()));
        } else if (DEFINITION.equals(name)) {
            q.setDefinition(readDefinition(in));
        } else if (SITES.equals(name)) {
            q.setAllCallsites(readCallSites(in));
        } else {
            // skip value (most likely $type key from .net serialization)
            in.nextString();
        }
    }
    in.endObject();
    return q;
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

private DefinitionSite readDefinition(JsonReader in) throws IOException {
    DefinitionSite def = DefinitionSites.createUnknownDefinitionSite();
    def.setKind(null);// w ww .  j  a va  2s .  com

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (DEF_KIND.equals(name)) {
            def.setKind(DefinitionSiteKind.valueOf(in.nextString()));
        } else if (DEF_ARG.equals(name)) {
            def.setArgIndex(in.nextInt());
        } else if (DEF_FIELD.equals(name)) {
            def.setField(CoReFieldName.get(in.nextString()));
        } else if (DEF_METHOD.equals(name)) {
            def.setMethod(CoReMethodName.get(in.nextString()));
        }
    }
    in.endObject();

    return def;
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

private CallSite readCallSite(JsonReader in) throws IOException {
    CallSite site = CallSites.createReceiverCallSite("LT.m()V");
    site.setKind(null);/*ww w.j  av a  2s .c  om*/
    site.setMethod(null);

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (CS_ARG.equals(name)) {
            site.setArgIndex(in.nextInt());
        } else if (CS_CALL.equals(name)) {
            site.setMethod(CoReMethodName.get(in.nextString()));
        } else if (CS_KIND.equals(name)) {
            site.setKind(CallSiteKind.valueOf(in.nextString()));
        }
    }
    in.endObject();
    return site;
}

From source file:cc.recommenders.utils.gson.UsageTypeAdapter.java

License:Open Source License

@Override
public Usage read(JsonReader in) throws IOException {
    Query q = new Query();
    q.setAllCallsites(null);//  ww  w.j  a  v  a2  s . c om

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (TYPE.equals(name)) {
            q.setType(VmTypeName.get(in.nextString()));
        } else if (CLASS_CTX.equals(name)) {
            q.setClassContext(VmTypeName.get(in.nextString()));
        } else if (METHOD_CTX.equals(name)) {
            q.setMethodContext(VmMethodName.get(in.nextString()));
        } else if (DEFINITION.equals(name)) {
            q.setDefinition(readDefinition(in));
        } else if (SITES.equals(name)) {
            q.setAllCallsites(readCallSites(in));
        } else {
            // skip value (most likely $type key from .net serialization)
            in.nextString();
        }
    }
    in.endObject();
    return q;
}

From source file:cc.recommenders.utils.gson.UsageTypeAdapter.java

License:Open Source License

private DefinitionSite readDefinition(JsonReader in) throws IOException {
    DefinitionSite def = DefinitionSites.createUnknownDefinitionSite();
    def.setKind(null);//  w w w  .  ja  va2s . co m

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (DEF_KIND.equals(name)) {
            def.setKind(DefinitionSiteKind.valueOf(in.nextString()));
        } else if (DEF_ARG.equals(name)) {
            def.setArgIndex(in.nextInt());
        } else if (DEF_FIELD.equals(name)) {
            def.setField(VmFieldName.get(in.nextString()));
        } else if (DEF_METHOD.equals(name)) {
            def.setMethod(VmMethodName.get(in.nextString()));
        }
    }
    in.endObject();

    return def;
}

From source file:cc.recommenders.utils.gson.UsageTypeAdapter.java

License:Open Source License

private CallSite readCallSite(JsonReader in) throws IOException {
    CallSite site = CallSites.createReceiverCallSite("LT.m()V");
    site.setKind(null);/* w w w .j  a v a 2s .  c o m*/
    site.setMethod(null);

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (CS_ARG.equals(name)) {
            site.setArgIndex(in.nextInt());
        } else if (CS_CALL.equals(name)) {
            site.setMethod(VmMethodName.get(in.nextString()));
        } else if (CS_KIND.equals(name)) {
            site.setKind(CallSiteKind.valueOf(in.nextString()));
        }
    }
    in.endObject();
    return site;
}

From source file:cern.molr.inspector.json.MissionTypeAdapter.java

License:Apache License

@Override
public Mission read(JsonReader in) throws IOException {
    in.beginObject();//from w ww  .  ja va2 s .  c o m
    in.nextName();
    final String agentName = in.nextString();
    in.nextName();
    final String className = in.nextString();
    in.nextName();
    final String entryPointsString = in.nextString();
    in.endObject();

    return new MissionImpl(agentName, className, Arrays.asList(entryPointsString.split(LIST_SEPARATOR)));
}