List of usage examples for com.google.gson.stream JsonReader nextNull
public void nextNull() throws IOException
From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java
License:Apache License
static Map<String, Object> parseObject(final JsonReader jsonReader) throws IOException { Map<String, Object> map = new HashMap<>(); String tempKey;// w w w . j a v a2 s . com JsonToken tok; // while (jsonReader.hasNext()) { // since we want to restore a map we assue a key/value pair tempKey = jsonReader.nextName(); // tok = jsonReader.peek(); // we reach the end of this array if (JsonToken.END_ARRAY == tok) { jsonReader.endArray(); // return break; } // what token switch (tok) { // if array/map - parse and append case BEGIN_ARRAY: map.put(tempKey, parseArray(jsonReader)); break; case BEGIN_OBJECT: map.put(tempKey, parseObject(jsonReader)); break; // if raw type case STRING: map.put(tempKey, jsonReader.nextString()); break; case BOOLEAN: map.put(tempKey, jsonReader.nextBoolean()); break; case NUMBER: map.put(tempKey, jsonReader.nextDouble()); break; // if null , add null and consume case NULL: map.put(tempKey, null); jsonReader.nextNull(); break; // all other cases are errors default: throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array"); } } return map; }
From source file:io.datakernel.datagraph.server.GsonClassTypeAdapter.java
License:Apache License
@Override public Class<?> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; }/*w w w .j ava2 s. co m*/ try { String className = in.nextString(); return Class.forName(className); } catch (ClassNotFoundException e) { throw new IOException(e); } }
From source file:io.datakernel.datagraph.server.GsonInetSocketAddressAdapter.java
License:Apache License
@Override public InetSocketAddress read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; }/* w w w .jav a2s . c o m*/ try { Iterator<String> split = Splitter.on(':').split(reader.nextString()).iterator(); InetAddress hostname = InetAddresses.forString(split.next()); int port = Integer.parseInt(split.next()); checkArgument(!split.hasNext()); return new InetSocketAddress(hostname, port); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); } }
From source file:io.datakernel.datagraph.server.GsonStreamIdAdapter.java
License:Apache License
@Override public StreamId read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; }//from w w w. j a v a 2 s .c o m return new StreamId(reader.nextLong()); }
From source file:io.grpc.internal.JsonParser.java
License:Apache License
private static Void parseJsonNull(JsonReader jr) throws IOException { jr.nextNull(); return null; }
From source file:io.prediction.workflow.JavaQueryTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (type.getRawType().equals(JavaQuery.class)) { return (TypeAdapter<T>) new TypeAdapter<JavaQuery>() { public void write(JsonWriter out, JavaQuery value) throws IOException { if (value == null) { out.nullValue();/* w w w. j a v a2 s .c om*/ } else { out.beginObject(); out.name("q").value(value.getQ().toUpperCase()); out.endObject(); } } public JavaQuery read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } else { reader.beginObject(); reader.nextName(); String q = reader.nextString(); reader.endObject(); return new JavaQuery(q.toUpperCase()); } } }; } else { return null; } }
From source file:io.ucoin.ucoinj.core.client.model.bma.gson.EndpointAdapter.java
License:Open Source License
@Override public NetworkPeering.Endpoint read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }/*from w w w .j av a 2 s . com*/ String ept = reader.nextString(); ArrayList<String> parts = new ArrayList<>(Arrays.asList(ept.split(" "))); NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint(); endpoint.port = Integer.parseInt(parts.remove(parts.size() - 1)); for (String word : parts) { if (InetAddressUtils.isIPv4Address(word)) { endpoint.ipv4 = word; } else if (InetAddressUtils.isIPv6Address(word)) { endpoint.ipv6 = word; } else if (word.startsWith("http")) { endpoint.url = word; } else { try { endpoint.protocol = EndpointProtocol.valueOf(word); } catch (IllegalArgumentException e) { // skip this part } } } if (endpoint.protocol == null) { endpoint.protocol = EndpointProtocol.UNDEFINED; } return endpoint; }
From source file:it.units.inginf.male.outputs.gson.DoubleTypeAdapter.java
License:Open Source License
@Override public Double read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; }// w w w. j a va 2s . c o m Double nextDouble; try { //nextDouble parses as a double or fallback to parsing a string using Java parseDouble which MANAGES NaN nextDouble = in.nextDouble(); } catch (NumberFormatException ex) { nextDouble = Double.NaN; } return nextDouble; }
From source file:net.chris54721.infinitycubed.utils.LowercaseEnumTypeAdapterFactory.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; }//from w w w . j a va2 s.co m final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); 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(reader.nextString()); } } }; }
From source file:net.daporkchop.toobeetooteebot.text.EnumTypeAdapterFactory.java
License:Open Source License
public <T> TypeAdapter<T> create(Gson p_create_1_, TypeToken<T> p_create_2_) { Class<T> oclass = (Class<T>) p_create_2_.getRawType(); if (!oclass.isEnum()) { return null; } else {/*from ww w . j ava 2s . c o m*/ final Map<String, T> map = Maps.newHashMap(); for (T t : oclass.getEnumConstants()) { map.put(this.getName(t), t); } return new TypeAdapter<T>() { public void write(JsonWriter p_write_1_, T p_write_2_) throws IOException { if (p_write_2_ == null) { p_write_1_.nullValue(); } else { p_write_1_.value(EnumTypeAdapterFactory.this.getName(p_write_2_)); } } public T read(JsonReader p_read_1_) throws IOException { if (p_read_1_.peek() == JsonToken.NULL) { p_read_1_.nextNull(); return null; } else { return map.get(p_read_1_.nextString()); } } }; } }