added ALLOW_UNQUOTED_FIELD_NAMES_MAPPER to JacksonUtil
This commit is contained in:
		
							parent
							
								
									1d77a1c027
								
							
						
					
					
						commit
						02ca2b0235
					
				@ -1,12 +1,12 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * <p>
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * <p>
 | 
			
		||||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 * <p>
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
@ -15,7 +15,9 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.common.util;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonParser;
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.core.json.JsonWriteFeature;
 | 
			
		||||
import com.fasterxml.jackson.core.type.TypeReference;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.MapperFeature;
 | 
			
		||||
@ -46,6 +48,10 @@ public class JacksonUtil {
 | 
			
		||||
            .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
 | 
			
		||||
            .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
 | 
			
		||||
            .build();
 | 
			
		||||
    public static ObjectMapper ALLOW_UNQUOTED_FIELD_NAMES_MAPPER = JsonMapper.builder()
 | 
			
		||||
            .configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false)
 | 
			
		||||
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
 | 
			
		||||
            .build();
 | 
			
		||||
 | 
			
		||||
    public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
 | 
			
		||||
        try {
 | 
			
		||||
@ -119,11 +125,15 @@ public class JacksonUtil {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static JsonNode toJsonNode(String value) {
 | 
			
		||||
        return toJsonNode(value, OBJECT_MAPPER);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static JsonNode toJsonNode(String value, ObjectMapper mapper) {
 | 
			
		||||
        if (value == null || value.isEmpty()) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            return OBJECT_MAPPER.readTree(value);
 | 
			
		||||
            return mapper.readTree(value);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            throw new IllegalArgumentException(e);
 | 
			
		||||
        }
 | 
			
		||||
@ -138,7 +148,11 @@ public class JacksonUtil {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ObjectNode newObjectNode() {
 | 
			
		||||
        return OBJECT_MAPPER.createObjectNode();
 | 
			
		||||
        return newObjectNode(OBJECT_MAPPER);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ObjectNode newObjectNode(ObjectMapper mapper) {
 | 
			
		||||
        return mapper.createObjectNode();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static <T> T clone(T value) {
 | 
			
		||||
@ -216,6 +230,10 @@ public class JacksonUtil {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void addKvEntry(ObjectNode entityNode, KvEntry kvEntry, String key) {
 | 
			
		||||
        addKvEntry(entityNode, kvEntry, key, OBJECT_MAPPER);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void addKvEntry(ObjectNode entityNode, KvEntry kvEntry, String key, ObjectMapper mapper) {
 | 
			
		||||
        if (kvEntry.getDataType() == DataType.BOOLEAN) {
 | 
			
		||||
            kvEntry.getBooleanValue().ifPresent(value -> entityNode.put(key, value));
 | 
			
		||||
        } else if (kvEntry.getDataType() == DataType.DOUBLE) {
 | 
			
		||||
@ -224,7 +242,7 @@ public class JacksonUtil {
 | 
			
		||||
            kvEntry.getLongValue().ifPresent(value -> entityNode.put(key, value));
 | 
			
		||||
        } else if (kvEntry.getDataType() == DataType.JSON) {
 | 
			
		||||
            if (kvEntry.getJsonValue().isPresent()) {
 | 
			
		||||
                entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get()));
 | 
			
		||||
                entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get(), mapper));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            entityNode.put(key, kvEntry.getValueAsString());
 | 
			
		||||
 | 
			
		||||
@ -15,15 +15,11 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.rule.engine.metadata;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonParser;
 | 
			
		||||
import com.fasterxml.jackson.core.json.JsonWriteFeature;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import com.google.common.util.concurrent.MoreExecutors;
 | 
			
		||||
import com.google.gson.JsonParseException;
 | 
			
		||||
import org.apache.commons.collections.CollectionUtils;
 | 
			
		||||
import org.apache.commons.lang3.BooleanUtils;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
@ -35,14 +31,12 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.DataType;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.JsonDataEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.KvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.TsKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -60,8 +54,6 @@ import static org.thingsboard.server.common.data.DataConstants.SHARED_SCOPE;
 | 
			
		||||
 | 
			
		||||
public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> implements TbNode {
 | 
			
		||||
 | 
			
		||||
    private static ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
 | 
			
		||||
    private static final String VALUE = "value";
 | 
			
		||||
    private static final String TS = "ts";
 | 
			
		||||
 | 
			
		||||
@ -73,8 +65,6 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
 | 
			
		||||
    @Override
 | 
			
		||||
    public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
 | 
			
		||||
        this.config = loadGetAttributesNodeConfig(configuration);
 | 
			
		||||
        mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
 | 
			
		||||
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
 | 
			
		||||
        this.fetchToData = config.isFetchToData();
 | 
			
		||||
        this.getLatestValueWithTs = config.isGetLatestValueWithTs();
 | 
			
		||||
        this.isTellFailureIfAbsent = BooleanUtils.toBooleanDefaultIfNull(this.config.isTellFailureIfAbsent(), true);
 | 
			
		||||
@ -101,7 +91,7 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
 | 
			
		||||
            ctx.tellNext(msg, FAILURE);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        JsonNode msgDataNode = toJsonNode(msg.getData());
 | 
			
		||||
        JsonNode msgDataNode = JacksonUtil.toJsonNode(msg.getData(), JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        if (fetchToData) {
 | 
			
		||||
            if (!msgDataNode.isObject()) {
 | 
			
		||||
                ctx.tellFailure(msg, new IllegalArgumentException("Msg body is not an object!"));
 | 
			
		||||
@ -125,7 +115,7 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
 | 
			
		||||
                    String prefix = getPrefix(keyScope);
 | 
			
		||||
                    kvEntryList.forEach(kvEntry -> {
 | 
			
		||||
                        if (fetchToData) {
 | 
			
		||||
                            addKvEntryToJson((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey());
 | 
			
		||||
                            JacksonUtil.addKvEntry((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey(), JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
                        } else {
 | 
			
		||||
                            msgMetaData.putValue(prefix + kvEntry.getKey(), kvEntry.getValueAsString());
 | 
			
		||||
                        }
 | 
			
		||||
@ -180,9 +170,9 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private TsKvEntry getValueWithTs(TsKvEntry tsKvEntry) {
 | 
			
		||||
        ObjectNode value = mapper.createObjectNode();
 | 
			
		||||
        ObjectNode value = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        value.put(TS, tsKvEntry.getTs());
 | 
			
		||||
        addKvEntryToJson(value, tsKvEntry, VALUE);
 | 
			
		||||
        JacksonUtil.addKvEntry(value, tsKvEntry, VALUE, JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        return new BasicTsKvEntry(tsKvEntry.getTs(), new JsonDataEntry(tsKvEntry.getKey(), value.toString()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -202,30 +192,6 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
 | 
			
		||||
        return prefix;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void addKvEntryToJson(ObjectNode entityNode, KvEntry kvEntry, String key) {
 | 
			
		||||
        if (kvEntry.getDataType() == DataType.BOOLEAN) {
 | 
			
		||||
            kvEntry.getBooleanValue().ifPresent(value -> entityNode.put(key, value));
 | 
			
		||||
        } else if (kvEntry.getDataType() == DataType.DOUBLE) {
 | 
			
		||||
            kvEntry.getDoubleValue().ifPresent(value -> entityNode.put(key, value));
 | 
			
		||||
        } else if (kvEntry.getDataType() == DataType.LONG) {
 | 
			
		||||
            kvEntry.getLongValue().ifPresent(value -> entityNode.put(key, value));
 | 
			
		||||
        } else if (kvEntry.getDataType() == DataType.JSON) {
 | 
			
		||||
            if (kvEntry.getJsonValue().isPresent()) {
 | 
			
		||||
                entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get()));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            entityNode.put(key, kvEntry.getValueAsString());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static JsonNode toJsonNode(String value) {
 | 
			
		||||
        try {
 | 
			
		||||
            return mapper.readTree(value);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            throw new JsonParseException("Can't parse jsonValue: " + value, e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private List<String> getNotExistingKeys(List<AttributeKvEntry> existingAttributesKvEntry, List<String> allKeys) {
 | 
			
		||||
        List<String> existingKeys = existingAttributesKvEntry.stream().map(KvEntry::getKey).collect(Collectors.toList());
 | 
			
		||||
        return allKeys.stream().filter(key -> !existingKeys.contains(key)).collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
@ -15,25 +15,22 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.rule.engine.metadata;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonParser;
 | 
			
		||||
import com.fasterxml.jackson.core.json.JsonWriteFeature;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ArrayNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import com.google.gson.JsonParseException;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.thingsboard.server.common.data.StringUtils;
 | 
			
		||||
import org.apache.commons.lang3.math.NumberUtils;
 | 
			
		||||
import org.thingsboard.common.util.DonAsynchron;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleNode;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbNode;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbNodeException;
 | 
			
		||||
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.StringUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.Aggregation;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.ReadTsKvQuery;
 | 
			
		||||
@ -41,7 +38,6 @@ import org.thingsboard.server.common.data.kv.TsKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
@ -75,7 +71,6 @@ public class TbGetTelemetryNode implements TbNode {
 | 
			
		||||
    private TbGetTelemetryNodeConfiguration config;
 | 
			
		||||
    private List<String> tsKeyNames;
 | 
			
		||||
    private int limit;
 | 
			
		||||
    private ObjectMapper mapper;
 | 
			
		||||
    private String fetchMode;
 | 
			
		||||
    private String orderByFetchAll;
 | 
			
		||||
    private Aggregation aggregation;
 | 
			
		||||
@ -91,10 +86,6 @@ public class TbGetTelemetryNode implements TbNode {
 | 
			
		||||
            orderByFetchAll = ASC_ORDER;
 | 
			
		||||
        }
 | 
			
		||||
        aggregation = parseAggregationConfig(config.getAggregation());
 | 
			
		||||
 | 
			
		||||
        mapper = new ObjectMapper();
 | 
			
		||||
        mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
 | 
			
		||||
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Aggregation parseAggregationConfig(String aggName) {
 | 
			
		||||
@ -146,7 +137,7 @@ public class TbGetTelemetryNode implements TbNode {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void process(List<TsKvEntry> entries, TbMsg msg, List<String> keys) {
 | 
			
		||||
        ObjectNode resultNode = mapper.createObjectNode();
 | 
			
		||||
        ObjectNode resultNode = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        if (FETCH_MODE_ALL.equals(fetchMode)) {
 | 
			
		||||
            entries.forEach(entry -> processArray(resultNode, entry));
 | 
			
		||||
        } else {
 | 
			
		||||
@ -169,36 +160,16 @@ public class TbGetTelemetryNode implements TbNode {
 | 
			
		||||
            ArrayNode arrayNode = (ArrayNode) node.get(entry.getKey());
 | 
			
		||||
            arrayNode.add(buildNode(entry));
 | 
			
		||||
        } else {
 | 
			
		||||
            ArrayNode arrayNode = mapper.createArrayNode();
 | 
			
		||||
            ArrayNode arrayNode = JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER.createArrayNode();
 | 
			
		||||
            arrayNode.add(buildNode(entry));
 | 
			
		||||
            node.set(entry.getKey(), arrayNode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ObjectNode buildNode(TsKvEntry entry) {
 | 
			
		||||
        ObjectNode obj = mapper.createObjectNode()
 | 
			
		||||
                .put("ts", entry.getTs());
 | 
			
		||||
        switch (entry.getDataType()) {
 | 
			
		||||
            case STRING:
 | 
			
		||||
                obj.put("value", entry.getValueAsString());
 | 
			
		||||
                break;
 | 
			
		||||
            case LONG:
 | 
			
		||||
                obj.put("value", entry.getLongValue().get());
 | 
			
		||||
                break;
 | 
			
		||||
            case BOOLEAN:
 | 
			
		||||
                obj.put("value", entry.getBooleanValue().get());
 | 
			
		||||
                break;
 | 
			
		||||
            case DOUBLE:
 | 
			
		||||
                obj.put("value", entry.getDoubleValue().get());
 | 
			
		||||
                break;
 | 
			
		||||
            case JSON:
 | 
			
		||||
                try {
 | 
			
		||||
                    obj.set("value", mapper.readTree(entry.getJsonValue().get()));
 | 
			
		||||
                } catch (IOException e) {
 | 
			
		||||
                    throw new JsonParseException("Can't parse jsonValue: " + entry.getJsonValue().get(), e);
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
        ObjectNode obj = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        obj.put("ts", entry.getTs());
 | 
			
		||||
        JacksonUtil.addKvEntry(obj, entry, "value", JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
 | 
			
		||||
        return obj;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user