Example usage for com.google.gson.stream MalformedJsonException MalformedJsonException

List of usage examples for com.google.gson.stream MalformedJsonException MalformedJsonException

Introduction

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

Prototype

public MalformedJsonException(Throwable throwable) 

Source Link

Usage

From source file:com.gd.misc.test.JsonReader.java

License:Apache License

/**
 * Returns the {@link com.google.gson.stream.JsonToken#NUMBER double} value of the next token,
 * consuming it. If the next token is a string, this method will attempt to
 * parse it as a double using {@link Double#parseDouble(String)}.
 *
 * @throws IllegalStateException if the next token is not a literal value.
 * @throws NumberFormatException if the next literal value cannot be parsed
 *     as a double, or is non-finite./*from   w  w  w.j  a v  a2  s.c om*/
 */
public double nextDouble() throws IOException {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    if (p == PEEKED_LONG) {
        peeked = PEEKED_NONE;
        pathIndices[stackSize - 1]++;
        return (double) peekedLong;
    }

    if (p == PEEKED_NUMBER) {
        peekedString = new String(buffer, pos, peekedNumberLength);
        pos += peekedNumberLength;
    } else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
        peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
    } else if (p == PEEKED_UNQUOTED) {
        peekedString = nextUnquotedValue();
    } else if (p != PEEKED_BUFFERED) {
        throw new IllegalStateException("Expected a double but was " + peek() + locationString());
    }

    peeked = PEEKED_BUFFERED;
    double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
    if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) {
        throw new MalformedJsonException("JSON forbids NaN and infinities: " + result + locationString());
    }
    peekedString = null;
    peeked = PEEKED_NONE;
    pathIndices[stackSize - 1]++;
    return result;
}

From source file:com.gd.misc.test.JsonReader.java

License:Apache License

/**
 * Throws a new IO exception with the given message and a context snippet
 * with this reader's content./*  ww  w  . jav  a  2s  .  c  o  m*/
 */
private IOException syntaxError(String message) throws IOException {
    throw new MalformedJsonException(message + locationString());
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

protected JsonToken validateExpectedToken(JsonToken expectedToken) throws IOException, MalformedJsonException {
    JsonToken tokenType = jsonReader.peek();
    if (tokenType != expectedToken) {
        throw new MalformedJsonException("Expected: " + expectedToken + " Got: " + tokenType);
    }//  w  ww  .ja va2  s  . com
    return tokenType;
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

public void sourceError(String message) throws MalformedJsonException {
    // TODO: add source location to message.
    throw new MalformedJsonException(message);
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

public String consumeStringValue() throws IOException {
    if (jsonReader.peek() != JsonToken.STRING) {
        throw new MalformedJsonException("Expected: " + JsonToken.STRING);
    }//w  ww .j a  v a 2 s. c o m
    return jsonReader.nextString();
}

From source file:org.geotools.data.arcgisrest.GeoJSONParser.java

License:Open Source License

/**
 * Parses a Geometry in GeoJSON format//from ww  w.  ja  va  2 s  . c  om
 * 
 * @return list of arrays with ring coordinates
 * @throws IOException,
 *           JsonSyntaxException, IllegalStateException
 */
public Geometry parseGeometry() throws JsonSyntaxException, IOException, IllegalStateException {

    double[] coords;
    GeometryBuilder builder = new GeometryBuilder();
    GeometryFactory geomFactory = new GeometryFactory();

    // If geometry is null, returns a null point
    try {
        if (this.reader.peek() == JsonToken.NULL) {
            this.reader.nextNull();
            throw (new MalformedJsonException("just here to avoid repeating the return statement"));
        }
    } catch (IllegalStateException | MalformedJsonException e) {
        return builder.point();
    }

    this.reader.beginObject();

    // Check the presence of feature type
    if (!reader.nextName().equals(FEATURE_TYPE)) {
        throw (new JsonSyntaxException("Geometry type expected"));
    }

    switch (reader.nextString()) {

    case GEOMETRY_POINT:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        coords = this.parsePointCoordinates();
        this.reader.endObject();
        return (Geometry) builder.point(coords[0], coords[1]);

    case GEOMETRY_MULTIPOINT:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        List<double[]> pointCoords = this.parseMultiPointCoordinates();
        ;
        Point[] points = new Point[pointCoords.size()];
        for (int i = 0; i < pointCoords.size(); i++) {
            points[i] = (Point) builder.point(pointCoords.get(i)[0], pointCoords.get(i)[1]);
        }
        this.reader.endObject();
        return (Geometry) new MultiPoint(points, geomFactory);

    case GEOMETRY_LINE:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        coords = this.parseLineStringCoordinates();
        this.reader.endObject();
        return (Geometry) builder.lineString(coords);

    case GEOMETRY_MULTILINE:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        List<double[]> lineArrays = this.parseMultiLineStringCoordinates();
        LineString[] lines = new LineString[lineArrays.size()];
        int i = 0;
        for (double[] array : lineArrays) {
            lines[i++] = builder.lineString(array);
        }
        this.reader.endObject();
        return (Geometry) builder.multiLineString(lines);

    case GEOMETRY_POLYGON:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        List<double[]> rings = this.parsePolygonCoordinates();
        this.reader.endObject();
        return (Geometry) builder.polygon(rings.get(0)); // FIXME: what about
                                                         // holes?

    case GEOMETRY_MULTIPOLYGON:
        this.checkPropertyName(FEATURE_GEOMETRY_COORDINATES);
        List<List<double[]>> polyArrays = this.parseMultiPolygonCoordinates();
        Polygon[] polys = new Polygon[polyArrays.size()];
        int j = 0;
        for (List<double[]> array : polyArrays) {
            polys[j++] = builder.polygon(array.get(0)); // FIXME: what about holes?
        }
        this.reader.endObject();
        return (Geometry) builder.multiPolygon(polys);

    default:
        throw (new JsonSyntaxException("Unrecognized geometry type"));
    }

}

From source file:org.geotools.data.arcgisrest.GeoJSONParser.java

License:Open Source License

/**
 * Parses a GeoJSON feature properties. The values returned in a map is a
 * Boolean, a String, or a Double (for every numeric values)
 * /*from  w ww . ja  va 2 s  .co m*/
 * @return A map with property names as keys, and property values as values
 * 
 * @throws IOException,
 *           JsonSyntaxException, IllegalStateException
 */
public Map<String, Object> parseProperties() throws JsonSyntaxException, IOException, IllegalStateException {

    Map<String, Object> props = new HashMap<String, Object>();
    String name;

    // If properties is null, returns a null point
    // If geometry is null, returns a null point
    try {
        if (this.reader.peek() == JsonToken.NULL) {
            this.reader.nextNull();
            throw (new MalformedJsonException("just here to avoid repeating the return statement"));
        }
    } catch (IllegalStateException | MalformedJsonException e) {
        return props;
    }

    this.reader.beginObject();

    try {
        while (this.reader.hasNext()) {
            name = this.reader.nextName();

            switch (this.reader.peek()) {

            case BOOLEAN:
                props.put(name, this.reader.nextBoolean());
                break;

            case NUMBER:
                props.put(name, this.reader.nextDouble());
                break;

            case STRING:
                props.put(name, this.reader.nextString());
                break;

            case NULL:
                this.reader.nextNull();
                props.put(name, null);
                break;

            default:
                throw (new JsonSyntaxException("Value expected"));
            }
        }
    } catch (IOException | IllegalStateException e) {
        throw (new NoSuchElementException(e.getMessage()));
    }

    this.reader.endObject();

    return props;
}