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

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

Introduction

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

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, 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();/*from w w w .ja v  a 2 s  . c  o 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  www.j  ava 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 . j  av  a 2s  .c om
    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:jp.yokomark.utils.gson.adapter.EnumTypeAdapterFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w ww.j a  v a2  s . c  o m*/
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<? super T> rawType = type.getRawType();
    if (!rawType.isEnum()) { // not an enum type, so do not create any of adapter.
        return null;
    }

    final Map<String, T> jsonNameToInstance = new HashMap<String, T>();
    for (T constant : (T[]) rawType.getEnumConstants()) {
        jsonNameToInstance.put(constantNameToJsonName(((Enum<?>) constant).name()), constant);
    }

    return (new TypeAdapter<T>() {

        @Override
        public T read(final JsonReader reader) throws IOException {
            final String constValue = reader.nextString();
            return jsonNameToInstance.get(constValue);
        }

        @Override
        public void write(final JsonWriter writer, T constant) throws IOException {
            writer.value(constantNameToJsonName(((Enum<?>) constant).name()));
        }
    }).nullSafe();
}

From source file:json_export_import.GSON_Observer.java

public void read() {
    try {//from   w ww .  j a va 2  s.c o m
        JsonReader reader = new JsonReader(new FileReader("/home/rgreim/Output.json"));
        reader.beginArray();
        while (reader.hasNext()) {
            String sequence = reader.nextString();
            System.out.println("Sequenz: " + sequence);
        }
        reader.endArray();
        reader.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(GSON_Observer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GSON_Observer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:me.ixfan.wechatkit.message.out.json.MassMessageGsonTypeAdapter.java

License:Open Source License

@Override
public MessageForMassSend read(JsonReader in) throws IOException {
    MessageForMassSend.Filter filter = null;
    List<String> toUser = null;
    String msgType = null;//from   ww  w.j  a v a2  s.  c o  m
    String msgContent = null;

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "msgtype":
            msgType = in.nextString();
            break;
        case "filter":
            in.beginObject();
            String tagId = null;
            boolean isToAll = false;
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "is_to_all":
                    isToAll = in.nextBoolean();
                    break;
                case "tag_id":
                    tagId = in.nextString();
                    break;
                default:
                    break;
                }
            }
            in.endObject();
            filter = new MessageForMassSend.Filter(tagId, isToAll);
            break;
        case "touser":
            in.beginArray();
            toUser = new ArrayList<>();
            while (in.hasNext()) {
                toUser.add(in.nextString());
            }
            in.endArray();
            break;
        case "text":
        case "image":
        case "voice":
        case "mpnews":
        case "mpvideo":
        case "wxcard":
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "content":
                case "media_id":
                case "card_id":
                    msgContent = in.nextString();
                    break;
                default:
                    break;
                }
            }
            in.endObject();
            break;
        default:
            break;
        }
    }

    in.endObject();

    if (null != filter) {
        return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, filter.getTagId(),
                filter.isToAll());
    } else if (null != toUser) {
        return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, toUser);
    }
    return null;
}

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 ww  w .j a 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.bpiwowar.experimaestro.tasks.FileAdapter.java

License:Open Source License

@Override
public File read(JsonReader in) throws IOException {
    final String s = in.nextString();

    if (s.startsWith(FILE_PROTOCOL)) {
        return new File(s.substring(FILE_PROTOCOL.length()));
    }//from www  .  j av a  2  s  .  c o m

    return new File(s);
}

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;
    }//ww  w.  j  a v  a  2s  . 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 {/*from ww w . ja v a2 s  .co  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());
                }
            }
        };
    }
}