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

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

Introduction

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

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

From source file:net.derquinse.common.gson.GsonByteString.java

License:Apache License

@Override
public ByteString read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }// w  w w.j a  v a 2 s .  c o  m
    final String string = in.nextString();
    return ByteString.fromHexString(string);
}

From source file:net.derquinse.common.gson.GsonHashCode.java

License:Apache License

@Override
public HashCode read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from  w  ww. j av  a2 s .  c  o m*/
    final String string = in.nextString();
    final ByteString byteString = ByteString.fromHexString(string);
    if (byteString.isEmpty()) {
        return null;
    }
    return byteString.toHashCode();
}

From source file:net.evecom.android.json.JsonSysOrgansData.java

License:Open Source License

public static ArrayList<MianPerson> getData(final String path, String entity_str) throws Exception {

    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    byte[] entity = entity_str.getBytes();
    conn.setConnectTimeout(5000);// ww  w  . j  a  v  a 2s.c  om
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
    conn.getOutputStream().write(entity);
    if (conn.getResponseCode() == 200) {
        InputStream inputstream = conn.getInputStream();
        StringBuffer buffer = new StringBuffer();
        byte[] b = new byte[1024];
        for (int i; (i = inputstream.read(b)) != -1;) {
            buffer.append(new String(b, 0, i));
        }
        StringReader reader = new StringReader(buffer.toString());
        JsonReader jsonReader = new JsonReader(reader);
        list = new ArrayList<MianPerson>();
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String nextname1 = "";
            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                nextname1 = jsonReader.nextInt() + "";
            } else {
                nextname1 = jsonReader.nextName();
            }
            if ("rows".equals(nextname1)) {
                jsonReader.beginArray();
                while (jsonReader.hasNext()) {
                    jsonReader.beginObject();
                    MianPerson person = new MianPerson();
                    while (jsonReader.hasNext()) {
                        String nextName = "";
                        if (jsonReader.peek() == JsonToken.NULL) {
                            jsonReader.nextNull();
                        } else if (jsonReader.peek() == JsonToken.NUMBER) {
                            nextName = jsonReader.nextInt() + "";
                        } else if (jsonReader.peek() == JsonToken.STRING) {
                            nextName = jsonReader.nextString() + "";
                        } else {
                            nextName = jsonReader.nextName();
                        }
                        String nextValue = "";
                        if (nextName.equals("IDCARDNO")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setIDCARDNO(nextValue);
                        } else if (nextName.equals("HOUSEHOLDID")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setHOUSEHOLDID(nextValue);
                        } else if (nextName.equals("AREAID")) {// id
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setAREAID(nextValue);
                        } else if (nextName.equals("AREANAME")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setAREANAME(nextValue);
                        } else if (nextName.equals("PERSONNAME")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setPERSONNAME(nextValue);
                        } else if (nextName.equals("MALEDICTID")) {//  1
                                                                   // 2
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            if (null != nextValue && "1".equals(nextValue)) {
                                person.setMALEDICTID("");
                            } else if (null != nextValue && "2".equals(nextValue)) {
                                person.setMALEDICTID("");
                            }
                        } else if (nextName.equals("BIRTH")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setBIRTH(nextValue);
                        } else if (nextName.equals("HOUSEADDR")) {// 
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setHOUSEADDR(nextValue);
                        } else if (nextName.equals("PERSONID")) {// id
                            if (jsonReader.peek() == JsonToken.NULL) {
                                jsonReader.nextNull();
                            } else if (jsonReader.peek() == JsonToken.NUMBER) {
                                nextValue = jsonReader.nextInt() + "";
                            } else {
                                nextValue = jsonReader.nextString();
                            }
                            // nextValue = jsonReader.nextString();
                            person.setPERSONID(nextValue);
                        }
                        System.out.println(nextName + "=" + nextValue);
                    }
                    list.add(person);
                    person = null;
                    jsonReader.endObject();
                }
                jsonReader.endArray();
            }
            //
        }
        jsonReader.endObject();

        // XmlPullParser xml = Xml.newPullParser();
        // xml.setInput(inputstream, "UTF-8");
        // int event = xml.getEventType();
        // while (event != XmlPullParser.END_DOCUMENT) {
        // switch (event) {
        // // 
        // case XmlPullParser.START_DOCUMENT:
        // list = new ArrayList<SysOrgan>();
        // break;
        // case XmlPullParser.START_TAG:
        //
        // String value = xml.getName();
        // if (value.equals("QY")) {
        // organ = new SysOrgan();
        // } else if (value.equals("SO_ID")) {
        // organ.setSoId(xml.nextText());
        // } else if (value.equals("so_Name")) {
        // organ.setSoName(xml.nextText());
        // }
        // break;
        // case XmlPullParser.END_TAG:
        // if (xml.getName().equals("QY")) {
        // list.add(organ);
        // organ = null;
        // }
        // break;
        // default:
        // break;
        // }
        // // 
        // event = xml.next();
        // }
        return list;
    } else {
        return null;
    }
}

From source file:net.feed_the_beast.launcher.json.EnumAdaptorFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   ww w . j  a va2  s .com*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (!type.getRawType().isEnum()) {
        return null;
    }
    final Map<String, T> map = Maps.newHashMap();
    for (T c : (T[]) type.getRawType().getEnumConstants()) {
        map.put(c.toString().toLowerCase(Locale.US), c);
    }

    return new TypeAdapter<T>() {
        @Override
        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            String name = reader.nextString();
            if (name == null) {
                return null;
            }
            return map.get(name.toLowerCase(Locale.US));
        }

        @Override
        public void write(JsonWriter writer, T value) throws IOException {
            if (value == null) {
                writer.nullValue();
            } else {
                writer.value(value.toString().toLowerCase(Locale.US));
            }
        }
    };
}

From source file:net.friendscraft_2_launch.launcher.json.EnumAdaptorFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*w  ww .  j a  v  a  2  s.c o m*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (!type.getRawType().isEnum())
        return null;
    final Map<String, T> map = new HashMap<String, T>();
    for (T c : (T[]) type.getRawType().getEnumConstants()) {
        map.put(c.toString().toLowerCase(Locale.US), c);
    }

    return new TypeAdapter<T>() {
        @Override
        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            String name = reader.nextString();
            if (name == null)
                return null;
            return map.get(name.toLowerCase(Locale.US));
        }

        @Override
        public void write(JsonWriter writer, T value) throws IOException {
            if (value == null) {
                writer.nullValue();
            } else {
                writer.value(value.toString().toLowerCase(Locale.US));
            }
        }
    };
}

From source file:net.minecrell.workbench.tools.util.gson.LowerCaseEnumTypeAdapterFactory.java

License:Open Source License

@Override
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 v  a 2 s  . c o  m*/

    final Map<String, T> lookup = new HashMap<>();
    for (T constant : rawType.getEnumConstants()) {
        lookup.put(toLowerCase(constant), constant);
    }

    return new TypeAdapter<T>() {

        @Override
        public void write(JsonWriter out, T value) throws IOException {
            if (value == null) {
                out.nullValue();
            } else {
                out.value(toLowerCase(value));
            }
        }

        @Override
        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            } else {
                return lookup.get(reader.nextString());
            }
        }
    };
}

From source file:net.oneandone.stool.configuration.adapter.FileNodeTypeAdapter.java

License:Apache License

@Override
public FileNode read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from w ww  .j  a  v a  2  s  .co m*/
    return world.file(in.nextString());
}

From source file:net.technicpack.launchercore.util.LowerCaseEnumTypeAdapterFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class rawType = type.getRawType();
    if (!rawType.isEnum()) {
        return null;
    }// w ww  .j a  v  a  2s . c o  m

    final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
    for (Object constant : rawType.getEnumConstants()) {
        lowercaseToConstant.put(toLowercase(constant), (T) constant);
    }

    return new TypeAdapter<T>() {
        public void write(JsonWriter out, T value) throws IOException {
            if (value == null)
                out.nullValue();
            else
                out.value(LowerCaseEnumTypeAdapterFactory.this.toLowercase(value));
        }

        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            return lowercaseToConstant.get(reader.nextString());
        }
    };
}

From source file:nz.ac.otago.psyanlab.common.model.util.LongSparseArrayGsonAdapter.java

License:Open Source License

@Override
public LongSparseArray<T> read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from w w w .j  a  v a 2 s  .c  o  m*/
    LongSparseArray<Object> temp = mGson.fromJson(in, mTypeOfLongSparseArrayOfObject);
    LongSparseArray<T> result = new LongSparseArray<T>(temp.size());
    long key;
    JsonElement tElement;
    for (int i = 0; i < temp.size(); i++) {
        key = temp.keyAt(i);
        tElement = mGson.toJsonTree(temp.get(key), new TypeToken<T>() {
        }.getType());
        result.put(key, mGson.fromJson(tElement, mTClazz));
    }
    return result;
}

From source file:one.talon.api.JSON.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:/*from  w  ww  . j a  v  a  2s.  c o  m*/
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        if (date.equals("0001-01-01T00:00:00Z")) {
            // explicitly convert to ISO8601 format date in case of empty "golang" string
            date = "0001-01-01T00:00:00.000+0000";
        }
        return formatter.parseDateTime(date);
    }
}