20 Star 107 Fork 23

yidasanqian / json-util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

json-util

Maven Central

Json Util Integrate Jackson、Gson And Fastjson

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

Env

  • JDK8+

Feature

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

  • List
  • Map
  • JsonString
  • Pojo

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

Usage

maven

<dependency>
    <groupId>io.github.yidasanqian</groupId>
    <artifactId>json-util</artifactId>
    <version>2.0.1</version>          
</dependency>

选择 jackson、gson and fastjson 其中之一引入 pom.xml:


<properties>
    <jackson.version>specify version</jackson.version>
    <gson.version>specify version</gson.version>
    <fastjson.version>specify version</fastjson.version>
</properties>
<dependencies>
        <!-- use jackson lib-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind-jsr310</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>

初始化时可以指定使用的Json库:

JsonUtil.initJson(JsonEnum.FASTJSON);

JsonEnum的值有JACKSON, FASTJSON, GSON

若未指定则自动查找用户引入的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());

解析到Map的例子:

String json = "{"id":1, "username":"yidasanqian"}"
TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() {};
Map result = JsonUtil.toMap(json, typeReference.getType());
MIT License Copyright (c) 2018 Linyu Chen 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.

简介

json-util工具类整合了Jackson、Gson和Fastjson库并统一API。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/yidasanqian/json-util.git
git@gitee.com:yidasanqian/json-util.git
yidasanqian
json-util
json-util
master

搜索帮助