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.gw2InfoViewer.services.json.typeadaptors.EventAdapter.java

License:Open Source License

@Override
public Event read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }//from   w ww.j av a2 s  .  c om

    Event event = new Event();

    reader.beginObject();
    reader.nextName(); //world id
    event.setWorldId(reader.nextInt());
    reader.nextName(); //map id
    event.setMapId(reader.nextInt());
    reader.nextName(); //event id
    event.setEventId(reader.nextString());
    reader.nextName();
    event.setState(EventState.valueOf(reader.nextString())); //state
    reader.endObject();

    return event;
}

From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchFormatJsonAdapter.java

License:LGPL

@Override
public List<String> read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/* www  . jav a  2 s.c o m*/

    String joinedFormats = in.nextString();
    List<String> formats = Arrays.asList(joinedFormats.split(FORMAT_SEPARATOR_REGEX));
    return formats;
}

From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchRoutingTypeJsonAdapter.java

License:LGPL

@Override
public RoutingType read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }//from  w w w. ja v a  2s. c o  m

    RoutingType value = null;
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "required":
            if (in.nextBoolean()) {
                value = RoutingType.REQUIRED;
            } else {
                value = RoutingType.OPTIONAL;
            }
            break;
        default:
            throw new AssertionFailure(
                    "Unexpected property for attribute of type " + RoutingType.class + ": " + name);
        }
    }

    return value;
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractExtraPropertiesJsonAdapter.java

License:LGPL

@Override
public T read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }// w ww. j a v a  2 s .c  om

    T instance = createInstance();
    try {
        in.beginObject();
        while (in.hasNext()) {
            String name = in.nextName();
            FieldAdapter<? super T> fieldAdapter = fieldAdapters.get(name);
            if (fieldAdapter == null) {
                extraPropertyAdapter.readOne(in, name, instance);
            } else {
                fieldAdapter.read(in, instance);
            }
        }
        in.endObject();
    } catch (IllegalStateException e) {
        throw new JsonSyntaxException(e);
    }

    return instance;
}

From source file:org.jclouds.http.functions.ParseFirstJsonValueNamed.java

License:Apache License

private JsonToken skipAndPeek(JsonToken token, JsonReader reader) throws IOException {
    switch (token) {
    case BEGIN_ARRAY:
        reader.beginArray();/*ww  w .  ja  v a  2 s  .  c  o m*/
        break;
    case END_ARRAY:
        reader.endArray();
        break;
    case BEGIN_OBJECT:
        reader.beginObject();
        break;
    case END_OBJECT:
        reader.endObject();
        break;
    case NAME:
        // NOTE that we have already advanced on NAME in the eval block;
        break;
    case STRING:
        reader.nextString();
        break;
    case NUMBER:
        reader.nextString();
        break;
    case BOOLEAN:
        reader.nextBoolean();
        break;
    case NULL:
        reader.nextNull();
        break;
    case END_DOCUMENT:
        break;
    }
    return reader.peek();
}

From source file:org.jclouds.json.internal.OptionalTypeAdapterFactory.java

License:Apache License

protected <E> TypeAdapter<Optional<E>> newOptionalAdapter(final TypeAdapter<E> elementAdapter) {
    return new TypeAdapter<Optional<E>>() {
        public void write(JsonWriter out, Optional<E> value) throws IOException {
            if (!value.isPresent()) {
                out.nullValue();//from w  w w  . j ava  2s . co m
                return;
            }
            elementAdapter.write(out, value.get());
        }

        public Optional<E> read(JsonReader in) throws IOException {
            Optional<E> result = Optional.absent();
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
            } else {
                E element = elementAdapter.read(in);
                if (element != null) {
                    result = Optional.of(element);
                }
            }
            return result;
        }
    };
}

From source file:org.jsfr.json.GsonParser.java

License:Open Source License

private ResumableParser createResumableParserImpl(Reader reader, SurfingContext context) {

    final JsonReader jsonReader = this.jsonReaderFactory.createJsonReader(reader);
    final JsonProvider jsonProvider = context.getConfig().getJsonProvider();

    AbstractPrimitiveHolder stringHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override/*from  ww w  .j  av a 2 s . c o m*/
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextString());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder numberHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextDouble());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder booleanHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextBoolean());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder nullHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            jsonReader.nextNull();
            return jsonProvider.primitiveNull();
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    return new GsonResumableParser(jsonReader, context, stringHolder, numberHolder, booleanHolder, nullHolder);
}

From source file:org.komodo.rest.json.RestPropertySerializer.java

License:Open Source License

@Override
public RestProperty read(JsonReader in) throws IOException {

    String propName = null;//from   ww  w .j av  a  2  s .c om
    Object propValue = null;

    in.beginObject();

    while (in.hasNext()) {
        String name = in.nextName();
        if (RestProperty.NAME_LABEL.equals(name))
            propName = in.nextString();
        else if (RestProperty.VALUE_LABEL.equals(name)) {
            JsonToken token = in.peek();
            switch (token) {
            case BOOLEAN:
                propValue = in.nextBoolean();
                break;
            case NUMBER: {
                double value = in.nextDouble();
                if (value % 1 == 0)
                    propValue = (int) value;
                else
                    propValue = value;
                break;
            }
            case STRING:
                propValue = in.nextString();
                break;
            case NULL:
                in.nextNull();
                propValue = null;
                break;
            case BEGIN_ARRAY:
                final Object[] value = BUILDER.fromJson(in, Object[].class);

                //
                // BUILDER always converts json numbers to double regardless
                // of them being integers so need to do some checking and on-the-fly
                // conversion
                //
                for (int i = 0; i < value.length; ++i) {
                    if (value[i] instanceof Double && ((double) value[i] % 1) == 0)
                        value[i] = ((Double) value[i]).intValue();
                }

                propValue = value;
                break;
            default:
                throw new IOException(Messages.getString(Messages.Error.UNEXPECTED_JSON_TOKEN, name));
            }
        }
    }

    in.endObject();

    RestProperty property = new RestProperty(propName, propValue);
    if (!isComplete(property))
        throw new IOException(
                Messages.getString(Messages.Error.INCOMPLETE_JSON, RestProperty.class.getSimpleName()));

    return property;
}

From source file:org.komodo.rest.relational.json.BasicEntitySerializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w w w .  j  av  a 2s  .c  o m
 *
 * @see org.komodo.rest.relational.json.AbstractEntitySerializer#read(com.google.gson.stream.JsonReader)
 */
@Override
public T read(final JsonReader in) throws IOException {
    final T entity = createEntity();

    beginRead(in);

    while (in.hasNext()) {
        final String name = in.nextName();

        if (readExtension(name, entity, in) != null)
            continue;

        if (PROPERTIES.equals(name))
            readProperties(in, entity);
        else if (LINKS.equals(name))
            readLinks(in, entity);
        else {
            JsonToken token = in.peek();
            switch (token) {
            case BOOLEAN:
                entity.addTuple(name, in.nextBoolean());
                break;
            case NUMBER: {
                double value = in.nextDouble();
                if (value % 1 == 0)
                    entity.addTuple(name, (int) value);
                else
                    entity.addTuple(name, value);
                break;
            }
            case STRING:
                entity.addTuple(name, in.nextString());
                break;
            case NULL:
                in.nextNull();
                entity.addTuple(name, null);
                break;
            case BEGIN_ARRAY:
                final Object[] value = BUILDER.fromJson(in, Object[].class);

                //
                // BUILDER always converts json numbers to double regardless
                // of them being integers so need to do some checking and on-the-fly
                // conversion
                //
                for (int i = 0; i < value.length; ++i) {
                    if (value[i] instanceof Double && ((double) value[i] % 1) == 0)
                        value[i] = ((Double) value[i]).intValue();
                }

                entity.addTuple(name, value);
                break;
            default:
                throw new IOException(Messages.getString(Messages.Error.UNEXPECTED_JSON_TOKEN, name));
            }
        }
    }

    if (!isComplete(entity)) {
        throw new IOException(Messages.getString(INCOMPLETE_JSON, getClass().getSimpleName()));
    }

    endRead(in);
    return entity;
}

From source file:org.komodo.rest.relational.json.TeiidVdbStatusSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/* w ww  .j  a v  a  2  s .  co m*/
 *
 * @see org.komodo.rest.relational.json.AbstractEntitySerializer#read(com.google.gson.stream.JsonReader)
 */
@Override
public RestTeiidVdbStatus read(final JsonReader in) throws IOException {
    final RestTeiidVdbStatus entity = createEntity();

    beginRead(in);

    while (in.hasNext()) {
        final String name = in.nextName();

        if (RestTeiidVdbStatus.VDBS_LABEL.equals(name)) {
            final RestTeiidVdbStatusVdb[] vdbStatuses = BUILDER.fromJson(in, RestTeiidVdbStatusVdb[].class);
            entity.setVdbProperties(Arrays.asList(vdbStatuses));
        } else if (LINKS.equals(name))
            readLinks(in, entity);
        else {
            JsonToken token = in.peek();
            switch (token) {
            case BOOLEAN:
                entity.addTuple(name, in.nextBoolean());
                break;
            case NUMBER:
                entity.addTuple(name, in.nextInt());
                break;
            case STRING:
                entity.addTuple(name, in.nextString());
                break;
            case NULL:
                in.nextNull();
                entity.addTuple(name, null);
                break;
            case BEGIN_ARRAY:
                final String[] value = BUILDER.fromJson(in, String[].class);
                entity.addTuple(name, value);
                break;
            default:
                throw new IOException(Messages.getString(Messages.Error.UNEXPECTED_JSON_TOKEN, name));
            }
        }
    }

    if (!isComplete(entity)) {
        throw new IOException(Messages.getString(INCOMPLETE_JSON, getClass().getSimpleName()));
    }

    endRead(in);
    return entity;
}