Search in sources :

Example 1 with JsonToken

use of org.codehaus.jackson.JsonToken in project neo4j by neo4j.

the class StatementDeserializer method beginsWithCorrectTokens.

private boolean beginsWithCorrectTokens() throws IOException {
    List<JsonToken> expectedTokens = asList(START_OBJECT, FIELD_NAME, START_ARRAY);
    String expectedField = "statements";
    List<JsonToken> foundTokens = new ArrayList<>();
    for (int i = 0; i < expectedTokens.size(); i++) {
        JsonToken token = input.nextToken();
        if (i == 0 && token == null) {
            return false;
        }
        if (token == FIELD_NAME && !expectedField.equals(input.getText())) {
            addError(new Neo4jError(Status.Request.InvalidFormat, new DeserializationException(String.format("Unable to deserialize request. " + "Expected first field to be '%s', but was '%s'.", expectedField, input.getText()))));
            return false;
        }
        foundTokens.add(token);
    }
    if (!expectedTokens.equals(foundTokens)) {
        addError(new Neo4jError(Status.Request.InvalidFormat, new DeserializationException(String.format("Unable to deserialize request. " + "Expected %s, found %s.", expectedTokens, foundTokens))));
        return false;
    }
    return true;
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ArrayList(java.util.ArrayList) JsonToken(org.codehaus.jackson.JsonToken)

Example 2 with JsonToken

use of org.codehaus.jackson.JsonToken in project neo4j by neo4j.

the class BatchOperations method parseAndPerform.

protected void parseAndPerform(UriInfo uriInfo, HttpHeaders httpHeaders, HttpServletRequest req, InputStream body, Map<Integer, String> locations) throws IOException, ServletException {
    JsonParser jp = jsonFactory.createJsonParser(body);
    JsonToken token;
    RequestData requestData = RequestData.from(req);
    while ((token = jp.nextToken()) != null) {
        if (token == JsonToken.START_OBJECT) {
            String jobMethod = "", jobPath = "", jobBody = "";
            Integer jobId = null;
            while ((token = jp.nextToken()) != JsonToken.END_OBJECT && token != null) {
                String field = jp.getText();
                jp.nextToken();
                switch(field) {
                    case METHOD_KEY:
                        jobMethod = jp.getText().toUpperCase();
                        break;
                    case TO_KEY:
                        jobPath = jp.getText();
                        break;
                    case ID_KEY:
                        jobId = jp.getIntValue();
                        break;
                    case BODY_KEY:
                        jobBody = readBody(jp);
                        break;
                    default:
                        break;
                }
            }
            // Read one job description. Execute it.
            performRequest(uriInfo, jobMethod, jobPath, jobBody, jobId, httpHeaders, locations, requestData);
        }
    }
}
Also used : RequestData(org.neo4j.server.rest.web.InternalJettyServletRequest.RequestData) JsonToken(org.codehaus.jackson.JsonToken) JsonParser(org.codehaus.jackson.JsonParser)

Example 3 with JsonToken

use of org.codehaus.jackson.JsonToken in project OpenRefine by OpenRefine.

the class Cell method loadStreaming.

public static Cell loadStreaming(JsonParser jp, Pool pool) throws Exception {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
        return null;
    }
    Serializable value = null;
    String type = null;
    Recon recon = null;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        jp.nextToken();
        if ("r".equals(fieldName)) {
            if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
                String reconID = jp.getText();
                recon = pool.getRecon(reconID);
            } else {
                // legacy
                recon = Recon.loadStreaming(jp, pool);
            }
        } else if ("e".equals(fieldName)) {
            value = new EvalError(jp.getText());
        } else if ("v".equals(fieldName)) {
            JsonToken token = jp.getCurrentToken();
            if (token == JsonToken.VALUE_STRING) {
                value = jp.getText();
            } else if (token == JsonToken.VALUE_NUMBER_INT) {
                value = jp.getLongValue();
            } else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
                value = jp.getDoubleValue();
            } else if (token == JsonToken.VALUE_TRUE) {
                value = true;
            } else if (token == JsonToken.VALUE_FALSE) {
                value = false;
            }
        } else if ("t".equals(fieldName)) {
            type = jp.getText();
        }
    }
    if (value != null) {
        if (type != null && "date".equals(type)) {
            value = ParsingUtilities.stringToDate((String) value);
        }
        return new Cell(value, recon);
    } else {
        return null;
    }
}
Also used : Serializable(java.io.Serializable) JsonToken(org.codehaus.jackson.JsonToken) EvalError(com.google.refine.expr.EvalError)

Example 4 with JsonToken

use of org.codehaus.jackson.JsonToken in project OpenRefine by OpenRefine.

the class Recon method loadStreaming.

public static Recon loadStreaming(JsonParser jp, Pool pool) throws Exception {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
        return null;
    }
    Recon recon = null;
    long id = -1;
    long judgmentHistoryEntry = -1;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        jp.nextToken();
        if ("id".equals(fieldName)) {
            id = jp.getLongValue();
        } else if ("judgmentHistoryEntry".equals(fieldName)) {
            judgmentHistoryEntry = jp.getLongValue();
            if (recon != null) {
                recon.judgmentHistoryEntry = judgmentHistoryEntry;
            }
        } else {
            if (recon == null) {
                recon = new Recon(id, judgmentHistoryEntry);
            }
            if ("j".equals(fieldName)) {
                recon.judgment = stringToJudgment(jp.getText());
            } else if ("m".equals(fieldName)) {
                if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
                    // legacy case
                    String candidateID = jp.getText();
                    recon.match = pool.getReconCandidate(candidateID);
                } else {
                    recon.match = ReconCandidate.loadStreaming(jp);
                }
            } else if ("f".equals(fieldName)) {
                if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
                    return null;
                }
                int feature = 0;
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    if (feature < recon.features.length) {
                        JsonToken token = jp.getCurrentToken();
                        if (token == JsonToken.VALUE_STRING) {
                            recon.features[feature++] = jp.getText();
                        } else if (token == JsonToken.VALUE_NUMBER_INT) {
                            recon.features[feature++] = jp.getLongValue();
                        } else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
                            recon.features[feature++] = jp.getDoubleValue();
                        } else if (token == JsonToken.VALUE_FALSE) {
                            recon.features[feature++] = false;
                        } else if (token == JsonToken.VALUE_TRUE) {
                            recon.features[feature++] = true;
                        }
                    }
                }
            } else if ("c".equals(fieldName)) {
                if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
                    return null;
                }
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
                        // legacy case
                        String candidateID = jp.getText();
                        recon.addCandidate(pool.getReconCandidate(candidateID));
                    } else {
                        recon.addCandidate(ReconCandidate.loadStreaming(jp));
                    }
                }
            } else if ("service".equals(fieldName)) {
                recon.service = jp.getText();
            } else if ("identifierSpace".equals(fieldName)) {
                recon.identifierSpace = jp.getText();
                if ("null".equals(recon.identifierSpace)) {
                    recon.identifierSpace = FREEBASE_IDENTIFIER_SPACE;
                }
            } else if ("schemaSpace".equals(fieldName)) {
                recon.schemaSpace = jp.getText();
                if ("null".equals(recon.schemaSpace)) {
                    recon.schemaSpace = FREEBASE_SCHEMA_SPACE;
                }
            } else if ("judgmentAction".equals(fieldName)) {
                recon.judgmentAction = jp.getText();
            } else if ("judgmentBatchSize".equals(fieldName)) {
                recon.judgmentBatchSize = jp.getIntValue();
            } else if ("matchRank".equals(fieldName)) {
                recon.matchRank = jp.getIntValue();
            }
        }
    }
    return recon;
}
Also used : JsonToken(org.codehaus.jackson.JsonToken)

Example 5 with JsonToken

use of org.codehaus.jackson.JsonToken in project spring-security-oauth by spring-projects.

the class JacksonArrayOrStringDeserializer method deserialize.

@Override
public Set<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonToken token = jp.getCurrentToken();
    if (token.isScalarValue()) {
        String list = jp.getText();
        list = list.replaceAll("\\s+", ",");
        return new LinkedHashSet<String>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(list)));
    }
    return jp.readValueAs(new TypeReference<Set<String>>() {
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) JsonToken(org.codehaus.jackson.JsonToken)

Aggregations

JsonToken (org.codehaus.jackson.JsonToken)19 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 JsonParseException (org.codehaus.jackson.JsonParseException)3 JsonParser (org.codehaus.jackson.JsonParser)3 TypeDeserializer (org.codehaus.jackson.map.TypeDeserializer)3 LinkedHashMap (java.util.LinkedHashMap)2 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)2 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)2 BaseCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo)2 HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)2 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)2 Neo4jError (org.neo4j.server.rest.transactional.error.Neo4jError)2 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)2 EvalError (com.google.refine.expr.EvalError)1 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)1 GwtTestJSONException (com.googlecode.gwt.test.exceptions.GwtTestJSONException)1 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1