Giter Site home page Giter Site logo

wujiuye / json-parser Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 3.0 48 KB

这是一个类似self4j的json门面工具,自动适配依赖配置引入的json工具包,特别适合用于低层服务框架和中间件SDK的开发。

License: Apache License 2.0

Java 100.00%

json-parser's Introduction

json-parser

  • auth: wujiuye 2021/06/08
  • 从2.x版本开始,hotkit-json更名为json-parser,并从hotkit项目中独立出来成为独立维护的项目。
  • 建议使用最新版本:json-parser-core

Maven central License

模块说明

json-parser-core

json适配器组件,让切换json解析框架只需要切换依赖包即可。

json-parser-adapter

json-parser-adapter是json-parser-core适配springbootstarter包,自动将webmvcwebfluxjson解析工作交给json-parser-core完成, 以此让整个项目使用同一套json解析配置。

json-parser-core

json-parser-core目前已适配jackson、gson,json-parser-core根据项目中导入了哪个json解析工具就使用哪个json解析框架。

  • 注意:如果项目中即导入了jackson又导入了gson,那么将会按适配代码的执行顺序优先选择,见JsonUtils类,当然,也可通过配置方式切换(见配置说明)。

使用说明

1、在项目中添加json-parser-core依赖

<dependency>
    <groupId>com.github.wujiuye</groupId>
    <artifactId>json-parser-core</artifactId>
    <version>{version}</version>
</dependency>

2、在项目中添加jackson或者gson的依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>{version}</version>
</dependency>

支持的API

目前支持的API见JsonParser接口。可扩展,欢迎提交合并请求。

public interface JsonParser {
    // ======== 序列化 ==============
    <T> String toJsonString(T obj);
    // ======== 反序列化 ============
    <T> T fromJson(String jsonStr, Class<T> tClass);
    <T> T fromJson(InputStream jsonIn, Class<T> tClass);
    <T> T fromJson(String jsonStr, Type type);
    <T> T fromJson(InputStream jsonIn, Type type);
    <T> List<T> fromJsonArray(String jsonStr, TypeReference<List<T>> typeReference);
    <T> List<T> fromJsonArray(InputStream jsonIn, TypeReference<List<T>> typeReference);
    <K, V> Map<K, V> fromJsonMap(String jsonStr, TypeReference<Map<K, V>> typeReference);
    <K, V> Map<K, V> fromJsonMap(InputStream jsonIn, TypeReference<Map<K, V>> typeReference);
}

支持的配置项

public class SerializeConfig {
    /**
     * 是否序列化null字段
     */
    private boolean serializeNulls = false;
    /**
     * 是否开启防xss攻击过滤
     */
    private boolean openXssFilter = true;
    /**
     * 使用的时区
     */
    private int timezone = 8;
    /**
     * Date类型序列化格式,配置为null则序列化为时间戳(反序列化时也会自动适配时间戳)
     */
    private String dateFormat = "yyyy-MM-dd HH:mm:ss";
    /**
     * LocalDateTime类型序列化格式,配置为null则序列化为时间戳(反序列化时也会自动适配时间戳)
     */
    private String localDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    /**
     * LocalDate类型序列化格式,配置为null则序列化为时间戳(反序列化时也会自动适配时间戳)
     */
    private String localDateFormat = "yyyy-MM-dd";
}

修改配置:

class Main{
    public static void main(String[] args){
        com.wujiuye.jsonparser.core.JsonUtils.resetSerializeConfig(new SerializeConfig());
    }
}

json-parser-adapter

json-parser-adapterjson-parser-core适配springbootstarter包,自动将webmvcwebfluxjson解析工作交给json-parser-adapter完成,以此让整个项目使用同一套json解析配置。

使用说明

1、在项目中添加json-parser-core依赖

<dependency>
    <groupId>com.github.wujiuye</groupId>
    <artifactId>json-parser-core</artifactId>
    <version>{version}</version>
</dependency>

2、在项目中添加json-parser-adapter依赖

<dependency>
    <groupId>com.github.wujiuye</groupId>
    <artifactId>json-parser-adapter</artifactId>
    <version>{version}</version>
</dependency>

3、在application.yaml中配置序列化&反序列化参数

spring:
  json-parser:
    # 是否序列化null值,默认false
    serialize-null: false
    # Date类型序列化格式,默认:yyyy-MM-dd HH:mm:ss (如果配置为null,则会序列化为时间戳,反序列化则自动适配)
    date-format: yyyy-MM-dd HH:mm:ss
    # LocalDateTime类型序列化格式,默认:yyyy-MM-dd (如果配置为null,则会序列化为时间戳,反序列化则自动适配)
    local-date-format: yyyy-MM-dd
    # LocalDate类型序列化格式,默认:yyyy-MM-dd HH:mm:ss (如果配置为null,则会序列化为时间戳,反序列化则自动适配)
    local-date-time-format: yyyy-MM-dd HH:mm:ss
    # 时区(适配时间戳序列化&反序列化),默认为东8区
    timezone: 8
    # 是否开启xss过滤器,默认开启
    open-xss-filter: true

json-parser's People

Contributors

wujiuye avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.