Example usage for com.google.gson.stream JsonToken BEGIN_ARRAY

List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY

Introduction

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

Prototype

JsonToken BEGIN_ARRAY

To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.

Click Source Link

Document

The opening of a JSON array.

Usage

From source file:org.eclipse.skalli.core.rest.JSONRestReader.java

License:Open Source License

@Override
public boolean isValue() throws IOException {
    if (state == STATE_FINAL) {
        return false;
    }/*  w w  w .j av  a 2  s. co  m*/
    if (isKey("value")) { //$NON-NLS-1$
        return true;
    }
    JsonToken next = json.peek();
    return lookAhead == null && (next == JsonToken.STRING || next == JsonToken.NUMBER
            || next == JsonToken.BOOLEAN || next == JsonToken.BEGIN_ARRAY || next == JsonToken.BEGIN_OBJECT
            || next == JsonToken.NULL);
}

From source file:org.eclipse.skalli.core.rest.JSONRestReader.java

License:Open Source License

@Override
public boolean isArray() throws IOException {
    if (state == STATE_FINAL) {
        return false;
    }//from  ww  w .j  a v  a 2 s . c  o  m
    return json.peek() == JsonToken.BEGIN_ARRAY;
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.RuleGSONParser.java

License:Open Source License

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/* w ww .  j  a  v  a 2 s  . c o  m*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Rule[] rules = gson.fromJson(jr, Rule[].class);
                return new HashSet<Rule>(Arrays.asList(rules));
            } else {
                Rule rule = gson.fromJson(jr, Rule.class);
                Set<Rule> rules = new HashSet<Rule>();
                rules.add(rule);
                return rules;
            }
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.TemplateGSONParser.java

License:Open Source License

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/*from   w  w w .ja va2s .  c o m*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Template[] templates = gson.fromJson(jr, RuleTemplate[].class);
                return new HashSet<Template>(Arrays.asList(templates));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                Set<Template> templates = new HashSet<Template>();
                templates.add(template);
                return templates;
            }
        }
    } catch (Exception e1) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e1));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.eclipse.smarthome.storage.json.PropertiesTypeAdapter.java

License:Open Source License

@Override
public Map<String, Object> read(JsonReader in) throws IOException {
    // gson implementation code is modified when deserializing numbers
    JsonToken peek = in.peek();/*from www. ja  v a  2  s  .c o m*/
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    Map<String, Object> map = constructor.get(TOKEN).construct();

    if (peek == JsonToken.BEGIN_ARRAY) {
        in.beginArray();
        while (in.hasNext()) {
            in.beginArray(); // entry array
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
            in.endArray();
        }
        in.endArray();
    } else {
        in.beginObject();
        while (in.hasNext()) {
            JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
        }
        in.endObject();
    }
    return map;
}

From source file:org.immutables.gson.stream.JsonParserReader.java

License:Apache License

private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) {
    switch (token) {
    case START_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case END_ARRAY:
        return JsonToken.END_ARRAY;
    case START_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case END_OBJECT:
        return JsonToken.END_OBJECT;
    case FIELD_NAME:
        return JsonToken.NAME;
    case VALUE_FALSE:
        return JsonToken.BOOLEAN;
    case VALUE_TRUE:
        return JsonToken.BOOLEAN;
    case VALUE_NULL:
        return JsonToken.NULL;
    case VALUE_NUMBER_INT:
        return JsonToken.NUMBER;
    case VALUE_NUMBER_FLOAT:
        return JsonToken.NUMBER;
    case VALUE_STRING:
        return JsonToken.STRING;
    default: // Not semantically equivalent
        return JsonToken.NULL;
    }//from  w  w  w  .  java 2  s  .c o m
}

From source file:org.immutables.mongo.bson4gson.BsonReader.java

License:Apache License

private static JsonToken toGsonToken(BsonType type) {
    switch (type) {
    case END_OF_DOCUMENT:
        return JsonToken.END_DOCUMENT;
    case DOCUMENT:
        return JsonToken.BEGIN_OBJECT;
    case ARRAY://from www  .j  av a 2  s  .co m
        return JsonToken.BEGIN_ARRAY;
    case BOOLEAN:
        return JsonToken.BOOLEAN;
    case STRING:
    case SYMBOL:
    case OBJECT_ID:
    case BINARY:
    case REGULAR_EXPRESSION:
        return JsonToken.STRING;
    case DATE_TIME:
    case DOUBLE:
    case INT32:
    case INT64:
    case TIMESTAMP:
    case DECIMAL128:
        return JsonToken.NUMBER;
    case NULL:
        return JsonToken.NULL;
    default:
        // not really sure what to do with this type
        return JsonToken.NULL;
    }
}

From source file:org.jetbrains.io.JsonReaderEx.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *//*from w ww . j a  v  a2s.c  o m*/
public JsonToken peek() {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case PEEKED_END_OBJECT:
        return JsonToken.END_OBJECT;
    case PEEKED_BEGIN_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case PEEKED_END_ARRAY:
        return JsonToken.END_ARRAY;
    case PEEKED_SINGLE_QUOTED_NAME:
    case PEEKED_DOUBLE_QUOTED_NAME:
    case PEEKED_UNQUOTED_NAME:
        return JsonToken.NAME;
    case PEEKED_TRUE:
    case PEEKED_FALSE:
        return JsonToken.BOOLEAN;
    case PEEKED_NULL:
        return JsonToken.NULL;
    case PEEKED_SINGLE_QUOTED:
    case PEEKED_DOUBLE_QUOTED:
    case PEEKED_UNQUOTED:
    case PEEKED_BUFFERED:
        return JsonToken.STRING;
    case PEEKED_LONG:
    case PEEKED_NUMBER:
        return JsonToken.NUMBER;
    case PEEKED_EOF:
        return JsonToken.END_DOCUMENT;
    default:
        throw new AssertionError();
    }
}

From source file:org.kairosdb.core.http.rest.json.DataPointsParser.java

License:Apache License

public ValidationErrors parse() throws IOException, DatastoreException {
    long start = System.currentTimeMillis();
    ValidationErrors validationErrors = new ValidationErrors();

    JsonReader reader = new JsonReader(inputStream);

    try {/*  w w w . j a v  a2 s . c  o  m*/
        int metricCount = 0;

        if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) {
            try {
                reader.beginArray();

                while (reader.hasNext()) {
                    NewMetric metric = parseMetric(reader);
                    validateAndAddDataPoints(metric, validationErrors, metricCount);
                    metricCount++;
                }
            } catch (EOFException e) {
                validationErrors.addErrorMessage("Invalid json. No content due to end of input.");
            }

            reader.endArray();
        } else if (reader.peek().equals(JsonToken.BEGIN_OBJECT)) {
            NewMetric metric = parseMetric(reader);
            validateAndAddDataPoints(metric, validationErrors, 0);
        } else
            validationErrors.addErrorMessage("Invalid start of json.");

    } catch (EOFException e) {
        validationErrors.addErrorMessage("Invalid json. No content due to end of input.");
    } finally {
        reader.close();
    }

    ingestTime = (int) (System.currentTimeMillis() - start);

    return validationErrors;
}

From source file:org.mythdroid.services.GuideService.java

License:Open Source License

/**
 * Get ProgramGuide data/*from   www  . ja v a  2s .c  o m*/
 * @param start Date for start of data
 * @param end Date for end of data
 * @return ArrayList of Channels
 */
public ArrayList<Channel> GetProgramGuide(Date start, Date end) throws IOException {

    final Params params = new Params();
    params.put("StartTime", Globals.utcFormat(start)); //$NON-NLS-1$
    params.put("EndTime", Globals.utcFormat(end)); //$NON-NLS-1$
    params.put("StartChanId", 0); //$NON-NLS-1$
    params.put("NumChannels", -1); //$NON-NLS-1$
    params.put("Details", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    InputStream is = jc.GetStream("GetProgramGuide", params); //$NON-NLS-1$
    JsonReader jreader = new JsonReader(new BufferedReader(new InputStreamReader(is, "UTF-8")) //$NON-NLS-1$
    );

    ArrayList<Channel> channels = new ArrayList<Channel>();

    jreader.beginObject();
    skipTo(jreader, JsonToken.BEGIN_OBJECT);
    jreader.beginObject();
    skipTo(jreader, JsonToken.NAME);
    while (jreader.hasNext()) {
        String name = jreader.nextName();
        if (name.equals("NumOfChannels")) { //$NON-NLS-1$
            channels.ensureCapacity(jreader.nextInt());
            break;
        }
        jreader.skipValue();
    }
    skipTo(jreader, JsonToken.BEGIN_ARRAY);
    jreader.beginArray();
    while (jreader.hasNext()) {
        jreader.beginObject();
        channels.add((Channel) gson.fromJson(jreader, Channel.class));
        jreader.endObject();
    }
    jreader.endArray();
    jreader.endObject();
    jreader.endObject();
    jreader.close();
    jc.endStream();

    return channels;

}