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.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importCalendarContent(final JsonParser jsonParser, final Resource resource) throws IOException {
    if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        jsonParser.nextToken(); // skip START_ARRAY here
    } else {/*  w  w  w  .ja  v a  2  s .c  om*/
        throw new IOException("Improperly formed JSON - expected an START_ARRAY token, but got "
                + jsonParser.getCurrentToken().toString());
    }
    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        while (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
            extractEvent(jsonParser, resource);
            jsonParser.nextToken(); // get the next token - either a start object token or an end array token
        }
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

From source file:com.netflix.hollow.jsonadapter.HollowJsonAdapter.java

private int addUnstructuredMap(JsonParser parser, String mapTypeName, HollowMapWriteRecord mapRec)
        throws IOException {
    mapRec.reset();/* www. j  av  a 2  s  .c  o m*/

    HollowMapSchema schema = (HollowMapSchema) hollowSchemas.get(mapTypeName);
    ObjectFieldMapping valueRec = null;
    ObjectMappedFieldPath fieldMapping = null;

    JsonToken token = parser.nextToken();

    while (token != JsonToken.END_OBJECT) {
        if (token != JsonToken.FIELD_NAME) {
            HollowObjectWriteRecord mapKeyWriteRecord = (HollowObjectWriteRecord) getWriteRecord(
                    schema.getKeyType());
            String fieldName = mapKeyWriteRecord.getSchema().getFieldName(0);
            mapKeyWriteRecord.reset();

            switch (mapKeyWriteRecord.getSchema().getFieldType(0)) {
            case STRING:
                mapKeyWriteRecord.setString(fieldName, parser.getCurrentName());
                break;
            case BOOLEAN:
                mapKeyWriteRecord.setBoolean(fieldName, Boolean.valueOf(parser.getCurrentName()));
                break;
            case INT:
                mapKeyWriteRecord.setInt(fieldName, Integer.parseInt(parser.getCurrentName()));
                break;
            case LONG:
                mapKeyWriteRecord.setLong(fieldName, Long.parseLong(parser.getCurrentName()));
                break;
            case DOUBLE:
                mapKeyWriteRecord.setDouble(fieldName, Double.parseDouble(parser.getCurrentName()));
                break;
            case FLOAT:
                mapKeyWriteRecord.setFloat(fieldName, Float.parseFloat(parser.getCurrentName()));
                break;
            default:
                throw new IOException("Cannot parse type " + mapKeyWriteRecord.getSchema().getFieldType(0)
                        + " as key in map (" + mapKeyWriteRecord.getSchema().getName() + ")");
            }

            int keyOrdinal = stateEngine.add(schema.getKeyType(), mapKeyWriteRecord);

            int valueOrdinal;

            if (token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY) {
                valueOrdinal = parseSubType(parser, token, schema.getValueType());
            } else {
                if (valueRec == null) {
                    valueRec = getObjectFieldMapping(schema.getValueType());
                    fieldMapping = valueRec.getSingleFieldMapping();
                }
                addObjectField(parser, token, fieldMapping);
                valueOrdinal = valueRec.build(-1);
            }

            mapRec.addEntry(keyOrdinal, valueOrdinal);
        }
        token = parser.nextToken();
    }

    return stateEngine.add(schema.getName(), mapRec);
}

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

@SuppressWarnings("deprecation")
protected void readVideoField(JsonParser par, Video.Builder video, String fieldName) throws IOException {
    switch (fieldName) {
    case "mimes":
        for (startArray(par); endArray(par); par.nextToken()) {
            video.addMimes(par.getText());
        }/*from   w w  w.  j av a2  s.c  om*/
        break;
    case "minduration":
        video.setMinduration(par.getIntValue());
        break;
    case "maxduration":
        video.setMaxduration(par.getIntValue());
        break;
    case "protocol": {
        Protocol value = Protocol.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            video.setProtocol(value);
        }
    }
        break;
    case "protocols":
        for (startArray(par); endArray(par); par.nextToken()) {
            Protocol value = Protocol.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addProtocols(value);
            }
        }
        break;
    case "w":
        video.setW(par.getIntValue());
        break;
    case "h":
        video.setH(par.getIntValue());
        break;
    case "startdelay":
        video.setStartdelay(par.getIntValue());
        break;
    case "linearity": {
        VideoLinearity value = VideoLinearity.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            video.setLinearity(value);
        }
    }
        break;
    case "sequence":
        video.setSequence(par.getIntValue());
        break;
    case "battr":
        for (startArray(par); endArray(par); par.nextToken()) {
            CreativeAttribute value = CreativeAttribute.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addBattr(value);
            }
        }
        break;
    case "maxextended":
        video.setMaxextended(par.getIntValue());
        break;
    case "minbitrate":
        video.setMinbitrate(par.getIntValue());
        break;
    case "maxbitrate":
        video.setMaxbitrate(par.getIntValue());
        break;
    case "boxingallowed":
        video.setBoxingallowed(par.getValueAsBoolean());
        break;
    case "playbackmethod":
        for (startArray(par); endArray(par); par.nextToken()) {
            PlaybackMethod value = PlaybackMethod.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addPlaybackmethod(value);
            }
        }
        break;
    case "delivery":
        for (startArray(par); endArray(par); par.nextToken()) {
            ContentDeliveryMethod value = ContentDeliveryMethod.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addDelivery(value);
            }
        }
        break;
    case "pos": {
        AdPosition value = AdPosition.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            video.setPos(value);
        }
    }
        break;
    case "companionad":
        if (peekStructStart(par) == JsonToken.START_ARRAY) {
            // OpenRTB 2.2+
            for (startArray(par); endArray(par); par.nextToken()) {
                video.addCompanionad(readBanner(par));
            }
        } else { // START_OBJECT
            // OpenRTB 2.1-
            video.setCompanionad21(readCompanionAd(par));
        }
        break;
    case "api":
        for (startArray(par); endArray(par); par.nextToken()) {
            APIFramework value = APIFramework.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addApi(value);
            }
        }
        break;
    case "companiontype":
        for (startArray(par); endArray(par); par.nextToken()) {
            CompanionType value = CompanionType.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                video.addCompaniontype(value);
            }
        }
        break;
    case "skip":
        video.setSkip(par.getValueAsBoolean());
        break;
    case "skipmin":
        video.setSkipmin(par.getIntValue());
        break;
    case "skipafter":
        video.setSkipafter(par.getIntValue());
        break;
    default:
        readOther(video, par, fieldName);
    }
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importTallyContent(final JsonParser jsonParser, final Resource resource) throws IOException {

    SocialResourceConfiguration config = socialUtils.getStorageConfig(resource);
    final String rootPath = config.getAsiPath();
    resProvider = SocialResourceUtils.getSocialResource(resource.getResourceResolver().getResource(rootPath))
            .getResourceProvider();/*from  w  ww  .  jav  a  2  s . c  o  m*/
    if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        extractTally(resource, jsonParser, resProvider, tallyOperationsService);
        jsonParser.nextToken(); // get the next token - either a start object token or an end array token
    } else {
        throw new IOException("Improperly formed JSON - expected an START_ARRAY token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

private JsonToken _nextAfterName() {
    _nameCopied = false; // need to invalidate if it was copied
    JsonToken t = _nextToken;/*from w  w  w .  j ava 2  s .c  o  m*/
    _nextToken = null;
    // Also: may need to start new context?
    if (t == JsonToken.START_ARRAY) {
        _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
    } else if (t == JsonToken.START_OBJECT) {
        _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol);
    }
    return (_currToken = t);
}

From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parse one value for a field, which is either an
 *  object matching the type of the field, or a {boost:
 *  ..., value: ...}. *///  w w  w  .  ja v a 2s.c  o m
private static boolean parseOneValue(FieldDef fd, JsonParser p, Document doc) throws IOException {

    Object o = null;
    float boost = 1.0f;

    JsonToken token = p.nextToken();
    if (token == JsonToken.START_ARRAY) {
        if ("hierarchy".equals(fd.faceted) || fd.valueType == FieldDef.FieldValueType.LAT_LON) {
            o = getNativeValue(fd, token, p);
        } else {
            if (fd.multiValued == false) {
                fail(fd.name, "expected single value, not array, since this field is not multiValued");
            }
            while (true) {
                if (!parseOneValue(fd, p, doc)) {
                    break;
                }
            }
            return true;
        }
    } else {

        if (token == JsonToken.END_ARRAY) {
            assert fd.multiValued;
            return false;
        }

        if (fd.fieldType.indexOptions() != IndexOptions.NONE && token == JsonToken.START_OBJECT) {
            // Parse a {boost: X, value: Y}
            while (true) {
                token = p.nextToken();
                if (token == JsonToken.END_OBJECT) {
                    break;
                }
                assert token == JsonToken.FIELD_NAME;
                String key = p.getText();
                if (key.equals("boost")) {
                    token = p.nextToken();
                    if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) {
                        boost = p.getFloatValue();
                    } else {
                        fail(fd.name, "boost in inner object field value must have float or int value; got: "
                                + token);
                    }
                } else if (key.equals("value")) {
                    o = getNativeValue(fd, p.nextToken(), p);
                } else {
                    fail(fd.name, "unrecognized json key \"" + key
                            + "\" in inner object field value; must be boost or value");
                }
            }
            if (o == null) {
                fail(fd.name, "missing 'value' key");
            }
        } else {
            // Parse a native value:
            o = getNativeValue(fd, token, p);
        }
    }

    parseOneNativeValue(fd, doc, o, boost);
    return true;
}

From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java

protected boolean Sync(String service, String scope, String params) {
    final Date start = new Date();
    boolean hasError = false;
    if (params == null)
        params = "";
    final SQLiteDatabase db = mDB.getWritableDatabase();
    final ArrayList<TableInfo> notifyTableInfo = new ArrayList<TableInfo>();

    final String download = String.format(contentHelper.DOWNLOAD_SERVICE_URI, service, scope, params);
    final String upload = String.format(contentHelper.UPLOAD_SERVICE_URI, service, scope, params);
    final String scopeServerBlob = String.format("%s.%s.%s", service, scope, _.serverBlob);
    String serverBlob = null;//from   w w  w  . jav a  2  s . co  m
    Cursor cur = db.query(BlobsTable.NAME, new String[] { BlobsTable.C_VALUE }, BlobsTable.C_NAME + "=?",
            new String[] { scopeServerBlob }, null, null, null);
    final String originalBlob;
    if (cur.moveToFirst()) {
        originalBlob = serverBlob = cur.getString(0);
    } else {
        originalBlob = null;
    }
    cur.close();
    db.beginTransaction();
    try {
        boolean nochanges = false;
        if (serverBlob != null) {
            nochanges = !contentHelper.hasDirtTable(db, scope);
        }
        boolean resolve = false;
        final Metadata meta = new Metadata();
        final HashMap<String, Object> vals = new HashMap<String, Object>();
        final ContentValues cv = new ContentValues(2);
        JsonFactory jsonFactory = new JsonFactory();
        JsonToken current = null;
        String name = null;
        boolean moreChanges = false;
        boolean forceMoreChanges = false;
        do {
            final int requestMethod;
            final String serviceRequestUrl;
            final ContentProducer contentProducer;

            if (serverBlob != null) {
                requestMethod = HTTP_POST;
                if (nochanges) {
                    serviceRequestUrl = download;
                } else {
                    serviceRequestUrl = upload;
                    forceMoreChanges = true;
                }
                contentProducer = new SyncContentProducer(jsonFactory, db, scope, serverBlob, !nochanges,
                        notifyTableInfo, contentHelper);
                nochanges = true;
            } else {
                requestMethod = HTTP_GET;
                serviceRequestUrl = download;
                contentProducer = null;

            }
            if (moreChanges) {
                db.beginTransaction();
            }

            Result result = executeRequest(requestMethod, serviceRequestUrl, contentProducer);
            if (result.getStatus() == HttpStatus.SC_OK) {
                final JsonParser jp = jsonFactory.createParser(result.getInputStream());

                jp.nextToken(); // skip ("START_OBJECT(d) expected");
                jp.nextToken(); // skip ("FIELD_NAME(d) expected");
                if (jp.nextToken() != JsonToken.START_OBJECT)
                    throw new Exception("START_OBJECT(d - object) expected");
                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    name = jp.getCurrentName();
                    if (_.__sync.equals(name)) {
                        current = jp.nextToken();
                        while (jp.nextToken() != JsonToken.END_OBJECT) {
                            name = jp.getCurrentName();
                            current = jp.nextToken();
                            if (_.serverBlob.equals(name)) {
                                serverBlob = jp.getText();
                            } else if (_.moreChangesAvailable.equals(name)) {
                                moreChanges = jp.getBooleanValue() || forceMoreChanges;
                                forceMoreChanges = false;
                            } else if (_.resolveConflicts.equals(name)) {
                                resolve = jp.getBooleanValue();
                            }
                        }
                    } else if (_.results.equals(name)) {
                        if (jp.nextToken() != JsonToken.START_ARRAY)
                            throw new Exception("START_ARRAY(results) expected");
                        while (jp.nextToken() != JsonToken.END_ARRAY) {
                            meta.isDeleted = false;
                            meta.tempId = null;
                            vals.clear();
                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                name = jp.getCurrentName();
                                current = jp.nextToken();
                                if (current == JsonToken.VALUE_STRING) {
                                    vals.put(name, jp.getText());
                                } else if (current == JsonToken.VALUE_NUMBER_INT) {
                                    vals.put(name, jp.getLongValue());
                                } else if (current == JsonToken.VALUE_NUMBER_FLOAT) {
                                    vals.put(name, jp.getDoubleValue());
                                } else if (current == JsonToken.VALUE_FALSE) {
                                    vals.put(name, 0L);
                                } else if (current == JsonToken.VALUE_TRUE) {
                                    vals.put(name, 1L);
                                } else if (current == JsonToken.VALUE_NULL) {
                                    vals.put(name, null);
                                } else {
                                    if (current == JsonToken.START_OBJECT) {
                                        if (_.__metadata.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                                if (_.uri.equals(name)) {
                                                    meta.uri = jp.getText();
                                                } else if (_.type.equals(name)) {
                                                    meta.type = jp.getText();
                                                } else if (_.isDeleted.equals(name)) {
                                                    meta.isDeleted = jp.getBooleanValue();
                                                } else if (_.tempId.equals(name)) {
                                                    meta.tempId = jp.getText();
                                                }
                                            }
                                        } else if (_.__syncConflict.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                                if (_.isResolved.equals(name)) {
                                                } else if (_.conflictResolution.equals(name)) {
                                                } else if (_.conflictingChange.equals(name)) {
                                                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                        name = jp.getCurrentName();
                                                        current = jp.nextToken();
                                                        if (current == JsonToken.START_OBJECT) {
                                                            if (_.__metadata.equals(name)) {
                                                                while (jp.nextToken() != JsonToken.END_OBJECT) {

                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            // resolve conf

                                        } else if (_.__syncError.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                            }
                                        }
                                    }
                                }
                            }
                            TableInfo tab = contentHelper.getTableFromType(meta.type);
                            if (meta.isDeleted) {
                                tab.DeleteWithUri(meta.uri, db);
                            } else {
                                tab.SyncJSON(vals, meta, db);
                            }
                            if (!notifyTableInfo.contains(tab))
                                notifyTableInfo.add(tab);
                        }
                    }
                }
                jp.close();
                if (!hasError) {
                    cv.clear();
                    cv.put(BlobsTable.C_NAME, scopeServerBlob);
                    cv.put(BlobsTable.C_VALUE, serverBlob);
                    cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis());
                    cv.put(BlobsTable.C_STATE, 0);
                    db.replace(BlobsTable.NAME, null, cv);
                    db.setTransactionSuccessful();
                    db.endTransaction();
                    if (DEBUG) {
                        Log.d(TAG, "CP-Sync: commit changes");
                    }
                    final ContentResolver cr = getContext().getContentResolver();
                    for (TableInfo t : notifyTableInfo) {
                        final Uri nu = contentHelper.getDirUri(t.name, false);
                        cr.notifyChange(nu, null, false);
                        // false - do not force sync cause we are in sync
                        if (DEBUG) {
                            Log.d(TAG, "CP-Sync: notifyChange table: " + t.name + ", uri: " + nu);
                        }

                        for (String n : t.notifyUris) {
                            cr.notifyChange(Uri.parse(n), null, false);
                            if (DEBUG) {
                                Log.d(TAG, "+uri: " + n);
                            }
                        }
                    }
                    notifyTableInfo.clear();
                }
            } else {
                if (DEBUG) {
                    Log.e(TAG, "Server error in fetching remote contacts: " + result.getStatus());
                }
                hasError = true;
                break;
            }
        } while (moreChanges);
    } catch (final ConnectTimeoutException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ConnectTimeoutException", e);
        }
    } catch (final IOException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } catch (final ParseException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ParseException", e);
        }
    } catch (final Exception e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ParseException", e);
        }
    }
    if (hasError) {
        db.endTransaction();
        ContentValues cv = new ContentValues();
        cv.put(BlobsTable.C_NAME, scopeServerBlob);
        cv.put(BlobsTable.C_VALUE, originalBlob);
        cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis());
        cv.put(BlobsTable.C_STATE, -1);
        db.replace(BlobsTable.NAME, null, cv);
    }
    /*-if (!hasError) {
    final ContentValues cv = new ContentValues(2);
     cv.put(BlobsTable.C_NAME, scopeServerBlob);
     cv.put(BlobsTable.C_VALUE, serverBlob);
     db.replace(BlobsTable.NAME, null, cv);
     db.setTransactionSuccessful();
    }
    db.endTransaction();
    if (!hasError) {
     for (String t : notifyTableInfo) {
    getContext().getContentResolver().notifyChange(getDirUri(t),
          null);
     }
    }*/
    if (DEBUG) {
        Helpers.LogInfo(start);
    }
    return !hasError;
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

@Override
public String nextTextValue() throws IOException, JsonParseException {
    if (_currToken == JsonToken.FIELD_NAME) { // mostly copied from '_nextAfterName'
        _nameCopied = false;//from   ww  w  .  j a va  2  s . c o m
        JsonToken t = _nextToken;
        _nextToken = null;
        _currToken = t;
        if (t == JsonToken.VALUE_STRING) {
            if (_tokenIncomplete) {
                _tokenIncomplete = false;
                _finishString();
            }
            return _textBuffer.contentsAsString();
        }
        if (t == JsonToken.START_ARRAY) {
            _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
        } else if (t == JsonToken.START_OBJECT) {
            _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol);
        }
        return null;
    }
    // !!! TODO: optimize this case as well
    return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null;
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

@Override
public int nextIntValue(int defaultValue) throws IOException, JsonParseException {
    if (_currToken == JsonToken.FIELD_NAME) {
        _nameCopied = false;/*  www .  j av a2  s. c o  m*/
        JsonToken t = _nextToken;
        _nextToken = null;
        _currToken = t;
        if (t == JsonToken.VALUE_NUMBER_INT) {
            return getIntValue();
        }
        if (t == JsonToken.START_ARRAY) {
            _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
        } else if (t == JsonToken.START_OBJECT) {
            _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol);
        }
        return defaultValue;
    }
    // !!! TODO: optimize this case as well
    return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getIntValue() : defaultValue;
}