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:one.talon.api.JSON.java

License:Apache License

@Override
public LocalDate read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL://from   w  w w .  j  a  va2 s .c  o m
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        if (date.equals("0001-01-01T00:00:00Z")) {
            // explicitly convert to ISO8601 format date in case of empty "golang" string
            date = "0001-01-01T00:00:00.000+0000";
        }
        return formatter.parseLocalDate(date);
    }
}

From source file:ooo.oxo.moments.net.TimestampTypeAdapter.java

License:Open Source License

@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {/*from  w w w . ja v  a2 s  . c  om*/
        return new Date(Long.parseLong(in.nextString()) * 1000l);
    }
}

From source file:ooo.oxo.mr.net.ColorTypeAdapter.java

License:Open Source License

@Override
public Color read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {/* ww  w  .  ja  v a2 s .co  m*/
        try {
            return Color.parse(in.nextString());
        } catch (IllegalArgumentException e) {
            throw new JsonParseException(e);
        }
    }
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private List<InPort> readApplicationInputs(JsonReader jsonReader) throws IOException, ParserException {
    List<InPort> inPorts = new ArrayList<>();
    JsonToken peek = jsonReader.peek();//from w w w  .  j  a va2 s .  com
    PortModel portModel;
    InPort inPort;
    String name;
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            inPort = new InputPortIml(portModel);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    portModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    portModel.setPortId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DEFAULT_VALUE)) {
                    inPort.setDefaultValue(jsonReader.nextString());
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            inPorts.add(inPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application inputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));
    }

    return inPorts;
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private List<OutPort> readApplicationOutputs(JsonReader jsonReader) throws IOException, ParserException {
    List<OutPort> outPorts = new ArrayList<>();
    PortModel portModel;/*from w  ww. j a va 2s  .  c om*/
    OutPort outPort;
    String name;
    JsonToken peek = jsonReader.peek();
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            outPort = new OutPortImpl(portModel);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    portModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    portModel.setPortId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DEFAULT_VALUE)) {
                    jsonReader.skipValue(); // can output has default values?
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            outPorts.add(outPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application outputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));

    }
    return outPorts;
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readPosition(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();/*from w w  w .j a  v a 2  s  .c  o m*/
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // skip position data.
            jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readProperties(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();//w ww  .  j  a va2 s  .  c om
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // TODO: Read and use proprety values
            String name = jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }

}

From source file:org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java

License:Apache License

private LinkedHashMap<String, String> readNextObject(JsonReader reader)
        throws IOException, EndOfDocumentException {
    LinkedHashMap<String, String> row = new LinkedHashMap<>();
    boolean objectStarted = false;
    boolean shouldBeName = false;
    String currentName = null;/*from  ww w . j  a  v  a  2 s  .co  m*/

    while (true) {
        JsonToken token = reader.peek();
        switch (token) {
        case BEGIN_ARRAY:
            throw new IllegalArgumentException("Row data cannot have an array.");
        case END_ARRAY:
            throw new EndOfDocumentException("End of Json Array document.");
        case BEGIN_OBJECT:
            if (objectStarted == true) {
                throw new IllegalArgumentException("Nested objects not supported.");
            }
            if (shouldBeName == true) {
                throw new IllegalArgumentException("name expected, got begin_object");
            }
            objectStarted = true;
            shouldBeName = true;
            reader.beginObject();
            break;
        case END_OBJECT:
            if (shouldBeName == false) {
                throw new IllegalArgumentException("value expected, got end_object");
            }
            reader.endObject();
            return row;
        case NAME:
            if (shouldBeName == false) {
                throw new IllegalArgumentException("name not expected at this point.");
            }
            shouldBeName = false;
            currentName = reader.nextName();
            break;
        case NUMBER:
        case STRING:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            String n = reader.nextString();
            row.put(currentName, n);
            shouldBeName = true;
            break;
        case BOOLEAN:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            String b = String.valueOf(reader.nextBoolean());
            row.put(currentName, b);
            shouldBeName = true;
            break;
        case NULL:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            reader.nextNull();
            row.put(currentName, "");
            shouldBeName = true;
            break;
        case END_DOCUMENT:
            return row;

        default:
            throw new IllegalArgumentException(
                    "Illegal token detected inside json: token : " + token.toString());
        }
    }
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
        final Object typeMapping, final EntityProviderReadProperties readProperties)
        throws EdmException, EntityProviderException, IOException {
    final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
    Object value = null;/*from  ww  w. j a v  a 2  s  .c  o m*/
    final JsonToken tokenType = reader.peek();
    if (tokenType == JsonToken.NULL) {
        reader.nextNull();
    } else {
        switch (EdmSimpleTypeKind.valueOf(type.getName())) {
        case Boolean:
            if (tokenType == JsonToken.BOOLEAN) {
                value = reader.nextBoolean();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Byte:
        case SByte:
        case Int16:
        case Int32:
            if (tokenType == JsonToken.NUMBER) {
                value = reader.nextInt();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Single:
        case Double:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else if (tokenType == JsonToken.NUMBER) {
                value = reader.nextDouble();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        default:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        }
    }

    final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
    final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets()
            ? entityPropertyInfo.getFacets()
            : null;
    return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

@SuppressWarnings("unchecked")
private Object readComplexProperty(final JsonReader reader, final EntityComplexPropertyInfo complexPropertyInfo,
        final Object typeMapping, final EntityProviderReadProperties readProperties)
        throws EdmException, EntityProviderException, IOException {
    if (reader.peek().equals(JsonToken.NULL)) {
        reader.nextNull();
        if ((readProperties == null || readProperties.isValidatingFacets())
                && complexPropertyInfo.isMandatory()) {
            throw new EntityProviderException(
                    EntityProviderException.INVALID_PROPERTY_VALUE.addContent(complexPropertyInfo.getName()));
        }//ww  w  .  jav  a  2  s . c o m
        return null;
    }

    reader.beginObject();
    Map<String, Object> data = new HashMap<String, Object>();

    Map<String, Object> mapping;
    if (typeMapping != null) {
        if (typeMapping instanceof Map) {
            mapping = (Map<String, Object>) typeMapping;
        } else {
            throw new EntityProviderException(
                    EntityProviderException.INVALID_MAPPING.addContent(complexPropertyInfo.getName()));
        }
    } else {
        mapping = new HashMap<String, Object>();
    }

    while (reader.hasNext()) {
        String childName = reader.nextName();
        if (FormatJson.METADATA.equals(childName)) {
            reader.beginObject();
            childName = reader.nextName();
            if (!FormatJson.TYPE.equals(childName)) {
                throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
                        .addContent(FormatJson.TYPE).addContent(FormatJson.METADATA));
            }
            String actualTypeName = reader.nextString();
            String expectedTypeName = complexPropertyInfo.getType().getNamespace() + Edm.DELIMITER
                    + complexPropertyInfo.getType().getName();
            if (!expectedTypeName.equals(actualTypeName)) {
                throw new EntityProviderException(EntityProviderException.INVALID_ENTITYTYPE
                        .addContent(expectedTypeName).addContent(actualTypeName));
            }
            reader.endObject();
        } else {
            EntityPropertyInfo childPropertyInfo = complexPropertyInfo.getPropertyInfo(childName);
            if (childPropertyInfo == null) {
                throw new EntityProviderException(
                        EntityProviderException.INVALID_PROPERTY.addContent(childName));
            }
            Object childData = readPropertyValue(reader, childPropertyInfo, mapping.get(childName),
                    readProperties);
            if (data.containsKey(childName)) {
                throw new EntityProviderException(
                        EntityProviderException.DOUBLE_PROPERTY.addContent(childName));
            }
            data.put(childName, childData);
        }
    }
    reader.endObject();
    return data;
}