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);
}
}
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;
}
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());
}
}
}
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();
}
}
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();
}
}
Aggregations