GithubHelp home page GithubHelp logo

troyzhxu / xjsonkit Goto Github PK

View Code? Open in Web Editor NEW
27.0 2.0 6.0 156 KB

超轻量级 JSON / JSONB / XML / YAML 解析门面 API,用法简单,使业务代码不依赖具体实现,解耦神器!

License: Apache License 2.0

Java 100.00%
java json jsonb xml yaml

xjsonkit's Introduction

xJsonKit

Maven Central License Troy.Zhou

介绍

超轻量级 JSON / JSONB / XML / YAML正序列化反序列化门面框架,让业务代码不依赖具体的实现,与 Jackson、Gson、Fastjson 等 解耦,让你可以在需要的时候轻松切换项目底层实现!

安装教程

API 依赖

implementation 'cn.zhxu:xjsonkit-api:1.4.3'

该依赖提供了 JsonKitJsonbKitXmlKitYamlKit 四个工具类,它们分别提供了对 JSON / JSONB / XML / YAML 类型数据的正反序列化操作相关的 API。

JSON 相关实现

// Fastjson 实现
implementation 'cn.zhxu:xjsonkit-fastjson:1.4.3'
// Fastjson2 实现
implementation 'cn.zhxu:xjsonkit-fastjson2:1.4.3'
// Gson 实现
implementation 'cn.zhxu:xjsonkit-gson:1.4.3'
// Jackson 实现
implementation 'cn.zhxu:xjsonkit-jackson:1.4.3'
// Snack3 实现
implementation 'cn.zhxu:xjsonkit-snack3:1.4.3'

JSONB 相关实现

// Fastjson2-JSONB 实现
implementation 'cn.zhxu:xjsonkit-fastjson2-jsonb:1.4.3'

XML 相关实现

// Jackson Xml
implementation 'cn.zhxu:xjsonkit-jackson-xml:1.4.3'

YAML 相关实现

// Jackson Yaml
implementation 'cn.zhxu:xjsonkit-jackson-yaml:1.4.3'

使用说明

xJsonKit 共提供了 JsonKitJsonbKitXmlKitYamlKit 四个工具类。下文以 JsonKit 为例简述其用法,其它工具类使用方法类似。

反序列化 toMapper

String json = "{\"name\":\"Jack\",\"age\":20}";
// 转换为具有映射结构的 Mapper 对象
Mapper mapper = JsonKit.toMapper(json);

// 第一层的键集合大小
int size = mapper.size();               // 2
// 第一层的键值集合
Set<String> keys = mapper.keySet();     // ["name", "age"]
// 按键名访问 String 属性
String name = mapper.getString("name"); // Jack
// 按键名访问 int 属性
int age = mapper.getInt("age");         // 20
    
// 遍历 Mapper 对象
mapper.forEach((key, data) -> {
    System.out.println(key);            // 依次输出 name 和 age
    System.out.println(data);           // 依次输出 Jack 和 20
});

System.out.println(mapper);             // 输出 {"name":"Jack","age":20}

反序列化 toArray

String json = "[20,{\"name\":\"Jack\"},\"JsonKit\"]";
// 转换为具有数组结构的 Array 对象
Array array = JsonKit.toArray(json);

// 数组大小
int size = array.size();                // 3
// 按下标获取 int 数据
int value = array.getInt(0);            // 20
// 按下标获取 Mapper 数据
Mapper mapper = array.getMapper(1);     // {"name":"Jack"}
// 按下标获取 String 数据
String string = array.getString(2);     // JsonKit

// 遍历 Array 对象
array.forEach(((index, data) -> {
    System.out.println(index);          // 依次输出 0、 1 和 2
    System.out.println(data);           // 依次输出 20、 {"name":"Jack"} 和 JsonKit
}));

System.out.println(array);              // 输出 [20,{"name":"Jack"},"JsonKit"]

反序列化 toBean

String json = "{\"name\":\"Jack\",\"age\":20}";
// 根据类型 转换为 Java Bean
User user = JsonKit.toBean(User.class, json);

String name = user.getName();           // Jack
int name = user.getAge();               // 20

反序列化 toList

String json = "[{\"name\":\"Jack\",\"age\":20}, {\"name\":\"Tom\",\"age\":21}]";
// 根据类型 转换为 Java List
List<User> list = JsonKit.toList(User.class, json);

int size = list.size();                 // 2
User user1 = list.get(0);               // {"name":"Jack","age":20}
User user2 = list.get(1);               // {"name":"Tom","age":21}

正序列化 toJson

User user = new User();
user.setName("Jack");
user.setAge(20);

String json = JsonKit.toJson(user);     // 转换为 JSON 字符串

System.out.println(json);               // 输出 {"age":20,"name":"Jack"}

配置方法

如果已经添加了如 xjsonkit-jackson 的底层实现包,则不用任何配置即可正常使用。

但如果需要特殊配置,例如使用 Jackson 时,可进行如下配置:

ObjectMapper objectMapper = new ObjectMapper();
// TODO: 对 ObjectMapper 进行配置...

// 对 JsonKit 进行配置
Config.json(new JacksonDataConvertor(ObjectMapper));

其中 Configcn.zhxu.xjson.spi 包下的配置类,它里面有 json(..)jsonb(..)xml(..)yaml(..) 四个配置方法,它们分别作用于 JsonKitJsonbKitXmlKitYamlKit 四个工具类。

自定义底层实现

只需实现 DataConvertor 接口即可:

// 自定义 JSON 实现
Config.json(new MyJsonDataConvertor());
// 自定义 JSONB 实现
Config.jsonb(new MyJsonbDataConvertor());
// 自定义 XML 实现
Config.xml(new MyXmlDataConvertor());
// 自定义 YAML 实现
Config.yaml(new MyYamlDataConvertor());

友情链接

[ OkHttps ] 轻量却强大的 HTTP 客户端,前后端通用,支持 WebSocket 与 Stomp 协议

[ Bean Searcher ] 轻量级数据库条件检索引擎,一行代码实现复杂条件列表检索!

参与贡献

  1. Star Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

xjsonkit's People

Contributors

troyzhxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xjsonkit's Issues

JSON注解适配

作者你好,在实际项目中,有时候会用到json序列化工具的注解来标注字段的特殊处理,如

  • gson @SerializedName
  • fastjson @JSONField
  • jackson @JsonAlias

jsonkit能否出个通用注解来统一适配,让切换更顺滑

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.