Search in sources :

Example 36 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project spf4j by zolyfarkas.

the class SampleNode method parseInto.

public static void parseInto(final JsonParser jsonP, final SampleNode parentNode) throws IOException {
    consume(jsonP, JsonToken.FIELD_NAME);
    String name = jsonP.getCurrentName();
    consume(jsonP, JsonToken.VALUE_NUMBER_INT);
    int sc = jsonP.getIntValue();
    JsonToken nextToken = jsonP.nextToken();
    Method method = Methods.from(name);
    SampleNode sn = parentNode.get(method);
    if (sn == null) {
        sn = new SampleNode(sc);
        parentNode.put(method, sn);
    } else {
        sn.sampleCount += sc;
    }
    if (nextToken == JsonToken.END_OBJECT) {
        return;
    } else if (nextToken == JsonToken.FIELD_NAME) {
        consume(jsonP, JsonToken.START_ARRAY);
        while (jsonP.nextToken() != JsonToken.END_ARRAY) {
            parseInto(jsonP, sn);
        }
        consume(jsonP, JsonToken.END_OBJECT);
    } else {
        throw new JsonParseException(jsonP, "Expected field name or end Object, not: " + nextToken);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) Method(org.spf4j.base.avro.Method) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 37 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project jaffa-framework by jaffa-projects.

the class RESTfulServiceHelper method parseJsonFields.

public static Map<String, Object> parseJsonFields(String jsonString, Map<String, Class> validFieldNames) throws IOException {
    Map<String, Object> input = new HashMap<String, Object>();
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(jsonString);
    while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();
        if (JsonToken.FIELD_NAME.equals(jsonToken)) {
            String fieldName = parser.getCurrentName();
            if (validFieldNames.keySet().contains(fieldName)) {
                jsonToken = parser.nextToken();
                Class clazz = validFieldNames.get(fieldName);
                if (clazz.isAssignableFrom(Boolean.class) || clazz.isAssignableFrom(boolean.class)) {
                    input.put(fieldName, parser.getValueAsBoolean());
                } else if (clazz.isAssignableFrom(String.class)) {
                    input.put(fieldName, parser.getValueAsString());
                } else if (clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(int.class)) {
                    input.put(fieldName, parser.getValueAsInt());
                } else if (clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(double.class)) {
                    input.put(fieldName, parser.getValueAsDouble());
                } else if (clazz.isAssignableFrom(Long.class) || clazz.isAssignableFrom(long.class)) {
                    input.put(fieldName, parser.getValueAsLong());
                } else if (clazz.isAssignableFrom(DateTime.class)) {
                    if (jsonToken.isStructStart()) {
                        while (!jsonToken.isStructEnd()) {
                            jsonToken = parser.nextToken();
                            if (JsonToken.FIELD_NAME.equals(jsonToken)) {
                                jsonToken = parser.nextToken();
                                input.put(fieldName, new DateTime(parser.getValueAsLong()));
                            }
                        }
                    } else {
                        try {
                            DateTime dateTime = DateTime.parse(parser.getValueAsString());
                            input.put(fieldName, dateTime);
                        } catch (ParseException e) {
                            throw new IOException("Cannot parse DateTime " + parser.getValueAsString(), e);
                        }
                    }
                } else if (clazz.isAssignableFrom(String[].class)) {
                    ArrayNode node = mapper.readTree(parser);
                    Iterator<JsonNode> iterator = node.elements();
                    String[] array = new String[node.size()];
                    for (int i = 0; i < node.size(); i++) {
                        if (iterator.hasNext()) {
                            array[i] = iterator.next().asText();
                        }
                    }
                    input.put(fieldName, array);
                } else if (clazz.isAssignableFrom(List.class)) {
                    ArrayNode node = mapper.readTree(parser);
                    Iterator<JsonNode> iterator = node.elements();
                    List list = new ArrayList();
                    for (int i = 0; i < node.size(); i++) {
                        if (iterator.hasNext()) {
                            JsonNode n = iterator.next();
                            if (n.isContainerNode()) {
                                Map<Object, Object> map = mapper.readValue(n.toString(), Map.class);
                                list.add(map);
                            } else {
                                if ("STRING".equals(n.getNodeType().name())) {
                                    list.add(n.textValue());
                                } else if ("NUMBER".equals(n.getNodeType().name())) {
                                    if (n.isInt()) {
                                        list.add(n.intValue());
                                    } else if (n.isDouble()) {
                                        list.add(n.doubleValue());
                                    } else if (n.isLong()) {
                                        list.add(n.longValue());
                                    } else if (n.isFloat()) {
                                        list.add(n.floatValue());
                                    } else {
                                        LOGGER.warn("Not supported type. Field name = " + fieldName + ", Value = " + parser.getText() + ", Type = " + clazz.getTypeName());
                                        list.add(n);
                                    }
                                } else if ("BOOLEAN".equals(n.getNodeType().name())) {
                                    list.add(n.booleanValue());
                                } else {
                                    LOGGER.warn("Not supported type. Field name = " + fieldName + ", Value = " + parser.getText() + ", Type = " + clazz.getTypeName());
                                    list.add(n);
                                }
                            }
                        }
                    }
                    input.put(fieldName, list);
                } else {
                    LOGGER.warn("Not supported type. Field name = " + fieldName + ", Value = " + parser.getText() + ", Type = " + clazz.getTypeName());
                }
            }
        }
    }
    return input;
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DateTime(org.jaffa.datatypes.DateTime) JsonToken(com.fasterxml.jackson.core.JsonToken) ParseException(java.text.ParseException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 38 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project keycloak by keycloak.

the class RealmsConfigurationLoader method readRealmRoles.

private static void readRealmRoles(RealmRepresentation r, JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t != JsonToken.START_ARRAY) {
        throw new RuntimeException("Expected start_of_array on 'roles/realm' [" + t + "]");
    }
    t = p.nextToken();
    int count = 0;
    while (t == JsonToken.START_OBJECT) {
        RoleRepresentation u = p.readValueAs(RoleRepresentation.class);
        if (!seeking() || !skipRealmRoles) {
            enqueueCreateRealmRole(r, u);
        }
        t = p.nextToken();
        count += 1;
        // in order to short-circuit if any errors have occurred
        if (count % ERROR_CHECK_INTERVAL == 0) {
            checkPendingErrors(u.getName());
        }
    }
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 39 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project keycloak by keycloak.

the class RealmsConfigurationLoader method readRealms.

private static void readRealms(JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    while (t != JsonToken.END_OBJECT && t != JsonToken.END_ARRAY) {
        if (t != JsonToken.START_ARRAY) {
            readRealm(p);
            currentRealm += 1;
        }
        t = p.nextToken();
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 40 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project keycloak by keycloak.

the class RealmsConfigurationLoader method readClientRoles.

private static void readClientRoles(RealmRepresentation r, JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t != JsonToken.START_OBJECT) {
        throw new RuntimeException("Expected start_of_object on 'roles/client' [" + t + "]");
    }
    t = p.nextToken();
    int count = 0;
    while (t == JsonToken.FIELD_NAME) {
        String client = p.getCurrentName();
        t = p.nextToken();
        if (t != JsonToken.START_ARRAY) {
            throw new RuntimeException("Expected start_of_array on 'roles/client/" + client + " [" + t + "]");
        }
        t = p.nextToken();
        while (t != JsonToken.END_ARRAY) {
            RoleRepresentation u = p.readValueAs(RoleRepresentation.class);
            if (!seeking() || !skipClientRoles) {
                enqueueCreateClientRole(r, u, client);
            }
            t = p.nextToken();
            count += 1;
            // in order to short-circuit if any errors have occurred
            if (count % ERROR_CHECK_INTERVAL == 0) {
                checkPendingErrors(u.getName());
            }
        }
        t = p.nextToken();
    }
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) JsonToken(com.fasterxml.jackson.core.JsonToken)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)251 IOException (java.io.IOException)65 JsonParser (com.fasterxml.jackson.core.JsonParser)44 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 JsonFactory (com.fasterxml.jackson.core.JsonFactory)18 JsonParseException (com.fasterxml.jackson.core.JsonParseException)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 SqlNullable (com.facebook.presto.spi.function.SqlNullable)6 SqlType (com.facebook.presto.spi.function.SqlType)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 InputStream (java.io.InputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ByteString (com.google.protobuf.ByteString)4 HashSet (java.util.HashSet)4 MappingJsonFactory (com.fasterxml.jackson.databind.MappingJsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3