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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

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();/*  ww  w . ja  v  a2  s. co m*/
                } 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();/*from  ww  w.  j  av  a  2  s. c  o m*/
        return null;
    }

    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.bradipao.berengar.DbTool.java

License:Apache License

public static int gson2db(SQLiteDatabase mDB, File jsonFile) {

    // vars//from   www. ja va 2s  . com
    int iTableNum = 0;
    FileReader fr = null;
    BufferedReader br = null;
    JsonReader jr = null;
    String name = null;
    String val = null;

    String mTable = null;
    String mTableSql = null;
    ArrayList<String> aFields = null;
    ArrayList<String> aValues = null;
    ContentValues cv = null;

    // file readers
    try {
        fr = new FileReader(jsonFile);
        br = new BufferedReader(fr);
        jr = new JsonReader(br);
    } catch (FileNotFoundException e) {
        Log.e(LOGTAG, "error in gson2db file readers", e);
    }

    // parsing
    try {
        // start database transaction
        mDB.beginTransaction();
        // open root {
        jr.beginObject();
        // iterate through root objects
        while (jr.hasNext()) {
            name = jr.nextName();
            if (jr.peek() == JsonToken.NULL)
                jr.skipValue();
            // number of tables
            else if (name.equals("tables_num")) {
                val = jr.nextString();
                iTableNum = Integer.parseInt(val);
                if (GOLOG)
                    Log.d(LOGTAG, "TABLE NUM : " + iTableNum);
            }
            // iterate through tables array
            else if (name.equals("tables")) {
                jr.beginArray();
                while (jr.hasNext()) {
                    // start table
                    mTable = null;
                    aFields = null;
                    jr.beginObject();
                    while (jr.hasNext()) {
                        name = jr.nextName();
                        if (jr.peek() == JsonToken.NULL)
                            jr.skipValue();
                        // table name
                        else if (name.equals("table_name")) {
                            mTable = jr.nextString();
                        }
                        // table sql
                        else if (name.equals("table_sql")) {
                            mTableSql = jr.nextString();
                            if ((mTable != null) && (mTableSql != null)) {
                                mDB.execSQL("DROP TABLE IF EXISTS " + mTable);
                                mDB.execSQL(mTableSql);
                                if (GOLOG)
                                    Log.d(LOGTAG, "DROPPED AND CREATED TABLE : " + mTable);
                            }
                        }
                        // iterate through columns name
                        else if (name.equals("cols_name")) {
                            jr.beginArray();
                            while (jr.hasNext()) {
                                val = jr.nextString();
                                if (aFields == null)
                                    aFields = new ArrayList<String>();
                                aFields.add(val);
                            }
                            jr.endArray();
                            if (GOLOG)
                                Log.d(LOGTAG, "COLUMN NAME : " + aFields.toString());
                        }
                        // iterate through rows
                        else if (name.equals("rows")) {
                            jr.beginArray();
                            while (jr.hasNext()) {
                                jr.beginArray();
                                // iterate through values in row
                                aValues = null;
                                cv = null;
                                while (jr.hasNext()) {
                                    val = jr.nextString();
                                    if (aValues == null)
                                        aValues = new ArrayList<String>();
                                    aValues.add(val);
                                }
                                jr.endArray();
                                // add to database
                                cv = new ContentValues();
                                for (int j = 0; j < aFields.size(); j++)
                                    cv.put(aFields.get(j), aValues.get(j));
                                mDB.insert(mTable, null, cv);
                                if (GOLOG)
                                    Log.d(LOGTAG, "INSERT IN " + mTable + " : " + aValues.toString());
                            }
                            jr.endArray();
                        } else
                            jr.skipValue();
                    }
                    // end table
                    jr.endObject();
                }
                jr.endArray();
            } else
                jr.skipValue();
        }
        // close root }
        jr.endObject();
        jr.close();
        // successfull transaction
        mDB.setTransactionSuccessful();
    } catch (IOException e) {
        Log.e(LOGTAG, "error in gson2db gson parsing", e);
    } finally {
        mDB.endTransaction();
    }

    return iTableNum;
}

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();/*  w  ww .j a v a2  s . com*/
        return null;
    }
    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.bpiwowar.experimaestro.tasks.ClassChooserAdapter.java

License:Open Source License

@Override
public Object read(JsonReader in) throws IOException {
    // If string, use this
    if (in.peek() == JsonToken.STRING) {
        final String type = in.nextString();
        final Class<?> aClass = types.get(type);
        if (aClass == null) {
            throw new JsonParseException("No type " + type + " defined");
        }/*from   w  w  w . ja  v  a 2  s .c o  m*/
        return gson.fromJson(new JsonObject(), aClass);
    }

    // Get the Json object
    final JsonObject json;
    if (in instanceof JsonTreeReader) {
        json = ((JsonTreeReader) in).getJsonObject();
    } else {
        json = new JsonParser().parse(in).getAsJsonObject();
    }

    // Get the type
    final JsonElement _type = json.get("type");
    if (_type == null) {
        throw new JsonParseException("No type defined");
    }
    String type = _type.getAsString();

    // Get the class of the object to create
    final Class<?> aClass = types.get(type);
    if (aClass == null) {
        throw new JsonParseException("No type " + type + " defined");
    }

    return gson.fromJson(json, aClass);
}

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  v  a 2  s .  c  o 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 {/*w w w . ja  v  a2  s. com*/
        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());
                }
            }
        };
    }
}

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();// w  ww .  j ava  2  s  .c o  m
        return null;
    }
    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();// ww  w. j a va 2 s  . c om
        return null;
    }
    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);//  w w w .j a  v  a  2 s. c  o m
    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;
    }
}