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:org.terasology.utilities.gson.CaseInsensitiveEnumTypeAdapterFactory.java

License:Apache License

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

    final Map<String, T> lowercaseToConstant = Maps.newHashMap();
    for (T constant : rawType.getEnumConstants()) {
        String lowercase = toLowercase(constant);
        lowercaseToConstant.put(lowercase, 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 {
                String value = reader.nextString();
                return lowercaseToConstant.get(toLowercase(value));
            }
        }
    };
}

From source file:org.terasology.utilities.gson.UriTypeAdapterFactory.java

License:Apache License

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!Uri.class.isAssignableFrom(rawType)) {
        return null;
    }/* w ww . j a  v  a2  s  .  com*/

    final Constructor<T> constructor;
    try {
        constructor = rawType.getConstructor(String.class);
        constructor.setAccessible(true);
    } catch (NoSuchMethodException e) {
        logger.error("URI type {} lacks String constructor", rawType);
        return null;
    }

    return new TypeAdapter<T>() {
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            out.value(value.toString());
        }

        @Override
        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            } else {
                String nextString = reader.nextString();
                try {
                    return constructor.newInstance(nextString);
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                    logger.error("Failed to instantiate uri of type {} from value {}", rawType, nextString, e);
                    return null;
                }
            }
        }
    };
}

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

License:Open Source License

private boolean parseArticle(final Article a, final JsonReader reader,
        final Set<Article.ArticleField> skipNames, final IArticleOmitter filter) throws IOException {

    boolean skipObject = false;
    while (reader.hasNext() && reader.peek().equals(JsonToken.NAME)) {
        if (skipObject) {
            // field name
            reader.skipValue();//from  w  ww . j  a  v  a2 s  .c  o m
            // field value
            reader.skipValue();
            continue;
        }

        String name = reader.nextName();
        Article.ArticleField field = Article.ArticleField.valueOf(name);

        try {

            if (skipNames != null && skipNames.contains(field)) {
                reader.skipValue();
                continue;
            }

            switch (field) {
            case id:
                a.id = reader.nextInt();
                break;
            case title:
                a.title = reader.nextString();
                break;
            case unread:
                a.isUnread = reader.nextBoolean();
                break;
            case updated:
                a.updated = new Date(reader.nextLong() * 1000);
                break;
            case feed_id:
                if (reader.peek() == JsonToken.NULL)
                    reader.nextNull();
                else
                    a.feedId = reader.nextInt();
                break;
            case content:
                a.content = reader.nextString();
                break;
            case link:
                a.url = reader.nextString();
                break;
            case comments:
                a.commentUrl = reader.nextString();
                break;
            case attachments:
                a.attachments = parseAttachments(reader);
                break;
            case marked:
                a.isStarred = reader.nextBoolean();
                break;
            case published:
                a.isPublished = reader.nextBoolean();
                break;
            case labels:
                a.labels = parseLabels(reader);
                break;
            case author:
                a.author = reader.nextString();
                break;
            default:
                reader.skipValue();
                continue;
            }

            if (filter != null)
                skipObject = filter.omitArticle(field, a);

        } catch (IllegalArgumentException | StopJsonParsingException | IOException e) {
            Log.w(TAG, "Result contained illegal value for entry \"" + field + "\".");
            reader.skipValue();
        }
    }
    return skipObject;
}

From source file:pl.softech.gw.json.TaskGsonTypeAdapter.java

License:Apache License

@Override
public ITask read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }/*w w  w.  j av a 2s . c  o m*/

    reader.beginObject();

    TaskInstanceCreator instanceCreator = new TaskInstanceCreator();
    while (reader.peek() != JsonToken.END_OBJECT) {

        String name = reader.nextName();
        Object value;
        if (reader.peek() == JsonToken.BEGIN_OBJECT && !name.equals("module")) {
            value = read(reader);
        } else {
            if (name.equals("module")) {
                value = gson.getAdapter(ProjectModule.class).read(reader);
            } else {
                value = reader.nextString();
            }
        }

        if (name.equals("class")) {
            instanceCreator.className = value.toString();
        } else {
            instanceCreator.params.put(name, new Param(name, value));
        }

    }

    reader.endObject();
    return instanceCreator.create();
}

From source file:ro.cosu.vampires.server.util.gson.LowercaseEnumTypeAdapterFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<T> rawType = (Class<T>) type.getRawType();
    if (!rawType.isEnum()) {
        return null;
    }// www . ja va2  s .com

    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:sf.net.experimaestro.manager.json.JsonConverter.java

License:Open Source License

private Json readNext(JsonReader jsonReader) throws IOException {
    final JsonToken token = jsonReader.peek();
    switch (token) {
    case BEGIN_ARRAY: {
        jsonReader.beginArray();//  www.java 2s . c  o  m
        JsonArray array = new JsonArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            array.add(readNext(jsonReader));
        }
        jsonReader.endArray();
        return array;
    }

    case BEGIN_OBJECT: {
        jsonReader.beginObject();
        JsonObject object = new JsonObject();
        while (jsonReader.peek() != JsonToken.END_OBJECT) {
            final String name = jsonReader.nextName();
            final Json value = readNext(jsonReader);
            object.put(name, value);
        }
        jsonReader.endObject();
        return object;
    }

    case BOOLEAN:
        return new JsonBoolean(jsonReader.nextBoolean());

    case STRING:
        return new JsonString(jsonReader.nextString());

    case NULL: {
        jsonReader.nextNull();
        return JsonNull.getSingleton();
    }

    case NUMBER:
        return new JsonReal(jsonReader.nextDouble());

    default:
        throw new RuntimeException("Cannot handle GSON token type: " + token.name());

    }
}

From source file:TestGenerator.ArgumentCache.UniversalArrayTypeAdapter.java

License:Apache License

@Override
public Object read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from  w  w  w.j a  va2 s  . co m*/

    List<E> list = new ArrayList<E>();
    in.beginArray();
    while (in.hasNext()) {
        //            E instance = componentTypeAdapter.read(in); // old code replacing below block
        E instance = null;
        JsonElement je = this.jsonElementAdapter.read(in);
        if (je.isJsonObject() && je.getAsJsonObject().has(UniversalTypeAdapterFactory.class_token)) {
            try {
                Class clazz = Class.forName(
                        je.getAsJsonObject().get(UniversalTypeAdapterFactory.class_token).getAsString());
                TypeAdapter newAdapter = context.getAdapter(clazz);
                instance = (E) newAdapter.fromJsonTree(je);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            instance = componentTypeAdapter.fromJsonTree(je);
        }
        list.add(instance);
    }
    in.endArray();
    Object array = Array.newInstance(componentType, list.size());
    for (int i = 0; i < list.size(); i++) {
        Array.set(array, i, list.get(i));
    }

    return array;
}

From source file:uk.ac.stfc.isis.ibex.epics.conversion.json.LowercaseEnumTypeAdapterFactory.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  ww.  jav a2  s . c  o  m*/
 */
@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;
    }

    final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
    for (T constant : rawType.getEnumConstants()) {
        lowercaseToConstant.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 lowercaseToConstant.get(reader.nextString());
            }
        }
    };
}

From source file:uk.co.visalia.brightpearl.apiclient.client.adaptors.CalendarAdaptor.java

License:Apache License

@Override
public Calendar read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
        return null;
    }//from  ww  w  .  ja v a  2  s .c  o m
    String string = jsonReader.nextString();
    if (StringUtils.isNotBlank(string)) {
        if (storedInitException != null) {
            throw new JsonParseException("Could not parse '" + string + "' as an ISO date.",
                    storedInitException);
        }
        try {
            return datatypeFactory.newXMLGregorianCalendar(string).toGregorianCalendar();
        } catch (Exception e) {
            throw new JsonParseException("Could not parse '" + string + "' as an ISO date.", e);
        }
    }
    return null;
}

From source file:uk.co.visalia.brightpearl.apiclient.client.adaptors.DateTimeAdaptor.java

License:Apache License

@Override
public DateTime read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
        return null;
    }//from ww  w.ja  v a  2  s.  c  o m
    String string = jsonReader.nextString();
    return ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(string);
}