Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog.plugins.pipelineprocessor.functions.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.auto.value.AutoValue;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import org.graylog.autovalue.WithBeanGetter;
import org.graylog2.jackson.TypeReferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static com.google.common.base.Strings.isNullOrEmpty;
public class JsonUtils {
private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);
private static final String KEY_SEPARATOR = "_";
private static final String KEY_VALUE_SEPARATOR = ":";
private static final String LIST_SEPARATOR = ",";
private static final RemoveNullPredicate> REMOVE_NULL_PREDICATE = new RemoveNullPredicate<>();
private JsonUtils() {
}
public static JsonNode extractJson(
String value, ObjectMapper mapper, ExtractFlags extractFlags, boolean stringify)
throws IOException {
if (isNullOrEmpty(value)) {
throw new IOException("null result");
}
final Map json = mapper.readValue(value, TypeReferences.MAP_STRING_OBJECT);
ObjectNode resultRoot = mapper.createObjectNode();
for (Map.Entry mapEntry : json.entrySet()) {
for (Entry entry : parseValue(mapEntry.getKey(), mapEntry.getValue(), mapper, extractFlags)) {
if (stringify) {
resultRoot.put(entry.key(), entry.value().toString());
} else {
resultRoot.putPOJO(entry.key(), entry.value());
}
}
}
return resultRoot;
}
private static Collection parseValue(
String key, Object value, ObjectMapper mapper, ExtractFlags extractFlags)
throws JsonProcessingException {
if (value instanceof Boolean || value instanceof Number || value instanceof String) {
return Collections.singleton(Entry.create(key, value));
} else if (value instanceof Map) {
return parseObject(key, (Map) value, mapper, extractFlags);
} else if (value instanceof List) {
return parseArray(key, (List