Example usage for com.fasterxml.jackson.core JsonToken START_ARRAY

List of usage examples for com.fasterxml.jackson.core JsonToken START_ARRAY

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonToken START_ARRAY.

Prototype

JsonToken START_ARRAY

To view the source code for com.fasterxml.jackson.core JsonToken START_ARRAY.

Click Source Link

Document

START_ARRAY is returned when encountering '[' which signals starting of an Array value

Usage

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Starts an Array, skipping the '[' token, and if necessary a field name before it.
 *//*from  w  ww. j  a v a 2s .com*/
public static void startArray(JsonParser par) throws IOException {
    JsonToken token = par.getCurrentToken();
    if (token == null || token == JsonToken.FIELD_NAME) {
        token = par.nextToken();
    }
    if (token == JsonToken.START_ARRAY) {
        par.nextToken();
    } else {
        throw new JsonParseException(par, "Expected start of array");
    }
}

From source file:com.michaelwitbrock.jacksonstream.JsonArrayStreamDataSupplier.java

public JsonArrayStreamDataSupplier(File dataFile, Class<T> type) {
    this.type = type;
    try {/*from w  w  w  .j  a  v a 2  s. co  m*/
        // Setup and get into a state to start iterating
        parser = factory.createParser(dataFile);
        parser.setCodec(mapper);
        JsonToken token = parser.nextToken();
        if (token == null) {
            throw new RuntimeException("Can't get any JSON Token from " + dataFile.getAbsolutePath());
        }

        // the first token is supposed to be the start of array '['
        if (!JsonToken.START_ARRAY.equals(token)) {
            // return or throw exception
            maybeHasNext = false;
            throw new RuntimeException(
                    "Can't get any JSON Token fro array start from " + dataFile.getAbsolutePath());
        }
    } catch (Exception e) {
        maybeHasNext = false;
    }
    maybeHasNext = true;
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.OldAliasListDeserializer.java

@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    List<String> result;

    ObjectCodec codec = p.getCodec();/*from  w w w. j a v a 2s .c o m*/

    if (p.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = codec.readValue(p, new TypeReference<List<String>>() {
        });
    } else {
        LinkedHashMap<Integer, String> map = codec.readValue(p,
                new TypeReference<LinkedHashMap<Integer, String>>() {
                });

        result = new ArrayList<String>(map.values());
    }
    return result;
}

From source file:org.tanrabad.survey.service.json.MultiPolygonTypeConverter.java

private List<Location> getChildPolygon(JsonParser jsonParser) throws IOException {
    List<Location> eachChildPolygon;
    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
        eachChildPolygon = new ArrayList<>();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            eachChildPolygon.add(getLocation(jsonParser));
        }//from  ww w  . jav a  2s  . co  m
    } else {
        eachChildPolygon = null;
    }
    return eachChildPolygon;
}

From source file:org.onosproject.north.aaa.api.parser.impl.ScopeParser.java

@Override
public Set<Scope> parseJson(InputStream stream) throws IOException, ParseException {
    // begin parsing JSON to Application class
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<Scope> scopeSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }//w  w w  . j a  v  a2s  .com
        if (JsonToken.FIELD_NAME.equals(token) && "scopes".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after scopes");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }
                if (JsonToken.START_OBJECT.equals(token)) {
                    Scope scope = jsonToScope(jp);
                    scopeSet.add(scope);
                }
            }
        }
    }
    return scopeSet;
}

From source file:org.onosproject.north.aaa.api.parser.impl.ApplicationParser.java

@Override
public Set<Application> parseJson(InputStream stream) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<Application> applicationSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }//from w  ww . j  ava 2s . c om

        if (JsonToken.FIELD_NAME.equals(token) && "applications".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after applications");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }

                if (JsonToken.START_OBJECT.equals(token)) {
                    Application app = jsonToApplication(jp);
                    applicationSet.add(app);
                }
            }
        }
    }
    return applicationSet;
}

From source file:org.apache.batchee.jackson.JacksonJsonReader.java

@Override
public void open(final Serializable checkpoint) throws Exception {
    super.open(checkpoint);

    final ObjectMapper mapper = Jacksons.newMapper(configuration);
    parser = mapper.getFactory().createParser(new File(file));
    if (type != null) {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
    } else {/*from w ww  . j  av a  2s. co  m*/
        clazz = null;
    }

    if (skipRoot == null || "true".equalsIgnoreCase(skipRoot)) {
        final JsonToken token = parser.nextToken();
        if (token == JsonToken.START_ARRAY) {
            end = JsonToken.END_ARRAY;
        } else {
            end = JsonToken.END_OBJECT;
        }
    }
}

From source file:org.onosproject.north.aaa.api.parser.impl.RestAccessParser.java

@Override
public Set<RestAccess> parseJson(InputStream stream) throws IOException, ParseException {
    // begin parsing JSON to Application class
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<RestAccess> restAccessSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }/*from ww  w.j ava 2s.c  o m*/

        if (JsonToken.FIELD_NAME.equals(token) && "accesses".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after accesses");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }

                if (JsonToken.START_OBJECT.equals(token)) {
                    RestAccess restAccess = jsonToRestAccess(jp);
                    restAccessSet.add(restAccess);
                }
            }
        }
    }
    return restAccessSet;
}

From source file:com.basistech.AfterburnerOopsTest.java

@Test
public void oops() throws Exception {
    SmileFactory smileFactory = new SmileFactory();
    ObjectMapper mapper = new ObjectMapper(smileFactory);
    mapper = AnnotatedDataModelModule.setupObjectMapper(mapper);

    EntityMention em = new EntityMention();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<EntityMention> mentions = Lists.newArrayList();
    mentions.add(em);/* ww w.j a  va  2s. c  o  m*/
    mapper.writeValue(byteArrayOutputStream, mentions);

    mapper = AnnotatedDataModelModule.setupObjectMapper(new ObjectMapper(smileFactory));
    mapper.registerModule(new AfterburnerModule());
    JsonParser jp = smileFactory.createParser(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    jp.setCodec(mapper);

    JsonToken current;
    current = jp.nextToken();
    if (current != JsonToken.START_ARRAY) {
        System.err.println("Error: root should be array: quiting.");
        return;
    }

    while (jp.nextToken() != JsonToken.END_ARRAY) {
        jp.readValueAs(EntityMention.class);
    }

}

From source file:com.amazonaws.hal.client.HalJsonListUnmarshaller.java

@Override
public List<Object> unmarshall(JsonUnmarshallerContext context) throws Exception {
    List<Object> list = new ArrayList<>();
    JsonToken token = context.getCurrentToken();

    while (token != null && token != JsonToken.END_ARRAY) {
        if (token.isScalarValue()) {
            list.add(JsonUnmarshallerUtil.getObjectForToken(token, context));
        } else if (token == JsonToken.START_OBJECT) {
            context.nextToken();//from w  w  w  .  ja v a 2  s  .com
            list.add(HalJsonMapUnmarshaller.getInstance().unmarshall(context));
        } else if (token == JsonToken.START_ARRAY) {
            context.nextToken();
            list.add(HalJsonListUnmarshaller.getInstance().unmarshall(context));
        }

        token = context.nextToken();
    }

    return list;
}