Package | Description |
---|---|
org.dromara.hutool.core.bean |
Bean相关操作,包括Bean信息描述,Bean路径表达式、动态Bean、Bean工具等
|
org.dromara.hutool.core.bean.copier |
Bean拷贝实现,包括拷贝选项等
|
org.dromara.hutool.core.lang.mutable |
提供可变值对象的封装,用于封装int、long等不可变值,使其可变
|
org.dromara.hutool.json |
JSON(JavaScript Object Notation JavaScript对象表示法)封装
规范见:https://www.rfc-editor.org/rfc/rfc8259 包含以下组件: JSONObject: 使用键值对表示的数据类型,使用"{}"包围 JSONArray: 使用列表表示的数据类型,使用"[]"包围 JSONPrimitive:表示boolean、String、Number等原始类型 JSON封装主要包括JSON表示和JSON转换:
<-----JSONMapper----- <---JSONParser----
Java对象 <====================> JSON对象 <=================> JSON字符串
-----JSONMapper-----> ---JSONWriter---->
当然,为了高效转换,如果没有自定义需求,Java对象可以不通过JSON对象与JSON字符串转换:
JSONTokener:JSON字符串底层解析器,通过Stream方式读取JSON字符串并对不同字段自定义处理。 |
org.dromara.hutool.json.writer |
JSON对象自定义转JSON字符串实现
|
org.dromara.hutool.json.xml |
JSON与XML相互转换封装,基于json.org官方库改造
|
Modifier and Type | Method and Description |
---|---|
static <V> Map<String,V> |
BeanUtil.beanToMap(Object bean,
Map<String,V> targetMap,
boolean ignoreNullValue,
UnaryOperator<MutableEntry<Object,Object>> keyEditor)
对象转Map
通过实现 UnaryOperator 可以自定义字段值,如果这个Editor返回null则忽略这个字段,以便实现:
1. |
Modifier and Type | Field and Description |
---|---|
protected UnaryOperator<MutableEntry<Object,Object>> |
CopyOptions.fieldEditor
字段属性名和属性值编辑器,用于自定义属性转换规则(例如驼峰转下划线等),自定义属性值转换规则(例如null转""等)
|
Modifier and Type | Method and Description |
---|---|
protected MutableEntry<Object,Object> |
CopyOptions.editField(Object key,
Object value)
编辑字段值
|
Modifier and Type | Method and Description |
---|---|
CopyOptions |
CopyOptions.setFieldEditor(UnaryOperator<MutableEntry<Object,Object>> editor)
设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致 当转换后的字段名为null时忽略这个字段 需要注意的是,当使用ValueProvider作为数据提供者时,这个映射是相反的,即参数中key为目标Bean的名称,而返回值是提供者中的key,并且对值的修改无效 |
Modifier and Type | Method and Description |
---|---|
static <K,V> MutableEntry<K,V> |
MutableEntry.of(K key,
V value)
创建
MutableEntry |
Modifier and Type | Method and Description |
---|---|
Predicate<MutableEntry<Object,Object>> |
JSONFactory.getPredicate()
获取键值对过滤器
Predicate.test(Object) 返回true 表示接受,false 表示忽略同时 MutableEntry 为可变键值对,在判断逻辑中可同时修改键和值,修改后返回true entry中,key在JSONObject中为name,在JSONArray中为index |
Modifier and Type | Method and Description |
---|---|
void |
JSONFactory.doPredicate(MutableEntry<Object,Object> entry,
Consumer<MutableEntry<Object,Object>> consumer)
执行键值对过滤,如果提供的键值对执行
Predicate.test(Object) 返回false ,则忽略此键值对;如果处理后返回 true 表示接受,调用Consumer.accept(Object) 执行逻辑。 |
Modifier and Type | Method and Description |
---|---|
void |
JSONFactory.doPredicate(MutableEntry<Object,Object> entry,
Consumer<MutableEntry<Object,Object>> consumer)
执行键值对过滤,如果提供的键值对执行
Predicate.test(Object) 返回false ,则忽略此键值对;如果处理后返回 true 表示接受,调用Consumer.accept(Object) 执行逻辑。 |
static JSONFactory |
JSONFactory.of(JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
创建JSON工厂
|
static JSON |
JSONUtil.parse(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
解析对象为JSON,持的对象:
String: 解析为相应的对象
Number、boolean: 转换为
JSONPrimitive
Array、Iterable、Iterator:转换为JSONArray
Bean对象:转为JSONObject
|
static JSONArray |
JSONUtil.parseArray(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
对象转JSONArray
|
static JSONObject |
JSONUtil.parseObj(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
对象转JSONObject对象
|
static JSON |
JSONUtil.toJSON(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。
|
default String |
JSON.toJSONString(int indentFactor,
Predicate<MutableEntry<Object,Object>> predicate)
格式化输出JSON字符串
|
Constructor and Description |
---|
JSONFactory(JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
构造
|
Modifier and Type | Method and Description |
---|---|
JSONWriter |
JSONWriter.writeField(MutableEntry<Object,Object> pair)
写出字段名及字段值,如果字段值是
null 且忽略null值,则不写出任何内容 |
Modifier and Type | Method and Description |
---|---|
static JSONWriter |
JSONWriter.of(Appendable appendable,
int indentFactor,
int level,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
创建JSONWriter
|
static JSONWriter |
JSONWriter.of(Appendable appendable,
int indentFactor,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
创建JSONWriter
|
Constructor and Description |
---|
JSONWriter(Appendable appendable,
JSONFormatStyle formatStyle,
int level,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
构造
|
Modifier and Type | Method and Description |
---|---|
static JSONXMLParser |
JSONXMLParser.of(ParseConfig parseConfig,
Predicate<MutableEntry<Object,Object>> predicate)
创建XML解析器
|
Constructor and Description |
---|
JSONXMLParser(ParseConfig parseConfig,
Predicate<MutableEntry<Object,Object>> predicate)
构造
|
Copyright © 2025. All rights reserved.