List of usage examples for com.google.gson.stream JsonReader endObject
public void endObject() throws IOException
From source file:BundleTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* w ww .ja v a2 s.c om*/ 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 w w w. ja v a2s .co m 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: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 w ww . j av a 2 s. c om*/ 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 w w . j a v a 2 s . c o 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(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);/* w ww .j av a2 s . 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(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 a 2 s . c o m*/ 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. j av a 2s . c o 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 ww .j av a2 s . 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 . jav a 2 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))); }
From source file:ch.cyberduck.core.importer.ExpandriveBookmarkCollection.java
License:Open Source License
@Override protected void parse(final Local file) throws AccessDeniedException { try {/*from ww w . ja va2 s . co m*/ final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8")); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); final Host current = new Host(new FTPProtocol(), PreferencesFactory.get().getProperty("connection.hostname.default")); while (reader.hasNext()) { final String name = reader.nextName(); switch (name) { case "server": current.setHostname(reader.nextString()); break; case "username": current.getCredentials().setUsername(reader.nextString()); break; case "private_key_file": current.getCredentials().setIdentity(LocalFactory.get(reader.nextString())); break; case "remotePath": current.setDefaultPath(reader.nextString()); break; case "type": final Protocol type = ProtocolFactory.forName(reader.nextString()); if (null != type) { current.setProtocol(type); } break; case "protocol": final Protocol protocol = ProtocolFactory.forName(reader.nextString()); if (null != protocol) { current.setProtocol(protocol); // Reset port to default current.setPort(-1); } break; case "name": current.setNickname(reader.nextString()); break; case "region": current.setRegion(reader.nextString()); break; default: log.warn(String.format("Ignore property %s", name)); reader.skipValue(); break; } } reader.endObject(); this.add(current); } reader.endArray(); } catch (IllegalStateException | IOException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }