• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / rocketmq-dashboard / 200

pending completion
200

Pull #130

travis-ci-com

web-flow
Merge e87e98135 into 86bdb0636
Pull Request #130: Bump snakeyaml from 1.30 to 1.32

2374 of 2869 relevant lines covered (82.75%)

1.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.59
/src/main/java/org/apache/rocketmq/dashboard/util/JsonUtil.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package org.apache.rocketmq.dashboard.util;
19

20
import com.fasterxml.jackson.annotation.JsonInclude;
21
import com.fasterxml.jackson.core.type.TypeReference;
22
import com.fasterxml.jackson.databind.DeserializationFeature;
23
import com.fasterxml.jackson.databind.ObjectMapper;
24
import com.fasterxml.jackson.databind.SerializationFeature;
25
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
26
import com.google.common.base.Strings;
27
import com.google.common.base.Throwables;
28
import java.io.IOException;
29
import java.io.Writer;
30
import java.text.SimpleDateFormat;
31
import java.util.Map;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

35
@SuppressWarnings("unchecked")
36
public class JsonUtil {
37

38
    private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
2✔
39
    private static ObjectMapper objectMapper = new ObjectMapper();
2✔
40

41
    private JsonUtil() {
42
    }
43

44
    static {
45
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
2✔
46
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
2✔
47
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
2✔
48
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
2✔
49
        objectMapper.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
2✔
50
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
2✔
51
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
2✔
52
    }
2✔
53

54
    public static void writeValue(Writer writer, Object obj) {
55
        try {
56
            objectMapper.writeValue(writer, obj);
×
57
        }
58
        catch (IOException e) {
×
59
            Throwables.propagateIfPossible(e);
×
60
        }
×
61
    }
×
62

63
    public static <T> String obj2String(T src) {
64
        if (src == null) {
2✔
65
            return null;
×
66
        }
67

68
        try {
69
            return src instanceof String ? (String)src : objectMapper.writeValueAsString(src);
2✔
70
        }
71
        catch (Exception e) {
×
72
            logger.error("Parse Object to String error src=" + src, e);
×
73
            return null;
×
74
        }
75
    }
76

77
    public static <T> byte[] obj2Byte(T src) {
78
        if (src == null) {
×
79
            return null;
×
80
        }
81

82
        try {
83
            return src instanceof byte[] ? (byte[])src : objectMapper.writeValueAsBytes(src);
×
84
        }
85
        catch (Exception e) {
×
86
            logger.error("Parse Object to byte[] error", e);
×
87
            return null;
×
88
        }
89
    }
90

91
    public static <T> T string2Obj(String str, Class<T> clazz) {
92
        if (Strings.isNullOrEmpty(str) || clazz == null) {
×
93
            return null;
×
94
        }
95
        str = escapesSpecialChar(str);
×
96
        try {
97
            return clazz.equals(String.class) ? (T)str : objectMapper.readValue(str, clazz);
×
98
        }
99
        catch (Exception e) {
×
100
            logger.error("Parse String to Object error\nString: {}\nClass<T>: {}\nError: {}", str, clazz.getName(), e);
×
101
            return null;
×
102
        }
103
    }
104

105
    public static <T> T byte2Obj(byte[] bytes, Class<T> clazz) {
106
        if (bytes == null || clazz == null) {
×
107
            return null;
×
108
        }
109
        try {
110
            return clazz.equals(byte[].class) ? (T)bytes : objectMapper.readValue(bytes, clazz);
×
111
        }
112
        catch (Exception e) {
×
113
            logger.error("Parse byte[] to Object error\nbyte[]: {}\nClass<T>: {}\nError: {}", bytes, clazz.getName(), e);
×
114
            return null;
×
115
        }
116
    }
117

118
    public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
119
        if (Strings.isNullOrEmpty(str) || typeReference == null) {
2✔
120
            return null;
×
121
        }
122
        str = escapesSpecialChar(str);
2✔
123
        try {
124
            return (T)(typeReference.getType().equals(String.class) ? str : objectMapper.readValue(str, typeReference));
2✔
125
        }
126
        catch (Exception e) {
×
127
            logger.error("Parse String to Object error\nString: {}\nTypeReference<T>: {}\nError: {}", str,
×
128
                typeReference.getType(), e);
×
129
            return null;
×
130
        }
131
    }
132

133
    public static <T> T byte2Obj(byte[] bytes, TypeReference<T> typeReference) {
134
        if (bytes == null || typeReference == null) {
×
135
            return null;
×
136
        }
137
        try {
138
            return (T)(typeReference.getType().equals(byte[].class) ? bytes : objectMapper.readValue(bytes,
×
139
                typeReference));
140
        }
141
        catch (Exception e) {
×
142
            logger.error("Parse byte[] to Object error\nbyte[]: {}\nTypeReference<T>: {}\nError: {}", bytes,
×
143
                typeReference.getType(), e);
×
144
            return null;
×
145
        }
146
    }
147

148
    public static <T> T map2Obj(Map<String, String> map, Class<T> clazz) {
149
        String str = obj2String(map);
×
150
        return string2Obj(str, clazz);
×
151
    }
152

153
    private static String escapesSpecialChar(String str) {
154
        return str.replace("\n", "\\n").replace("\r", "\\r");
2✔
155
    }
156
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc