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:de.dentrassi.pm.maven.ByteArrayAdapter.java

License:Open Source License

@Override
public byte[] read(final JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from  w  ww.  ja v  a  2  s.  co m
        return null;
    }

    final String data = reader.nextString();

    return Base64.getDecoder().decode(data);
}

From source file:de.feike.tiingoclient.JSON.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:// w w  w.ja v a 2  s . c  o m
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        return parseFormatter.parseDateTime(date);
    }
}

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next string value from the reader or null if it is a null value
 * @param reader The reader/*w  w w .  j a v a  2 s  .c o m*/
 * @return String value or null
 * @throws IOException
 */
public static String nextStringOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextString();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next string value from the reader or null if it is a null value
 * @param reader The reader/*from  ww w  . j a v  a  2  s.c om*/
 * @return Boolean value or null
 * @throws IOException
 */
public static Boolean nextBooleanOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextBoolean();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next double value from the reader or null if it is a null value
 * @param reader The reader//from w w  w . jav  a2 s .com
 * @return Double value or null
 * @throws IOException
 */
public static Double nextDoubleOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextDouble();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next integer value from the reader or null if it is a null value
 * @param reader The reader/*from  w w  w.  ja  v a  2 s . co  m*/
 * @return Integer value or null
 * @throws IOException
 */
public static Integer nextIntegerOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextInt();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next long value from the reader or null if it is a null value
 * @param reader The reader/* w  ww .  j a va2 s . c  om*/
 * @return Long value or null
 * @throws IOException
 */
public static Long nextLongOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextLong();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:de.m0ep.canvas.gson.ISO8601TypeAdapter.java

License:Open Source License

@Override
public Date read(JsonReader reader) throws IOException {
    if (JsonToken.NULL == reader.peek()) {
        reader.nextNull();/*from  ww  w  .  j a v  a  2  s. co m*/
        return null;
    }

    String date = reader.nextString();
    Calendar cal = DatatypeConverter.parseDateTime(date);
    return cal.getTime();
}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

License:Apache License

/**
 * unmarshall single JSON object//from  w  w  w. j a va2s .  c o  m
 *
 * @param beanToBeCreatedClass
 * @param <T>
 * @return
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 */
public static <T> T unmarshall(JsonReader reader, java.lang.Class<T> beanToBeCreatedClass) throws IOException,
        NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
    // nothing there - bail out
    reader.beginObject();

    if (reader.peek() == null) {
        return null;
    }
    T value = beanToBeCreatedClass.getConstructor().newInstance();
    while (reader.hasNext()) {
        String key = reader.nextName();

        //  capitalise to standard setter pattern
        StringBuilder sb = new StringBuilder();
        sb.append(SETTER_PREFIX).append(Character.toUpperCase(key.charAt(0))).append(key.substring(1));
        String methodName = sb.toString();

        Method method = getCandidateMethod(beanToBeCreatedClass, methodName);

        // must be kind of setter method
        if (method != null) {
            Class clazz = method.getParameterTypes()[0];
            // as we have setter, we can process value
            Object v = unmarshalValue(reader, clazz);

            // can we use setter method directly?
            if (clazz.isAssignableFrom(v.getClass())) {
                method.invoke(value, v);
                continue;
            }

            Object obj = convertToObject(clazz, v);
            if (obj != null)
                method.invoke(value, obj);
        } else {
            // no suitable method was found - skip this value altogether
            reader.skipValue();
        }
    }

    reader.endObject();

    return value;

}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

License:Apache License

/**
 * read array into list//from   ww w  .  j a  va 2  s  . co m
 *
 * @param reader
 * @param beanToBeCreatedClass
 * @return
 */
public static <T> List<T> unmarshallArray(JsonReader reader, java.lang.Class<T> beanToBeCreatedClass)
        throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException,
        IllegalAccessException {
    ArrayList<T> retval = new ArrayList();
    reader.beginArray();
    // read objects after each other
    while (reader.peek() == JsonToken.BEGIN_OBJECT) {
        retval.add(unmarshall(reader, beanToBeCreatedClass));
    }
    reader.endArray();

    return retval;
}