Search in sources :

Example 1 with ReferenceList

use of org.graylog2.contentpacks.model.entities.references.ReferenceList in project graylog2-server by Graylog2.

the class ReferenceConverter method convert.

@Override
public Reference convert(JsonNode jsonNode) {
    if (jsonNode.isObject()) {
        final ImmutableSet<String> fieldNames = ImmutableSet.copyOf(jsonNode.fieldNames());
        if (fieldNames.equals(EXPECTED_FIELD_NAMES)) {
            // TODO: Possible to use ValueTypeDeserializer to avoid duplication?
            final String valueTypeText = jsonNode.path(ValueReference.FIELD_TYPE).asText();
            final ValueType valueType = ValueType.valueOf(valueTypeText.toUpperCase(Locale.ROOT));
            final JsonNode value = jsonNode.path(ValueReference.FIELD_VALUE);
            if (valueType == ValueType.BOOLEAN && value.isBoolean()) {
                return ValueReference.of(value.booleanValue());
            } else if (valueType == ValueType.DOUBLE && value.isDouble()) {
                return ValueReference.of(value.doubleValue());
            } else if (valueType == ValueType.FLOAT && value.isFloat()) {
                return ValueReference.of(value.floatValue());
            } else if (valueType == ValueType.INTEGER && value.isInt()) {
                return ValueReference.of(value.intValue());
            } else if (valueType == ValueType.LONG && (value.isLong() || value.isInt())) {
                // Jackson actually creates an int value for a small number so we check for both (long and int value) here
                return ValueReference.of(value.longValue());
            } else if (valueType == ValueType.STRING && value.isTextual()) {
                return ValueReference.of(value.textValue());
            } else if (valueType == ValueType.PARAMETER && value.isTextual()) {
                return ValueReference.createParameter(value.textValue());
            } else {
                return null;
            }
        } else {
            final ImmutableMap.Builder<String, Reference> map = ImmutableMap.builder();
            final Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            while (fields.hasNext()) {
                final Map.Entry<String, JsonNode> entry = fields.next();
                map.put(entry.getKey(), convert(entry.getValue()));
            }
            return new ReferenceMap(map.build());
        }
    } else if (jsonNode.isArray()) {
        final ImmutableList.Builder<Reference> list = ImmutableList.builder();
        for (JsonNode value : jsonNode) {
            list.add(convert(value));
        }
        return new ReferenceList(list.build());
    }
    return null;
}
Also used : ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) Reference(org.graylog2.contentpacks.model.entities.references.Reference) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) ImmutableMap(com.google.common.collect.ImmutableMap) ReferenceList(org.graylog2.contentpacks.model.entities.references.ReferenceList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Reference (org.graylog2.contentpacks.model.entities.references.Reference)1 ReferenceList (org.graylog2.contentpacks.model.entities.references.ReferenceList)1 ReferenceMap (org.graylog2.contentpacks.model.entities.references.ReferenceMap)1 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)1 ValueType (org.graylog2.contentpacks.model.entities.references.ValueType)1