List of usage examples for com.fasterxml.jackson.databind JsonNode asLong
public long asLong()
From source file:org.apache.syncope.core.misc.serialization.SyncTokenDeserializer.java
@Override public SyncToken deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); Object value = null;/*from ww w . j a v a 2 s .c om*/ if (tree.has("value")) { JsonNode node = tree.get("value"); value = node.isNull() ? null : node.isBoolean() ? node.asBoolean() : node.isDouble() ? node.asDouble() : node.isLong() ? node.asLong() : node.isInt() ? node.asInt() : node.asText(); if (value instanceof String) { String base64 = (String) value; if (Base64.isBase64(base64)) { value = Base64.decodeBase64(base64); } } } return new SyncToken(value); }
From source file:org.apache.syncope.core.provisioning.api.serialization.AttributeDeserializer.java
@Override public Attribute deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); String name = tree.get("name").asText(); List<Object> values = new ArrayList<>(); for (JsonNode node : tree.get("value")) { if (node.isNull()) { values.add(null);//w ww . j a va 2 s .c o m } else if (node.isObject()) { values.add(((ObjectNode) node).traverse(jp.getCodec()).readValueAs(GuardedString.class)); } else if (node.isBoolean()) { values.add(node.asBoolean()); } else if (node.isDouble()) { values.add(node.asDouble()); } else if (node.isLong()) { values.add(node.asLong()); } else if (node.isInt()) { values.add(node.asInt()); } else { String text = node.asText(); if (text.startsWith(AttributeSerializer.BYTE_ARRAY_PREFIX) && text.endsWith(AttributeSerializer.BYTE_ARRAY_SUFFIX)) { values.add(Base64.getDecoder().decode(StringUtils.substringBetween(text, AttributeSerializer.BYTE_ARRAY_PREFIX, AttributeSerializer.BYTE_ARRAY_SUFFIX))); } else { values.add(text); } } } return Uid.NAME.equals(name) ? new Uid(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString()) : Name.NAME.equals(name) ? new Name(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString()) : AttributeBuilder.build(name, values); }
From source file:org.apache.syncope.core.util.AttributeDeserializer.java
@Override public Attribute deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException, JsonProcessingException { ObjectNode tree = jp.readValueAsTree(); String name = tree.get("name").asText(); List<Object> values = new ArrayList<Object>(); for (Iterator<JsonNode> itor = tree.get("value").iterator(); itor.hasNext();) { JsonNode node = itor.next(); if (node.isNull()) { values.add(null);//w w w . j a va 2 s . co m } else if (node.isObject()) { values.add(((ObjectNode) node).traverse(jp.getCodec()).readValueAs(GuardedString.class)); } else if (node.isBoolean()) { values.add(node.asBoolean()); } else if (node.isDouble()) { values.add(node.asDouble()); } else if (node.isLong()) { values.add(node.asLong()); } else if (node.isInt()) { values.add(node.asInt()); } else { String text = node.asText(); if (text.startsWith(AttributeSerializer.BYTE_ARRAY_PREFIX) && text.endsWith(AttributeSerializer.BYTE_ARRAY_SUFFIX)) { values.add(Base64.decode(StringUtils.substringBetween(text, AttributeSerializer.BYTE_ARRAY_PREFIX, AttributeSerializer.BYTE_ARRAY_SUFFIX))); } else { values.add(text); } } } return Uid.NAME.equals(name) ? new Uid(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString()) : Name.NAME.equals(name) ? new Name(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString()) : AttributeBuilder.build(name, values); }
From source file:org.flowable.form.engine.impl.cmd.AbstractGetFormInstanceModelCmd.java
public void fillVariablesWithFormValues(Map<String, JsonNode> submittedFormFieldMap, List<FormField> allFields) { for (FormField field : allFields) { JsonNode fieldValueNode = submittedFormFieldMap.get(field.getId()); if (fieldValueNode == null || fieldValueNode.isNull()) { continue; }/*from w w w . j a va 2 s .co m*/ String fieldType = field.getType(); String fieldValue = fieldValueNode.asText(); if (FormFieldTypes.DATE.equals(fieldType)) { try { if (StringUtils.isNotEmpty(fieldValue)) { LocalDate dateValue = LocalDate.parse(fieldValue); variables.put(field.getId(), dateValue.toString("d-M-yyyy")); } } catch (Exception e) { LOGGER.error("Error parsing form date value for process instance {} and task {} with value {}", processInstanceId, taskId, fieldValue, e); } } else if (fieldValueNode.isBoolean()) { variables.put(field.getId(), fieldValueNode.asBoolean()); } else if (fieldValueNode.isLong()) { variables.put(field.getId(), fieldValueNode.asLong()); } else if (fieldValueNode.isDouble()) { variables.put(field.getId(), fieldValueNode.asDouble()); } else { variables.put(field.getId(), fieldValue); } } }
From source file:org.graylog2.inputs.codecs.GelfCodec.java
@Nullable @Override//from ww w.j a v a2 s. c o m public Message decode(@Nonnull final RawMessage rawMessage) { final GELFMessage gelfMessage = new GELFMessage(rawMessage.getPayload(), rawMessage.getRemoteAddress()); final String json = gelfMessage.getJSON(decompressSizeLimit); final JsonNode node; try { node = objectMapper.readTree(json); } catch (final Exception e) { log.error("Could not parse JSON, first 400 characters: " + StringUtils.abbreviate(json, 403), e); throw new IllegalStateException("JSON is null/could not be parsed (invalid JSON)", e); } // Timestamp. final double messageTimestamp = doubleValue(node, "timestamp"); final DateTime timestamp; if (messageTimestamp <= 0) { timestamp = rawMessage.getTimestamp(); } else { // we treat this as a unix timestamp timestamp = Tools.dateTimeFromDouble(messageTimestamp); } final Message message = new Message(stringValue(node, "short_message"), stringValue(node, "host"), timestamp); message.addField("full_message", stringValue(node, "full_message")); final String file = stringValue(node, "file"); if (file != null && !file.isEmpty()) { message.addField("file", file); } final long line = longValue(node, "line"); if (line > -1) { message.addField("line", line); } // Level is set by server if not specified by client. final int level = intValue(node, "level"); if (level > -1) { message.addField("level", level); } // Facility is set by server if not specified by client. final String facility = stringValue(node, ("facility")); if (facility != null && !facility.isEmpty()) { message.addField("facility", facility); } // Add additional data if there is some. final Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); while (fields.hasNext()) { final Map.Entry<String, JsonNode> entry = fields.next(); String key = entry.getKey(); // Do not index useless GELF "version" field. if ("version".equals(key)) { continue; } // Don't include GELF syntax underscore in message field key. if (key.startsWith("_") && key.length() > 1) { key = key.substring(1); } // We already set short_message and host as message and source. Do not add as fields again. if (key.equals("short_message") || key.equals("host")) { continue; } // Skip standard or already set fields. if (message.getField(key) != null || (Message.RESERVED_FIELDS.contains(key) && !Message.RESERVED_SETTABLE_FIELDS.contains(key))) { continue; } // Convert JSON containers to Strings, and pick a suitable number representation. final JsonNode value = entry.getValue(); final Object fieldValue; if (value.isContainerNode()) { fieldValue = value.toString(); } else if (value.isFloatingPointNumber()) { fieldValue = value.asDouble(); } else if (value.isIntegralNumber()) { fieldValue = value.asLong(); } else if (value.isNull()) { log.debug("Field [{}] is NULL. Skipping.", key); continue; } else if (value.isTextual()) { fieldValue = value.asText(); } else { log.debug("Field [{}] has unknown value type. Skipping.", key); continue; } message.addField(key, fieldValue); } return message; }
From source file:org.pentaho.pdi.engine.serializers.DataEventSerializer.java
public DataEventSerializer() { super(DataEvent.class); SimpleModule module = new SimpleModule(); module.addSerializer(DataEvent.class, new JsonSerializer<DataEvent>() { @Override//w ww.j a va 2 s .c om public void serialize(DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField("model-id", dataEvent.getSource().getId()); jsonGenerator.writeStringField("type", rows.getType().toString()); jsonGenerator.writeStringField("state", rows.getState().toString()); jsonGenerator.writeArrayFieldStart("rows"); for (Row row : rows) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart("names"); for (String name : row.getColumnNames()) { jsonGenerator.writeString(name); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart("objects"); for (Object obj : row.getObjects().get()) { jsonGenerator.writeStartObject(); if (obj == null) { jsonGenerator.writeStringField("type", "Null"); jsonGenerator.writeEndObject(); continue; } switch (obj.getClass().getSimpleName()) { case "String": jsonGenerator.writeStringField("type", "String"); jsonGenerator.writeStringField("obj", obj.toString()); break; case "Date": jsonGenerator.writeStringField("type", "Date"); jsonGenerator.writeStringField("obj", DATE_TIME_INSTANCE.format((Date) obj)); break; case "Integer": jsonGenerator.writeStringField("type", "Integer"); jsonGenerator.writeNumberField("obj", (Integer) obj); break; case "Long": jsonGenerator.writeStringField("type", "Long"); jsonGenerator.writeNumberField("obj", (Long) obj); break; case "Double": jsonGenerator.writeStringField("type", "Double"); jsonGenerator.writeNumberField("obj", (Double) obj); break; case "BigDecimal": jsonGenerator.writeStringField("type", "BigDecimal"); jsonGenerator.writeStringField("obj", obj.toString()); break; case "Boolean": jsonGenerator.writeStringField("type", "Boolean"); jsonGenerator.writeBooleanField("obj", (Boolean) obj); break; case "byte[]": jsonGenerator.writeStringField("type", "byte[]"); jsonGenerator.writeStringField("obj", new String(((byte[]) obj), "UTF-8")); break; default: if (obj instanceof Serializable) { jsonGenerator.writeStringField("type", "Object"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(obj); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField("obj", Base64.encodeBase64String(bytes)); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } }); module.addDeserializer(DataEvent.class, new StdNodeBasedDeserializer<DataEvent>(DataEvent.class) { @Override public DataEvent convert(JsonNode jsonNode, DeserializationContext deserializationContext) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf(jsonNode.get("type").asText()); Rows.STATE state = Rows.STATE.valueOf(jsonNode.get("state").asText()); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get("rows"); for (JsonNode row : json_rows) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for (JsonNode name : row.get("names")) { names.add(name.asText()); } List<Object> objects = new ArrayList<>(); for (JsonNode obj : row.get("objects")) { JsonNode t = obj.get("type"); JsonNode rawObject = obj.get("obj"); Object object = null; String objType = t.asText(); switch (objType) { case "Null": types.add(Void.class); break; case "String": types.add(String.class); object = rawObject.asText(); break; case "Integer": types.add(Integer.class); object = rawObject.asInt(); break; case "Long": types.add(Long.class); object = rawObject.asLong(); break; case "Date": types.add(Date.class); try { object = DATE_TIME_INSTANCE.parse(rawObject.asText()); } catch (ParseException e) { e.printStackTrace(); } break; case "Double": types.add(Double.class); object = rawObject.asDouble(); break; case "BigDecimal": types.add(BigDecimal.class); object = new BigDecimal(rawObject.asText()); break; case "Boolean": types.add(Boolean.class); object = rawObject.asBoolean(); break; case "byte[]": types.add(byte[].class); object = rawObject.asText().getBytes("UTF-8"); break; case "Object": try { types.add(Object.class); object = new ObjectInputStream( new ByteArrayInputStream(Base64.decodeBase64(rawObject.asText()))) .readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } break; } objects.add(object); } Row r = new DeserializedRow(names, types, objects); rows.add(r); } Rows rowsObj = new Rows(rows, type, state); return new DataEvent(new RemoteSource(jsonNode.get("model-id").asText()), rowsObj); } }); mapper.registerModule(module); }
From source file:us.ihmc.idl.serializers.extra.JacksonInterchangeSerializer.java
/** * Sequence//ww w . j a va 2 s . com */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void read_type_e(String name, IDLSequence seq) { checkArraySupport(); seq.resetQuick(); JsonNode child = node.path(name); if (child.isArray()) { for (JsonNode val : child) { if (seq instanceof IDLSequence.Boolean) { ((IDLSequence.Boolean) seq).add(val.asBoolean()); } else if (seq instanceof IDLSequence.Byte) { ((IDLSequence.Byte) seq).add((byte) val.asInt()); } else if (seq instanceof IDLSequence.Char) { ((IDLSequence.Char) seq).add(str2char(val.asText())); } else if (seq instanceof IDLSequence.Short) { ((IDLSequence.Short) seq).add((short) val.asInt()); } else if (seq instanceof IDLSequence.Integer) { ((IDLSequence.Integer) seq).add(val.asInt()); } else if (seq instanceof IDLSequence.Long) { ((IDLSequence.Long) seq).add(val.asLong()); } else if (seq instanceof IDLSequence.Float) { ((IDLSequence.Float) seq).add((float) val.asDouble()); } else if (seq instanceof IDLSequence.Double) { ((IDLSequence.Double) seq).add(val.asDouble()); } else if (seq instanceof IDLSequence.StringBuilderHolder) { ((IDLSequence.StringBuilderHolder) seq).add(val.asText()); } else if (seq instanceof IDLSequence.Object) { if (((IDLSequence.Object) seq).isEnum()) { ((IDLSequence.Object) seq) .add(str2enum(((IDLSequence.Object) seq).getEnumConstants(), val.asText())); } else { TopicDataType<Object> type = ((IDLSequence.Object) seq).getTopicDataType(); Object childStruct = ((IDLSequence.Object) seq).add(); if (val.isObject()) { type.deserialize(new JacksonInterchangeSerializer((ObjectNode) val, supportsArrays), childStruct); } } } else { throw new RuntimeException("Unknown sequence object"); } } } else { return; } }
From source file:us.ihmc.idl.serializers.extra.JacksonInterchangeSerializer.java
private void read_array(long[] arr, JsonNode child) { for (int i = 0; i < arr.length; i++) { JsonNode element = child.get(i); if (element != null) { arr[i] = element.asLong(); } else {// www. j a va 2 s .c om arr[i] = 0L; } } }