Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04405bb

Browse files
author
zhangpeng@qlteacher.com
committed
jackson 配置,参考spring
1 parent 66a29d0 commit 04405bb

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎src/main/java/com/jarvis/cache/serializer/JacksonJsonSerializer.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jarvis.cache.serializer;
22

3+
import java.io.IOException;
34
import java.lang.reflect.Array;
45
import java.lang.reflect.Method;
56
import java.lang.reflect.ParameterizedType;
@@ -11,8 +12,16 @@
1112
import java.util.Map;
1213
import java.util.Map.Entry;
1314

15+
import org.springframework.util.StringUtils;
16+
17+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
18+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.databind.DeserializationFeature;
1420
import com.fasterxml.jackson.databind.JavaType;
1521
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.SerializerProvider;
23+
import com.fasterxml.jackson.databind.module.SimpleModule;
24+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
1625
import com.jarvis.cache.reflect.generics.ParameterizedTypeImpl;
1726
import com.jarvis.cache.to.CacheWrapper;
1827
import com.jarvis.lib.util.BeanUtil;
@@ -23,6 +32,41 @@
2332
public class JacksonJsonSerializer implements ISerializer<Object> {
2433

2534
private static final ObjectMapper mapper=new ObjectMapper();
35+
36+
public JacksonJsonSerializer(){
37+
//mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
38+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
39+
mapper.registerModule(new SimpleModule().addSerializer(new JacksonJsonSerializer.NullValueSerializer((String)null)));
40+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
41+
}
42+
43+
private class NullValueSerializer extends StdSerializer<NullValue> {
44+
45+
private static final long serialVersionUID = 1999052150548658808L;
46+
private final String classIdentifier;
47+
48+
/**
49+
* @param classIdentifier can be {@literal null} and will be defaulted to {@code @class}.
50+
*/
51+
NullValueSerializer(String classIdentifier) {
52+
53+
super(NullValue.class);
54+
this.classIdentifier = StringUtils.hasText(classIdentifier) ? classIdentifier : "@class";
55+
}
56+
57+
/*
58+
* (non-Javadoc)
59+
* @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
60+
*/
61+
@Override
62+
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider)
63+
throws IOException {
64+
65+
jgen.writeStartObject();
66+
jgen.writeStringField(classIdentifier, NullValue.class.getName());
67+
jgen.writeEndObject();
68+
}
69+
}
2670

2771
@Override
2872
public byte[] serialize(final Object obj) throws Exception {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
//package org.springframework.cache.support;
17+
package com.jarvis.cache.serializer;
18+
19+
import java.io.Serializable;
20+
21+
/**
22+
* Simple serializable class that serves as a {@code null} replacement
23+
* for cache stores which otherwise do not support {@code null} values.
24+
*
25+
* @author Juergen Hoeller
26+
* @since 4.2.2
27+
* @see AbstractValueAdaptingCache
28+
*/
29+
public final class NullValue implements Serializable {
30+
31+
static final Object INSTANCE = new NullValue();
32+
33+
private static final long serialVersionUID = 1L;
34+
35+
36+
private NullValue() {
37+
}
38+
39+
private Object readResolve() {
40+
return INSTANCE;
41+
}
42+
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /