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:com.gilecode.yagson.adapters.TypeAdvisableComplexTypeAdapter.java

License:Apache License

public T read(JsonReader in, ReadContext ctx) throws IOException {
    JsonToken nextToken = in.peek();//from w  w w  . j  a v a 2s  .c  o m

    if (nextToken == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else if (nextToken == JsonToken.STRING) {
        // for complex type adapters, each string is a reference, no isReferenceString() match required
        String reference = in.nextString();
        T referenced = ctx.refsContext().getReferencedObject(reference);
        return referenced;
    }

    return readOptionallyAdvisedInstance(in, ctx);
}

From source file:com.gilecode.yagson.types.TypeUtils.java

License:Apache License

private static Type readTypeAdviceAfterTypeField(JsonReader in) throws IOException {
    // Check whether next tokens are type advise, fail if not
    String advisedTypeStr = in.nextString();
    String advisedClassStr = advisedTypeStr;

    int i = advisedTypeStr.indexOf('<');
    if (i >= 0) {
        if (!advisedTypeStr.endsWith(">")) {
            throw new JsonSyntaxException("Incorrect advised type: '" + advisedTypeStr + "'");
        }/*from w w w .j  a v  a 2s.c o  m*/
        advisedClassStr = advisedTypeStr.substring(0, i).trim();

        String parametersStr = advisedTypeStr.substring(i + 1, advisedTypeStr.length() - 1).trim();
        String[] parameters = parametersStr.split(",");
        Type[] parameterTypes = new Type[parameters.length];
        boolean hasParameterTypes = false;
        for (int j = 0; j < parameters.length; j++) {
            Type type = toType(parameters[j].trim());
            parameterTypes[j] = type;
            if (type != unknownType) {
                hasParameterTypes = true;
            }
        }

        if (hasParameterTypes) {
            Class advisedClass = (Class) toType(advisedClassStr);
            return $Gson$Types.newParameterizedTypeWithOwner(advisedClass.getEnclosingClass(), advisedClass,
                    parameterTypes);
        }
    }
    return toType(advisedClassStr);
}

From source file:com.github.gv2011.jsong.JsongAdapter.java

License:Open Source License

private JsonNode deserialize(final JsonFactoryImp jf, final JsonReader in) {
    return call(() -> {
        switch (in.peek()) {
        case STRING:
            return jf.primitive(in.nextString());
        case NUMBER:
            return jf.primitive(new BigDecimal(in.nextString()));
        case BOOLEAN:
            return jf.primitive(in.nextBoolean());
        case NULL:
            in.nextNull();//w  w w.j  a v a2s.c o  m
            return jf.jsonNull();
        case BEGIN_ARRAY:
            in.beginArray();
            final JsonList list = XStream.fromIterator(new It(jf, in)).collect(jf.toJsonList());
            in.endArray();
            return list;
        case BEGIN_OBJECT:
            in.beginObject();
            final JsonObject obj = XStream.fromIterator(new Itm(jf, in)).collect(jf.toJsonObject());
            in.endObject();
            return obj;
        case NAME:
        case END_DOCUMENT:
        case END_OBJECT:
        case END_ARRAY:
        default:
            throw new IllegalArgumentException();
        }
    });
}

From source file:com.github.kevinsawicki.halligan.Resource.java

License:Open Source License

/**
 * Parse resource property/*from   ww  w.  jav  a2s. co  m*/
 *
 * @param reader
 * @param name
 * @throws IOException
 */
protected void parseProperty(final JsonReader reader, final String name) throws IOException {
    JsonToken next = reader.peek();
    switch (next) {
    case BEGIN_OBJECT:
        properties.put(name, gson.getGson().fromJson(reader, Map.class));
        break;
    case STRING:
        properties.put(name, reader.nextString());
        break;
    case NUMBER:
        properties.put(name, reader.nextDouble());
        break;
    case NULL:
        properties.put(name, null);
        reader.nextNull();
        break;
    case BOOLEAN:
        properties.put(name, reader.nextBoolean());
        break;
    default:
        throw new IOException("Unrecognized property value token: " + next);
    }
}

From source file:com.github.lindenb.gatkui.Json2Xml.java

License:Open Source License

private void parse(String label, JsonReader r) throws Exception {
    if (!r.hasNext())
        return;/*from  w w  w .  ja v a 2s. co m*/
    JsonToken token = r.peek();
    switch (token) {
    case NAME:
        break;
    case BEGIN_OBJECT: {
        r.beginObject();
        parseObject(label, r);
        break;
    }
    case END_OBJECT: {
        break;
    }
    case BEGIN_ARRAY: {
        r.beginArray();
        parseArray(label, r);
        break;
    }
    case END_ARRAY: {
        break;
    }
    case NULL: {
        r.nextNull();
        w.writeEmptyElement(NS, "null");
        if (label != null)
            w.writeAttribute("name", label);
        break;
    }
    case STRING: {
        w.writeStartElement(NS, "string");
        if (label != null)
            w.writeAttribute("name", label);
        w.writeCharacters(r.nextString());
        w.writeEndElement();
        break;
    }
    case NUMBER: {
        w.writeStartElement(NS, "number");
        if (label != null)
            w.writeAttribute("name", label);
        String s;
        try {
            s = String.valueOf(r.nextLong());
        } catch (Exception err) {
            s = String.valueOf(r.nextDouble());
        }

        w.writeCharacters(s);
        w.writeEndElement();
        break;
    }
    case BOOLEAN: {
        w.writeStartElement(NS, "boolean");
        if (label != null)
            w.writeAttribute("name", label);
        w.writeCharacters(String.valueOf(r.nextBoolean()));
        w.writeEndElement();
        break;
    }
    case END_DOCUMENT: {
        break;
    }
    default:
        throw new IllegalStateException(token.name());
    }

}

From source file:com.github.riotopsys.malforandroid2.util.AnimeWatchedStatusTypeAdapter.java

License:Apache License

@Override
public AnimeWatchedStatus read(JsonReader in) throws IOException {
    if (in.peek() != JsonToken.NULL) {
        return AnimeWatchedStatus.getByServerKey(in.nextString());
    }/*from ww w .  j a  v a2 s  .  co  m*/
    in.nextNull();
    return null;
}

From source file:com.github.riotopsys.malforandroid2.util.MangaReadStatusTypeAdapter.java

License:Apache License

@Override
public MangaReadStatus read(JsonReader in) throws IOException {
    if (in.peek() != JsonToken.NULL) {
        return MangaReadStatus.getByServerKey(in.nextString());
    }/*  w  w  w.ja v  a2 s .co  m*/
    in.nextNull();
    return null;
}

From source file:com.google.caliper.json.InstantTypeAdapter.java

License:Apache License

@Override
public Instant read(JsonReader in) throws IOException {
    return ISODateTimeFormat.dateTime().parseDateTime(in.nextString()).toInstant();
}

From source file:com.google.gerrit.server.notedb.CommentTimestampAdapter.java

License:Apache License

@Override
public Timestamp read(JsonReader in) throws IOException {
    String str = in.nextString();
    TemporalAccessor ta;/*from w w  w  . j  a  v a  2 s  .  c  o  m*/
    try {
        ta = ISO_INSTANT.parse(str);
    } catch (DateTimeParseException e) {
        ta = LocalDateTime.from(FALLBACK.parse(str)).atZone(ZoneId.systemDefault());
    }
    return Timestamp.from(Instant.from(ta));
}

From source file:com.google.maps.internal.AddressComponentTypeAdapter.java

License:Open Source License

/**
 * Read a address component type from a Geocoding API result and convert it to a
 * {@link AddressComponentType}.//from  ww  w.j ava2  s  .c  o m
 */
@Override
public AddressComponentType read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    return AddressComponentType.lookup(reader.nextString());
}