Giter VIP home page Giter VIP logo

json-util's Introduction

json-util

Json Util Integrate Jackson、Gson And Fastjson

json-util工具类整合了Jackson、Gson和Fastjson库并统一API。

Env

  • JDK7+

Feature

统一调用API,形如toXXX。目前支持的转换的列表:

  • List
  • Map
  • JsonString
  • Pojo

List支持泛型,JsonString支持指定日期格式化,支持对象和Map的互相转换。

支持从application.propertiesapplication.yml中设置解析Json的库(jackson, gson, fastjson)。

application.properties:

json.class.type=jackson

application.yml:

json:
  class-type: gson

支持自动查找用户引入的Json库(jackson, gson, fastjson)用来解析Json

Usage

pom.xml

<properties>
    <jackson.version>2.9.0</jackson.version>
    <gson.version>2.8.2</gson.version>
    <fastjson.version>1.2.44</fastjson.version>
</properties>
<dependencies>
        <!-- use jackson lib-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>          
        </dependency>
         <!-- or use gson lib-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>     
        </dependency>
         <!-- or use fastjson lib-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>      
        </dependency>
    </dependencies>

默认从application.propertiesapplication.yml中读取配置的Json库,

若未配置则自动查找用户引入的Json库(jackson, gson, fastjson),若存在多个则默认使用Jackson作为json解析库。

解析到List的例子:

 String json = "[1, 2, 4, 5]";
 List result = JsonUtil.toList(json);
 
 ...
 json = "[{"id": 1,"username": "yidasanqian"},{"id": 2,"username": "yidasanqian2"}]"
 TypeReference<List<User>> typeReference = new TypeReference<List<User>>() {};
 List<User> result = JsonUtil.toList(json, typeReference.getType());

json-util's People

Contributors

yidasanqian avatar dependabot[bot] avatar

Watchers

James Cloos 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.