List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:au.edu.unsw.cse.soc.federatedcloud.deployers.github.repository.GitHubRepoDeploymentWrapper.java
License:Open Source License
protected <V> V parseJson(InputStream stream, Type type, Type listType) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(stream, CHARSET_UTF8), bufferSize); if (listType == null) try {/*from w w w . ja va2 s .co m*/ return gson.fromJson(reader, type); } catch (JsonParseException jpe) { IOException ioe = new IOException("Parse exception converting JSON to object"); //$NON-NLS-1$ ioe.initCause(jpe); throw ioe; } finally { try { reader.close(); } catch (IOException ignored) { // Ignored } } else { JsonReader jsonReader = new JsonReader(reader); try { if (jsonReader.peek() == BEGIN_ARRAY) return gson.fromJson(jsonReader, listType); else return gson.fromJson(jsonReader, type); } catch (JsonParseException jpe) { IOException ioe = new IOException("Parse exception converting JSON to object"); //$NON-NLS-1$ ioe.initCause(jpe); throw ioe; } finally { try { jsonReader.close(); } catch (IOException ignored) { // Ignored } } } }
From source file:blog.ClassTypeAdapter.java
License:Apache License
@Override public Class<?> read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull();//from ww w .ja v a 2 s . c o m return null; } Class<?> clazz; try { clazz = Class.forName(jsonReader.nextString()); } catch (ClassNotFoundException exception) { throw new IOException(exception); } return clazz; }
From source file:br.com.caelum.restfulie.gson.converters.DateTypeAdapter.java
License:Apache License
@Override public Date read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();//from w ww . ja v a2s . com return null; } return deserializeToDate(in.nextString()); }
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(); }// w w w .j a v a 2 s . c o 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:ch.cyberduck.core.importer.JsonBookmarkCollection.java
License:Open Source License
protected String readNext(final String name, final JsonReader reader) throws IOException { if (reader.peek() != JsonToken.NULL) { return reader.nextString(); } else {/* w ww .j av a 2 s.co m*/ reader.skipValue(); log.warn(String.format("No value for key %s", name)); return null; } }
From source file:cloud.artik.client.JSON.java
License:Apache License
@Override public DateTime read(JsonReader in) throws IOException { switch (in.peek()) { case NULL:/* w ww . jav a2 s .c o m*/ in.nextNull(); return null; default: String date = in.nextString(); return formatter.parseDateTime(date); } }
From source file:cn.ieclipse.af.demo.sample.volley.adapter.IntAdapter.java
License:Apache License
@Override public Number read(JsonReader in) throws IOException { int num = 0;/*www. j av a 2 s. co m*/ // 0 if (in.peek() == JsonToken.NULL) { in.nextNull(); num = 0; } else { try { double input = in.nextDouble();//?double?? num = (int) input;//int } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } return num; }
From source file:cn.ieclipse.af.demo.sample.volley.adapter.StringAdapter.java
License:Apache License
@Override public String read(JsonReader reader) { String text = ""; try {/*from ww w. j av a 2 s . c o m*/ if (reader.peek() == JsonToken.NULL) { reader.nextNull(); text = "";//Null } else { text = reader.nextString(); } } catch (Exception e) { e.printStackTrace(); } return text; }
From source file:co.cask.cdap.client.StreamClient.java
License:Apache License
/** * Reads events from a stream/*w ww . ja v a 2s. co m*/ * * @param streamId ID of the stream * @param start Timestamp in milliseconds or now-xs format to start reading event from (inclusive) * @param end Timestamp in milliseconds or now-xs format for the last event to read (exclusive) * @param limit Maximum number of events to read * @param callback Callback to invoke for each stream event read. If the callback function returns {@code false} * upon invocation, it will stops the reading * @throws IOException If fails to read from stream * @throws StreamNotFoundException If the given stream does not exists */ public void getEvents(Id.Stream streamId, String start, String end, int limit, Function<? super StreamEvent, Boolean> callback) throws IOException, StreamNotFoundException, UnauthenticatedException { long startTime = TimeMathParser.parseTime(start, TimeUnit.MILLISECONDS); long endTime = TimeMathParser.parseTime(end, TimeUnit.MILLISECONDS); URL url = config.resolveNamespacedURLV3(streamId.getNamespace(), String .format("streams/%s/events?start=%d&end=%d&limit=%d", streamId.getId(), startTime, endTime, limit)); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); AccessToken accessToken = config.getAccessToken(); if (accessToken != null) { urlConn.setRequestProperty(HttpHeaders.AUTHORIZATION, accessToken.getTokenType() + " " + accessToken.getValue()); } if (urlConn instanceof HttpsURLConnection && !config.isVerifySSLCert()) { try { HttpRequests.disableCertCheck((HttpsURLConnection) urlConn); } catch (Exception e) { // TODO: Log "Got exception while disabling SSL certificate check for request.getURL()" } } try { if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { throw new UnauthenticatedException("Unauthorized status code received from the server."); } if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new StreamNotFoundException(streamId); } if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { return; } // The response is an array of stream event object InputStream inputStream = urlConn.getInputStream(); JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8)); jsonReader.beginArray(); while (jsonReader.peek() != JsonToken.END_ARRAY) { Boolean result = callback.apply(GSON.<StreamEvent>fromJson(jsonReader, StreamEvent.class)); if (result == null || !result) { break; } } drain(inputStream); // No need to close reader, the urlConn.disconnect in finally will close all underlying streams } finally { urlConn.disconnect(); } }
From source file:co.cask.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory.java
License:Apache License
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { @SuppressWarnings("unchecked") Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; }/*w w w.j a v a 2s . c o m*/ final Map<String, T> lowercaseToConstant = new HashMap<>(); for (T constant : rawType.getEnumConstants()) { lowercaseToConstant.put(toLowercase(constant), constant); } return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(toLowercase(value)); } } public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } else { return lowercaseToConstant.get(toLowercase(reader.nextString())); } } }; }