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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

From source file:DateTimeTypeAdapter.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from  w ww . j ava 2  s  . c  o m*/
        return null;
    }
    return new DateTime(in.nextString());
}

From source file:DurationTypeAdapter.java

License:Apache License

@Override
public Duration read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from   ww  w .j a  v  a2s  . c o m*/
        return null;
    }
    return new Duration(in.nextString());
}

From source file:TemporalTypeAdapter.java

License:Apache License

@Override
public T read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*www  .j  av a2  s.c  om*/
        return null;
    }
    String temporalString = preProcess(in.nextString());
    return parseFunction.apply(temporalString);
}

From source file:IntervalTypeAdapter.java

License:Apache License

@Override
public Interval read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*w w  w  .j a  v  a 2 s. co  m*/
        return null;
    }
    return new Interval(in.nextString());
}

From source file:abtlibrary.utils.as24ApiClient.JSON.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL://  w ww .  j a  v  a 2 s.c o m
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        return formatter.parseDateTime(date.substring(0, date.indexOf("Z")) + ".000Z");
    }
}

From source file:abtlibrary.utils.as24ApiClient.JSON.java

License:Apache License

@Override
public LocalDate read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL://from w w w. j  av  a  2s. co  m
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        return formatter.parseLocalDate(date);
    }
}

From source file:at.orz.arangodb.entity.CollectionStatusTypeAdapter.java

License:Apache License

@Override
public CollectionStatus read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*  www . j a v a2  s .  co  m*/
        return null;
    }

    CollectionStatus ret = CollectionStatus.valueOf(in.nextInt());
    return ret;
}

From source file:at.yawk.mojangapi.InstantTypeAdapter.java

License:Mozilla Public License

@Override
public Instant read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        return null;
    } else {/*from w  w  w .  j ava 2 s .c  o  m*/
        return Instant.ofEpochMilli(in.nextLong());
    }
}

From source file:at.yawk.mojangapi.UUIDTypeAdapter.java

License:Mozilla Public License

@Override
public UUID read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        return null;
    } else {//from  w  w w .  j a va 2s.  c om
        StringBuilder s = new StringBuilder(in.nextString());
        s.insert(8 + 4 * 3, '-');
        s.insert(8 + 4 * 2, '-');
        s.insert(8 + 4, '-');
        s.insert(8, '-');
        return UUID.fromString(s.toString());
    }
}

From source file:au.edu.unsw.cse.soc.federatedcloud.deployers.github.repository.GitHubClient.java

License:Open Source License

/**
 * Parse JSON to specified type//from w w  w  . j  av a2  s  .c  o m
 *
 * @param <V>
 * @param stream
 * @param type
 * @param listType
 * @return parsed type
 * @throws IOException
 */
protected <V> V parseJson(InputStream stream, Type type, Type listType) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, CHARSET_UTF8), bufferSize);
    if (listType == null)
        try {
            return gson.fromJson(reader, type);
        } catch (JsonParseException jpe) {
            IOException ioe = new IOException("Parse exception converting JSON to object"); //$NON-NLS-1$
            ioe.initCause(jpe);
            throw ioe;
        } finally {
            try {
                reader.close();
            } catch (IOException ignored) {
                // Ignored
            }
        }
    else {
        JsonReader jsonReader = new JsonReader(reader);
        try {
            if (jsonReader.peek() == BEGIN_ARRAY)
                return gson.fromJson(jsonReader, listType);
            else
                return gson.fromJson(jsonReader, type);
        } catch (JsonParseException jpe) {
            IOException ioe = new IOException("Parse exception converting JSON to object"); //$NON-NLS-1$
            ioe.initCause(jpe);
            throw ioe;
        } finally {
            try {
                jsonReader.close();
            } catch (IOException ignored) {
                // Ignored
            }
        }
    }
}