Package com.fasterxml.jackson.databind

Examples of com.fasterxml.jackson.databind.JsonNode


        public Edge edgeFromJson(final JsonNode json, final Vertex out, final Vertex in) throws IOException {
            final Map<String, Object> props = GraphSONUtility.readProperties(json);

            final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokens._ID));
            final JsonNode nodeLabel = json.get(GraphSONTokens._LABEL);
            final String label = nodeLabel == null ? EMPTY_STRING : nodeLabel.textValue();

            final Edge e = out.addEdge(label, in, T.id, edgeId);
            for (Map.Entry<String, Object> entry : props.entrySet()) {
                e.property(entry.getKey(), entry.getValue());
            }
View Full Code Here


            final Optional<String> language =  (null == languageParms || languageParms.size() == 0) ?
                    Optional.empty() : Optional.ofNullable(languageParms.get(0));

            return Triplet.with(script, bindings, language);
        } else {
            final JsonNode body;
            try {
                body = mapper.readTree(request.content().toString(CharsetUtil.UTF_8));
            } catch (IOException ioe) {
                throw new IllegalArgumentException("body could not be parsed", ioe);
            }

            final JsonNode scriptNode = body.get(Tokens.ARGS_GREMLIN);
            if (null == scriptNode) throw new IllegalArgumentException("no gremlin script supplied");

            final JsonNode bindingsNode = body.get(Tokens.ARGS_BINDINGS);
            if (bindingsNode != null && !bindingsNode.isObject()) throw new IllegalArgumentException("bindings must be a Map");

            final Map<String,Object> bindings = new HashMap<>();
            if (bindingsNode != null)
                bindingsNode.fields().forEachRemaining(kv -> bindings.put(kv.getKey(), fromJsonNode(kv.getValue())));

            final JsonNode languageNode = body.get(Tokens.ARGS_LANGUAGE);
            final Optional<String> language =  null == languageNode ?
                    Optional.empty() : Optional.ofNullable(languageNode.asText());

            return Triplet.with(scriptNode.asText(), bindings, language);
        }
    }
View Full Code Here

*
* @author Jonathan Halterman
*/
public class JsonNodeValueReader implements ValueReader<JsonNode> {
  public Object get(JsonNode source, String memberName) {
    JsonNode propertyNode = source.get(memberName);
    if (propertyNode == null)
      throw new IllegalArgumentException();

    switch (propertyNode.getNodeType()) {
      case BOOLEAN:
        return propertyNode.asBoolean();
      case NULL:
        return null;
      case NUMBER:
        return ((NumericNode) propertyNode).numberValue();
      case POJO:
        return ((POJONode) propertyNode).getPojo();
      case STRING:
        return propertyNode.asText();
      case BINARY:
        try {
          return propertyNode.binaryValue();
        } catch (IOException ignore) {
          return null;
        }

      case ARRAY:
View Full Code Here

        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }

    private static String stringValue(final JsonNode json, final String fieldName) {
        if (json != null) {
            final JsonNode value = json.get(fieldName);

            if (value != null) {
                return value.asText();
            }
        }
        return null;
    }
View Full Code Here

        return null;
    }

    private static long longValue(final JsonNode json, final String fieldName) {
        if (json != null) {
            final JsonNode value = json.get(fieldName);

            if (value != null) {
                return value.asLong(-1L);
            }
        }
        return -1L;
    }
View Full Code Here

        return -1L;
    }

    private static int intValue(final JsonNode json, final String fieldName) {
        if (json != null) {
            final JsonNode value = json.get(fieldName);

            if (value != null) {
                return value.asInt(-1);
            }
        }
        return -1;
    }
View Full Code Here

        return -1;
    }

    private static double doubleValue(final JsonNode json, final String fieldName) {
        if (json != null) {
            final JsonNode value = json.get(fieldName);

            if (value != null) {
                return value.asDouble(-1.0);
            }
        }
        return -1.0;
    }
View Full Code Here

    @Override
    public Message decode(@Nonnull final RawMessage rawMessage) {
        final GELFMessage gelfMessage = new GELFMessage(rawMessage.getPayload());
        final String json = gelfMessage.getJSON();

        final JsonNode node;

        try {
            node = objectMapper.readTree(json);
        } catch (final Exception e) {
            log.error("Could not parse JSON!", 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();
            final JsonNode value = entry.getValue();

            // 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)) {
                continue;
            }

            // Convert JSON containers to Strings, and pick a suitable number representation.
            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;
            }
View Full Code Here

    public SimulationPlan create(File jsonFile) throws Exception {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.enable(Feature.ALLOW_COMMENTS);
        ObjectMapper mapper = new ObjectMapper(jsonFactory);
        JsonNode root = mapper.readTree(jsonFile);
        SimulationPlan simulation = new SimulationPlan();

        simulation.setIntervalType(SimulationPlan.IntervalType.fromText(getString(root.get("intervalType"), "minutes")));
        DateTimeService dateTimeService;

        switch (simulation.getIntervalType()) {
        case SECONDS:
            simulation.setCollectionInterval(getLong(root.get("collectionInterval"), 20L));
            simulation.setAggregationInterval(getLong(root.get("aggregationInterval"), 2500L));
            simulation.setMetricsServerConfiguration(createSecondsConfiguration());
            simulation.setMetricsReportInterval(getInt(root.get("metricsReportInterval"), 30));
            simulation.setSimulationRate(1440);
            dateTimeService = new SecondsDateTimeService();
            break;
        case MINUTES:
            simulation.setCollectionInterval(getLong(root.get("collectionInterval"), 1250L));
            simulation.setAggregationInterval(getLong(root.get("aggregationInterval"), 150000L));
            simulation.setMetricsServerConfiguration(createMinutesConfiguration());
            simulation.setMetricsReportInterval(getInt(root.get("metricsReportInterval"), 180));
            simulation.setDateTimeService(new MinutesDateTimeService());
            simulation.setSimulationRate(2400);
            dateTimeService = new MinutesDateTimeService();
            break;
        default// HOURS
            simulation.setCollectionInterval(getLong(root.get("collectionInterval"), 30000L));
            simulation.setAggregationInterval(3600000L);
            simulation.setMetricsServerConfiguration(new MetricsConfiguration());
            simulation.setMetricsReportInterval(getInt(root.get("metricsReportInterval"), 1200));
            simulation.setSimulationRate(1000);
            dateTimeService = new DateTimeService();
        }

        simulation.setSimulationType(SimulationPlan.SimulationType.fromText(getString(root.get("simulationType"),
            "threaded")));
        if (SimulationType.SEQUENTIAL.equals(simulation.getSimulationType())) {
            dateTimeService = new SimulatedDateTimeService();
        }

        dateTimeService.setConfiguration(simulation.getMetricsServerConfiguration());
        simulation.setDateTimeService(dateTimeService);

        simulation.setNumMeasurementCollectors(getInt(root.get("numMeasurementCollectors"), 5));
        simulation.setNumReaders(getInt(root.get("numReaders"), 1));
        simulation.setReaderThreadPoolSize(getInt(root.get("readerThreadPoolSize"), 1));
        simulation.setSimulationTime(getInt(root.get("simulationTime"), 10));
        simulation.setBatchSize(getInt(root.get("batchSize"), 5000));

        String[] nodes;
        if (root.get("nodes") == null || root.get("nodes").size() == 0) {
            nodes = new String[] {InetAddress.getLocalHost().getHostAddress()};
        } else {
            nodes = new String[root.get("nodes").size()];
            int i = 0;
            for (JsonNode node : root.get("nodes")) {
                nodes[i++] = node.asText();
            }
        }
        simulation.setNodes(nodes);

        simulation.setCqlPort(getInt(root.get("cqlPort"), 9142));
        simulation.setAggregationBatchSize(getInt(root.get("aggregationBatchSize"), 250));
        simulation.setAggregationType(SimulationPlan.AggregationType.fromText(getString(root.get("aggregationType"),
            "sync")));
        simulation.setAggregationEnabled(getBoolean(root.get("aggregationEnabled"), true));


        return simulation;
    }
View Full Code Here

    @Override
    public Link deserialize(JsonParser jsonParser,
                            DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        String rel = node.fieldNames().next();
        String href = node.elements().next().get("href").textValue();


        return new Link(rel,href);
    }
View Full Code Here

TOP

Related Classes of com.fasterxml.jackson.databind.JsonNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.