List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts) throws IOException, JSONException, Exception { String n = null;//from w w w. j ava2 s .c om int i = 0; while (reader.hasNext()) { JsonToken peek = reader.peek(); String v = null; if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_ARRAY) { if (n.equals(tableName)) { i = readJsnArr(reader, tableName, clazz); } else { if (n.equals("params")) { reader.beginArray(); if (reader.hasNext()) { reader.beginObject(); if (reader.hasNext()) { n = reader.nextName(); v = reader.nextString(); } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.END_ARRAY) { reader.endArray(); } } return i; }
From source file:se.sics.datamodel.gson.TypeInfoAdapter.java
License:Open Source License
@Override public TypeInfo read(JsonReader reader) throws IOException { String typeName;/*w w w. j a v a2 s . c o m*/ Gson gson = GsonHelper.getGson(); reader.beginObject(); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("typeName")) { throw new IOException(); } typeName = reader.nextString(); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("fieldMap")) { throw new IOException(); } if (reader.peek() != JsonToken.BEGIN_ARRAY) { throw new IOException(); } reader.beginArray(); TypeInfo.Builder tiBuilder = new TypeInfo.Builder(typeName); while (reader.peek() != JsonToken.END_ARRAY) { if (reader.peek() != JsonToken.BEGIN_OBJECT) { throw new IOException(); } reader.beginObject(); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("id")) { throw new IOException(); } ByteId fieldId = new ByteId((byte[]) gson.fromJson(reader, byte[].class)); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("field")) { throw new IOException(); } FieldInfo fieldInfo = gson.fromJson(reader, FieldInfo.class); tiBuilder.put(fieldId, fieldInfo); reader.endObject(); } reader.endArray(); reader.endObject(); return tiBuilder.build(); }
From source file:se.sics.datamodel.gson.ValueHolderAdapter.java
License:Open Source License
@Override public ValueHolder read(JsonReader reader) throws IOException { Gson gson = GsonHelper.getGson();//w w w . j av a 2 s .c om ValueHolder.Builder vhBuilder = new ValueHolder.Builder(); if (reader.peek() != JsonToken.BEGIN_OBJECT) { throw new IOException(); } reader.beginObject(); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("fieldMap")) { throw new IOException(); } if (reader.peek() != JsonToken.BEGIN_ARRAY) { throw new IOException(); } reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { if (reader.peek() != JsonToken.BEGIN_OBJECT) { throw new IOException(); } reader.beginObject(); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("id")) { throw new IOException(); } ByteId fieldId = new ByteId((byte[]) gson.fromJson(reader, byte[].class)); if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("field")) { throw new IOException(); } JsonElement je = gson.fromJson(reader, JsonElement.class); vhBuilder.put(fieldId, je); if (reader.peek() != JsonToken.END_OBJECT) { throw new IOException(); } reader.endObject(); } reader.endArray(); reader.endObject(); return vhBuilder.build(); }
From source file:teste.adapter.ArrayAdapter.java
public List<T> read(JsonReader reader) throws IOException { List<T> list = new ArrayList<T>(); Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { T inning = gson.fromJson(reader, adapterclass); list.add(inning);//from w ww . j a v a2s . co m } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { T inning = gson.fromJson(reader, adapterclass); list.add(inning); } reader.endArray(); } return list; }
From source file:us.blanshard.sudoku.android.NetworkService.java
License:Apache License
private void sendBatch(List<Rpc.Request> batch, final Map<Integer, RpcCall<?>> calls) { String json = GSON.toJson(batch); Rpc.Response<Object> singleReponse = null; HttpURLConnection conn = null; try {//from w w w.j a v a2s . c om URL url = new URL(RPC_URL); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout((int) SECONDS.toMillis(15)); conn.setReadTimeout((int) SECONDS.toMillis(60)); // App engine request timeout Writer out = new OutputStreamWriter(conn.getOutputStream(), Charsets.UTF_8); out.write(json); out.flush(); conn.connect(); conn.getHeaderFields(); // Waits for the response if (url.getHost().equals(conn.getURL().getHost())) { if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { Reader reader = new InputStreamReader(conn.getInputStream(), Charsets.UTF_8); JsonReader in = new JsonReader(reader); if (in.peek() == JsonToken.BEGIN_ARRAY) { Json.setIdToType(new Function<Integer, Type>() { @Override public Type apply(@Nullable Integer input) { return calls.get(input).op.getResultTypeToken().getType(); } }); try { List<Rpc.Response<?>> responses = GSON.fromJson(in, BATCH_RESULT_TOKEN.getType()); // Log.d(TAG, "#responses: " + responses.size()); for (Rpc.Response<?> res : responses) { RpcCall<?> call = calls.get(res.id); if (call == null) { Log.w(TAG, "Mismatched ID in batch response: " + res.id); } else { call.setResponse(res); } } } finally { Json.setIdToType(null); } } else { // Probably just a single error, something bad happened. singleReponse = GSON.fromJson(in, Rpc.Response.class); if (singleReponse.error == null) singleReponse.error = Rpc.error(0, "unknown", null); // Log.d(TAG, "RPC batch returned single response: " + GSON.toJson(singleReponse)); } } else { // Unexpected error from the server. Make a response object, so we // don't retry. singleReponse = new Rpc.Response<Object>(); singleReponse.error = Rpc.error(conn.getResponseCode(), conn.getResponseMessage(), null); } if (singleReponse != null) { for (RpcCall<?> call : calls.values()) call.setResponse(singleReponse); } } else { // Whoops, we were redirected unexpectedly. Possibly there's a // network sign-on page. Log.e(TAG, "redirected trying to send RPC: " + conn.getURL()); } } catch (Exception e) { Log.e(TAG, "send RPC batch", e); } finally { if (conn != null) conn.disconnect(); } }
From source file:util.android.gson.ArrayAdapter.java
License:Apache License
@Override public List<T> read(JsonReader reader) throws IOException { List<T> list = new ArrayList<T>(); Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { T inning = gson.fromJson(reader, adapterclass); list.add(inning);// w w w . j a v a2 s.c o m } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { T inning = gson.fromJson(reader, adapterclass); list.add(inning); } reader.endArray(); } return list; }