420接收网闸文件服务(现场部署版)
Lius
2025-02-27 f06f695488a64dcc0945e282dffe120148f21dca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.mm.util;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.IOException;
 
/**
 * json转换.
 */
public class JsonMapper {
    /** logger. */
    private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
 
    /** jackson. */
    private ObjectMapper mapper;
 
    /** constructor. */
    public JsonMapper() {
        mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }
 
    public String toJson(Object object) throws IOException {
        return mapper.writeValueAsString(object);
    }
 
    public <T> T fromJson(String jsonString, Class<T> clazz) throws IOException {
        if ((jsonString == null) || "".equals(jsonString.trim())) {
            return null;
        }
 
        return mapper.readValue(jsonString, clazz);
    }
 
    /**
     * new TypeReference<List<String>>(){}
     */
    public <T> T fromJson(String jsonString, TypeReference<T> typeReference)
            throws IOException {
        if ((jsonString == null) || "".equals(jsonString.trim())) {
            return null;
        }
 
        return (T) mapper.readValue(jsonString, typeReference);
    }
 
    public String toJsonP(String functionName, Object object)
            throws IOException {
        return toJson(new JSONPObject(functionName, object));
    }
}