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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:org.sensorhub.impl.security.gxoauth.OAuthAuthenticator.java

License:Mozilla Public License

private String parseUserInfoJson(JsonReader reader) throws IOException {
    String userId = null;//www  .  j a v a  2s.co m

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if ("id".equals(name) || "user_id".equals(name) || "uid".equals(name))
            userId = reader.nextString();
        else
            reader.skipValue();
    }
    reader.endObject();

    return userId;
}

From source file:org.smartparam.manager.json.vendor.gson.ParameterDiffSerializer.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
    if (ParameterDiff.class.isAssignableFrom(type.getRawType())) {
        return (TypeAdapter) new TypeAdapter<ParameterDiff>() {

            @Override//w  ww  .j av a  2  s.  c om
            public void write(JsonWriter out, ParameterDiff value) throws IOException {
                out.beginObject().name("previous").value(gson.toJson(value.previous())).name("current")
                        .value(gson.toJson(value.current())).endObject();
            }

            @Override
            public ParameterDiff read(JsonReader in) throws IOException {
                in.beginObject();
                in.nextName();
                SimpleParameter previous = gson.fromJson(in, SimpleParameter.class);
                in.nextName();
                SimpleParameter current = gson.fromJson(in, SimpleParameter.class);
                in.endObject();

                return new ParameterDiff(previous, current);
            }
        };
    }
    return null;
}

From source file:org.smartparam.manager.json.vendor.gson.ParameterEntryDiffSerializer.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
    if (ParameterEntryDiff.class.isAssignableFrom(type.getRawType())) {
        return (TypeAdapter) new TypeAdapter<ParameterEntryDiff>() {

            @Override/* w w  w  .  j a  va2  s.c o  m*/
            public void write(JsonWriter out, ParameterEntryDiff value) throws IOException {
                out.beginObject().name("previous").value(gson.toJson(value.previous())).name("current")
                        .value(gson.toJson(value.current())).endObject();
            }

            @Override
            public ParameterEntryDiff read(JsonReader in) throws IOException {
                in.beginObject();
                in.nextName();
                SimpleParameterEntry previous = gson.fromJson(in, SimpleParameterEntry.class);
                in.nextName();
                SimpleParameterEntry current = gson.fromJson(in, SimpleParameterEntry.class);
                in.endObject();

                return new ParameterEntryDiff(previous, current);
            }
        };
    }
    return null;
}

From source file:org.sonarsource.sonarlint.core.container.connected.update.SettingsDownloader.java

License:Open Source License

private static void parseProperty(BiPredicate<String, String> filter, BiConsumer<String, String> consumer,
        JsonReader reader) throws IOException {
    String key = null;//from w w w.j a  v  a2s.  c om
    String value = null;
    while (reader.hasNext()) {
        String propName = reader.nextName();
        switch (propName) {
        case "key":
            key = reader.nextString();
            break;
        case "value":
            value = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }
    // Storage optimisation: don't store properties having same value than global properties
    if (filter.test(key, value)) {
        consumer.accept(key, value);
    }
}

From source file:org.spongepowered.plugin.meta.gson.ModMetadataAdapter.java

License:MIT License

@Override
public PluginMetadata read(JsonReader in) throws IOException {
    in.beginObject();//from   ww  w .  j  a  v a 2s . c  o m

    final Set<String> processedKeys = new HashSet<>();

    final PluginMetadata result = new PluginMetadata("unknown");
    String id = null;

    while (in.hasNext()) {
        final String name = in.nextName();
        if (!processedKeys.add(name)) {
            throw new JsonParseException("Duplicate key '" + name + "' in " + in);
        }

        switch (name) {
        case "modid":
            id = in.nextString();
            result.setId(id);
            break;
        case "name":
            result.setName(in.nextString());
            break;
        case "version":
            result.setVersion(in.nextString());
            break;
        case "description":
            result.setDescription(in.nextString());
            break;
        case "url":
            result.setUrl(in.nextString());
            break;
        case "authorList":
            in.beginArray();
            while (in.hasNext()) {
                result.addAuthor(in.nextString());
            }
            in.endArray();
            break;
        case "requiredMods":
            in.beginArray();
            while (in.hasNext()) {
                result.addRequiredDependency(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        case "dependencies":
            in.beginArray();
            while (in.hasNext()) {
                result.loadAfter(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        case "dependants":
            in.beginArray();
            while (in.hasNext()) {
                result.loadBefore(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        default:
            result.setExtension(name, this.gson.fromJson(in, getExtension(name)));
        }
    }

    in.endObject();

    if (id == null) {
        throw new JsonParseException("Mod metadata is missing required element 'modid'");
    }

    return result;
}

From source file:org.sprintapi.hyperdata.gson.HyperDataTypeAdapter.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//w w w .j a v  a2 s  .  c  o m
public Object read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    Object instance = constructor.construct();

    BoundField metadataField = null;
    if (metadataAccess != null) {
        metadataField = boundFields.get(metadataAccess.fieldName);
    }

    Object meta = null;
    Map<String, BoundField> metaBoundFields = null;

    try {
        in.beginObject();
        while (in.hasNext()) {
            String name = in.nextName();
            if ((name != null) && (metadataField != null) && (name.startsWith(Constants.META_CHAR))) {
                if (meta == null) {
                    meta = constructorConstructor.get(metadataField.type).construct();
                    if (!Map.class.isAssignableFrom(meta.getClass())) {
                        metaBoundFields = reflectiveFactory.getBoundFields(gson, metadataField.type,
                                metadataField.type.getRawType());
                    }
                }

                if (metaBoundFields != null) {
                    BoundField field = metaBoundFields.get(name.substring(1));
                    if (field == null || !field.deserialized) {
                        in.skipValue();
                    } else {
                        field.read(in, meta);
                    }
                } else {
                    TypeAdapter<Object> ta = gson.getAdapter(Object.class);
                    Object value = ta.read(in);
                    ((Map) meta).put(name.substring(1), value);
                }

            } else if ((name != null) && (!name.equals(metadataAccess.fieldName))) {
                BoundField field = boundFields.get(name);
                if (field == null || !field.deserialized) {
                    in.skipValue();
                } else {
                    field.read(in, instance);
                }
            }
        }
        if (metadataAccess.setter != null) {
            metadataAccess.setter.invoke(instance, meta);
            //TODO
        }

    } catch (IllegalStateException e) {
        throw new JsonSyntaxException(e);
    } catch (IllegalAccessException e) {
        throw new AssertionError(e);
    } catch (IllegalArgumentException e) {
        throw new AssertionError(e);
    } catch (InvocationTargetException e) {
        throw new AssertionError(e);
    }
    in.endObject();
    return instance;
}

From source file:org.terasology.logic.behavior.asset.BehaviorTreeFormat.java

License:Apache License

private String nextName(JsonReader in, String expectedName) throws IOException {
    String name = in.nextName();
    if (!expectedName.equals(name)) {
        throw new RuntimeException(expectedName + " expected!");
    }// ww w .  j a va  2 s .  c om
    return name;
}

From source file:org.translator.java.AppInventorProperties.java

License:Apache License

protected AppInventorProperties(JsonReader reader) throws IOException {
    reader.beginObject();/*w w  w . ja  va 2s  .  com*/

    while (reader.peek() == JsonToken.NAME) {
        String name = reader.nextName();
        if (name.equals("$Components")) {
            reader.beginArray();

            while (reader.peek() == JsonToken.BEGIN_OBJECT)
                components.add(new AppInventorProperties(reader));

            reader.endArray();
        } else {
            String n = reader.nextString();
            if (n.matches("True|False"))
                n = n.toLowerCase();
            properties.put(name, n);
        }
    }

    reader.endObject();
}

From source file:org.translator.java.AppInventorScreen.java

License:Apache License

private void parseComponentJSON(JsonReader reader) throws IOException {
    reader.beginObject();/*w w w  .  j av  a  2 s  .c  o m*/

    while (reader.peek() == JsonToken.NAME) {
        String name = reader.nextName();
        if (name.equals("Properties"))
            form = new AppInventorProperties(reader);
        else {
            String d = reader.nextString();
            if (d.matches("True|False"))
                d = d.toLowerCase();
            data.put(name, d);
        }
    }

    reader.endObject();
}

From source file:org.ttrssreader.net.JSONConnector.java

License:Open Source License

private String readResult(Map<String, String> params, boolean login, boolean retry) throws IOException {
    InputStream in = doRequest(params);
    if (in == null)
        return null;

    JsonReader reader = null;
    String ret = "";
    try {/*from  w ww  .j a  v a  2  s  .co m*/
        reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        // Check if content contains array or object, array indicates login-response or error, object is content

        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (!name.equals("content")) {
                reader.skipValue();
                continue;
            }

            JsonToken t = reader.peek();
            if (!t.equals(JsonToken.BEGIN_OBJECT))
                continue;

            JsonObject object = new JsonObject();
            reader.beginObject();
            while (reader.hasNext()) {
                object.addProperty(reader.nextName(), reader.nextString());
            }
            reader.endObject();

            if (object.get(SESSION_ID) != null) {
                ret = object.get(SESSION_ID).getAsString();
            }
            if (object.get(STATUS) != null) {
                ret = object.get(STATUS).getAsString();
            }
            if (this.apiLevel == -1 && object.get(API_LEVEL) != null) {
                this.apiLevel = object.get(API_LEVEL).getAsInt();
            }
            if (object.get(VALUE) != null) {
                ret = object.get(VALUE).getAsString();
            }
            if (object.get(ERROR) != null) {
                String message = object.get(ERROR).getAsString();
                Context ctx = MyApplication.context();

                switch (message) {
                case API_DISABLED:
                    lastError = ctx.getString(R.string.Error_ApiDisabled, Controller.getInstance().username());
                    break;
                case NOT_LOGGED_IN:
                case LOGIN_ERROR:
                    if (!login && retry && login())
                        return readResult(params, false, false); // Just do the same request again
                    else
                        lastError = ctx.getString(R.string.Error_LoginFailed);
                    break;
                case INCORRECT_USAGE:
                    lastError = ctx.getString(R.string.Error_ApiIncorrectUsage);
                    break;
                case UNKNOWN_METHOD:
                    lastError = ctx.getString(R.string.Error_ApiUnknownMethod);
                    break;
                default:
                    lastError = ctx.getString(R.string.Error_ApiUnknownError);
                    break;
                }

                hasLastError = true;
                Log.e(TAG, message);
                return null;
            }

        }
    } finally {
        if (reader != null)
            reader.close();
    }
    if (ret.startsWith("\""))
        ret = ret.substring(1, ret.length());
    if (ret.endsWith("\""))
        ret = ret.substring(0, ret.length() - 1);

    return ret;
}