
com.cuisongliu.jwt.autoconfig.FastJsonBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jwt-spring-boot-autoconfigure Show documentation
Show all versions of jwt-spring-boot-autoconfigure Show documentation
jwt integration with springboot.
The newest version!
package com.cuisongliu.jwt.autoconfig;
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.util.IOUtils;
import org.springframework.http.MediaType;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* fastjson相关的配置
* 最佳用法单例: Effective Java (No.3)
* @author cuisongliu [[email protected]]
* @since 2018-03-11 23:26
*/
public enum FastJsonBuilder {
INSTANCE;
/**
* 支持的mediaType类型
*/
public List supportedMediaType() {
ArrayList mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
return mediaTypes;
}
public FastJsonConfig fastjsonConfig() {
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue
);
fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
fastJsonConfig.setCharset(IOUtils.UTF8);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
ValueFilter valueFilter = new ValueFilter() {
@Override
public Object process(Object o, String s, Object o1) {
if (null == o1) {
o1 = "";
}
return o1;
}
};
fastJsonConfig.setCharset(Charset.forName("utf-8"));
fastJsonConfig.setSerializeFilters(valueFilter);
//解决Long转json精度丢失的问题
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
return fastJsonConfig;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy