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

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

Introduction

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

Prototype

JsonToken VALUE_NULL

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

Click Source Link

Document

VALUE_NULL is returned when encountering literal "null" in value context

Usage

From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java

/**
 * Parses the properties of a feature/*  w  ww  . j  a v  a 2 s. c  o  m*/
 *
 * Syntax:
 *
 * "properties": {"prop0": "value0"}
 *
 * @param jsParser
 */
private void parseProperties(JsonParser jp, int fieldIndex) throws IOException, SQLException {
    jp.nextToken();//START_OBJECT {
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        JsonToken value = jp.nextToken();
        if (value == JsonToken.VALUE_STRING) {
            getPreparedStatement().setObject(fieldIndex, jp.getText());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_TRUE) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_FALSE) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_FLOAT) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsDouble());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_INT) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsInt());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NULL) {
            getPreparedStatement().setObject(fieldIndex, null);
            fieldIndex++;
        } else {
            //ignore other value
        }
    }

}

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

public final Audio.Builder readAudio(JsonParser par) throws IOException {
    Audio.Builder audio = Audio.newBuilder();
    for (startObject(par); endObject(par); par.nextToken()) {
        String fieldName = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readAudioField(par, audio, fieldName);
        }//from w ww . j av  a  2s. c o m
    }
    return audio;
}

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   ww  w  .j  a v  a2  s  . c o  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.google.openrtb.json.OpenRtbJsonReader.java

public final Banner.Builder readBanner(JsonParser par) throws IOException {
    Banner.Builder banner = Banner.newBuilder();
    for (startObject(par); endObject(par); par.nextToken()) {
        String fieldName = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readBannerField(par, banner, fieldName);
        }/*from w w  w  .ja  va  2 s .  com*/
    }
    return banner;
}

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

public final Format.Builder readFormat(JsonParser par) throws IOException {
    Format.Builder format = Format.newBuilder();
    for (startObject(par); endObject(par); par.nextToken()) {
        String fieldName = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readFormatField(par, format, fieldName);
        }//from   w  ww .j a v a  2s . c o m
    }
    return format;
}

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

public final Site.Builder readSite(JsonParser par) throws IOException {
    Site.Builder site = Site.newBuilder();
    for (startObject(par); endObject(par); par.nextToken()) {
        String fieldName = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readSiteField(par, site, fieldName);
        }//from  ww  w.  j  a  v  a 2  s  . co  m
    }
    return site;
}

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

public final App.Builder readApp(JsonParser par) throws IOException {
    App.Builder app = App.newBuilder();/*  w  w w.jav  a  2s .c o  m*/
    for (startObject(par); endObject(par); par.nextToken()) {
        String name = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readAppField(par, app, name);
        }
    }
    return app;
}

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

public final Content.Builder readContent(JsonParser par) throws IOException {
    Content.Builder content = Content.newBuilder();
    for (startObject(par); endObject(par); par.nextToken()) {
        String name = getCurrentName(par);
        if (par.nextToken() != JsonToken.VALUE_NULL) {
            readContentField(par, content, name);
        }/*from   w  w w  .  jav  a  2s .  co m*/
    }
    return content;
}

From source file:com.zenesis.qx.remote.RequestHandler.java

/**
 * Reads an array from JSON, where each value is of the listed in types; EG the first element
 * is class type[0], the second element is class type[1] etc
 * @param jp/*from www  .  ja  v a  2s  .  c o m*/
 * @param types
 * @return
 * @throws IOException
 */
private Object[] readArray(JsonParser jp, Class[] types) throws IOException {
    if (jp.getCurrentToken() == JsonToken.VALUE_NULL)
        return null;

    ArrayList result = new ArrayList();
    for (int paramIndex = 0; jp.nextToken() != JsonToken.END_ARRAY; paramIndex++) {
        Class type = null;
        if (types != null && paramIndex < types.length)
            type = types[paramIndex];

        if (type != null && type.isArray()) {
            if (jp.getCurrentToken() == JsonToken.VALUE_NULL)
                result.add(null);
            else if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
                Object obj = readArray(jp, type.getComponentType());
                result.add(obj);
            } else
                throw new IllegalStateException("Expected array but found " + jp.getCurrentToken());

        } else if (type != null && Proxied.class.isAssignableFrom(type)) {
            Integer id = jp.readValueAs(Integer.class);
            if (id != null) {
                Proxied obj = getProxied(id);
                result.add(obj);
            } else
                result.add(null);

        } else if (type != null && Enum.class.isAssignableFrom(type)) {
            Object obj = jp.readValueAs(Object.class);
            if (obj != null) {
                String str = Helpers.camelCaseToEnum(obj.toString());
                obj = Enum.valueOf(type, str);
                result.add(obj);
            }
        } else {
            Object obj = jp.readValueAs(type != null ? type : Object.class);
            result.add(obj);
        }
    }
    return result.toArray(new Object[result.size()]);
}

From source file:com.zenesis.qx.remote.RequestHandler.java

/**
 * Reads an array from JSON, where each value is of the class clazz.  Note that while the result
 * is an array, you cannot assume that it is an array of Object, or use generics because generics
 * are always Objects - this is because arrays of primitive types are not arrays of Objects
 * @param jp/*from w w  w  .java 2  s  .  com*/
 * @param clazz
 * @return
 * @throws IOException
 */
private Object readArray(JsonParser jp, Class clazz) throws IOException {
    if (jp.getCurrentToken() == JsonToken.VALUE_NULL)
        return null;

    boolean isProxyClass = Proxied.class.isAssignableFrom(clazz);
    ArrayList result = new ArrayList();
    for (; jp.nextToken() != JsonToken.END_ARRAY;) {
        if (isProxyClass) {
            Integer id = jp.readValueAs(Integer.class);
            if (id != null) {
                Proxied obj = getProxied(id);
                if (obj == null)
                    log.fatal("Cannot read object of class " + clazz + " from id=" + id);
                else if (!clazz.isInstance(obj))
                    throw new ClassCastException(
                            "Cannot cast " + obj + " class " + obj.getClass() + " to " + clazz);
                else
                    result.add(obj);
            } else
                result.add(null);
        } else {
            Object obj = readSimpleValue(jp, clazz);
            result.add(obj);
        }
    }

    Object arr = Array.newInstance(clazz, result.size());
    for (int i = 0; i < result.size(); i++)
        Array.set(arr, i, result.get(i));
    return arr;
    //return result.toArray(Array.newInstance(clazz, result.size()));
}