mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
3e79778272
commit
463a8308e0
@ -112,11 +112,69 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
|||||||
return this.raw.get(key);
|
return this.raw.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// region ----- addValue
|
||||||
|
/**
|
||||||
|
* 加入{@code null}元素,如果设置中忽略null值,则忽略
|
||||||
|
*
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONArray addNull() {
|
||||||
|
this.add(null);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append an object value. This increases the array's length by one. <br>
|
|
||||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
*
|
*
|
||||||
* @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。
|
* @param value Number值
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONArray addValue(final Boolean value) {
|
||||||
|
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||||
|
this.add(this.factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
|
*
|
||||||
|
* @param value Number值
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONArray addValue(final Number value) {
|
||||||
|
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||||
|
this.add(this.factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
|
*
|
||||||
|
* @param value Character值
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONArray addValue(final Character value) {
|
||||||
|
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||||
|
this.add(this.factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
|
*
|
||||||
|
* @param value String值
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONArray addValue(final String value) {
|
||||||
|
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||||
|
this.add(this.factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
|
*
|
||||||
|
* @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the {@code null}。
|
||||||
* @return this.
|
* @return this.
|
||||||
*/
|
*/
|
||||||
public JSONArray addValue(final Object value) {
|
public JSONArray addValue(final Object value) {
|
||||||
@ -124,6 +182,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
|||||||
this.add(this.factory.getMapper().toJSON(value, false));
|
this.add(this.factory.getMapper().toJSON(value, false));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据给定名列表,与其位置对应的值组成JSONObject
|
* 根据给定名列表,与其位置对应的值组成JSONObject
|
||||||
@ -204,6 +263,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
|||||||
return this.raw.set(index, element);
|
return this.raw.set(index, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// region ----- add
|
||||||
@Override
|
@Override
|
||||||
public boolean add(final JSON element) {
|
public boolean add(final JSON element) {
|
||||||
if (null == element && config().isIgnoreNullValue()) {
|
if (null == element && config().isIgnoreNullValue()) {
|
||||||
@ -235,8 +295,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
|||||||
}
|
}
|
||||||
this.add(element);
|
this.add(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({"unchecked"})
|
||||||
|
@ -227,6 +227,70 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置设置{@code null}值。在忽略null模式下移除对应键值对。
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @return this.
|
||||||
|
*/
|
||||||
|
public JSONObject putNull(final String key){
|
||||||
|
this.put(key, null);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param value Boolean类型对象
|
||||||
|
* @return this.
|
||||||
|
* @throws JSONException 值是无穷数字抛出此异常
|
||||||
|
*/
|
||||||
|
public JSONObject putValue(final String key, final Boolean value) throws JSONException {
|
||||||
|
this.put(key, factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param value Number类型对象
|
||||||
|
* @return this.
|
||||||
|
* @throws JSONException 值是无穷数字抛出此异常
|
||||||
|
*/
|
||||||
|
public JSONObject putValue(final String key, final Number value) throws JSONException {
|
||||||
|
this.put(key, factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param value Character类型对象
|
||||||
|
* @return this.
|
||||||
|
* @throws JSONException 值是无穷数字抛出此异常
|
||||||
|
*/
|
||||||
|
public JSONObject putValue(final String key, final Character value) throws JSONException {
|
||||||
|
this.put(key, factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param value String类型对象
|
||||||
|
* @return this.
|
||||||
|
* @throws JSONException 值是无穷数字抛出此异常
|
||||||
|
*/
|
||||||
|
public JSONObject putValue(final String key, final String value) throws JSONException {
|
||||||
|
// put时,不解析字符串,而是作为JSONPrimitive对待
|
||||||
|
this.put(key, factory.ofPrimitive(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
*
|
*
|
||||||
@ -236,6 +300,9 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
* @throws JSONException 值是无穷数字抛出此异常
|
* @throws JSONException 值是无穷数字抛出此异常
|
||||||
*/
|
*/
|
||||||
public JSONObject putValue(final String key, final Object value) throws JSONException {
|
public JSONObject putValue(final String key, final Object value) throws JSONException {
|
||||||
|
if(null == value){
|
||||||
|
return putNull(key);
|
||||||
|
}
|
||||||
// put时,如果value为字符串,不解析,而是作为JSONPrimitive对待
|
// put时,如果value为字符串,不解析,而是作为JSONPrimitive对待
|
||||||
this.put(key, factory.getMapper().toJSON(value, false));
|
this.put(key, factory.getMapper().toJSON(value, false));
|
||||||
return this;
|
return this;
|
||||||
|
@ -300,7 +300,7 @@ public class JSONArrayTest {
|
|||||||
@Test
|
@Test
|
||||||
public void putNullTest() {
|
public void putNullTest() {
|
||||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||||
array.addValue(null);
|
array.addNull();
|
||||||
|
|
||||||
assertEquals("[null]", array.toString());
|
assertEquals("[null]", array.toString());
|
||||||
}
|
}
|
||||||
|
@ -51,22 +51,22 @@ public class JSONNullTest {
|
|||||||
@Test
|
@Test
|
||||||
public void setNullTest(){
|
public void setNullTest(){
|
||||||
// 忽略null
|
// 忽略null
|
||||||
String json1 = JSONUtil.ofObj().putValue("key1", null).toString();
|
String json1 = JSONUtil.ofObj().putNull("key1").toString();
|
||||||
Assertions.assertEquals("{}", json1);
|
Assertions.assertEquals("{}", json1);
|
||||||
|
|
||||||
// 不忽略null
|
// 不忽略null
|
||||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).putValue("key1", null).toString();
|
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).putNull("key1").toString();
|
||||||
Assertions.assertEquals("{\"key1\":null}", json1);
|
Assertions.assertEquals("{\"key1\":null}", json1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setNullOfJSONArrayTest(){
|
public void setNullOfJSONArrayTest(){
|
||||||
// 忽略null
|
// 忽略null
|
||||||
String json1 = JSONUtil.ofArray().addValue(null).toString();
|
String json1 = JSONUtil.ofArray().addNull().toString();
|
||||||
Assertions.assertEquals("[]", json1);
|
Assertions.assertEquals("[]", json1);
|
||||||
|
|
||||||
// 不忽略null
|
// 不忽略null
|
||||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addValue(null).toString();
|
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addNull().toString();
|
||||||
Assertions.assertEquals("[null]", json1);
|
Assertions.assertEquals("[null]", json1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -730,7 +730,7 @@ public class JSONObjectTest {
|
|||||||
@Test
|
@Test
|
||||||
public void nullToEmptyTest() {
|
public void nullToEmptyTest() {
|
||||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false))
|
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false))
|
||||||
.putValue("a", null)
|
.putNull("a")
|
||||||
.putValue("b", "value2");
|
.putValue("b", "value2");
|
||||||
|
|
||||||
final String s = json1.toJSONString(0, (pair) -> {
|
final String s = json1.toJSONString(0, (pair) -> {
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package org.dromara.hutool.json.jmh;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
|
import org.dromara.hutool.core.util.RandomUtil;
|
||||||
|
import org.dromara.hutool.json.JSONArray;
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试JSON树结构转JSON字符串性能
|
||||||
|
*/
|
||||||
|
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||||
|
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||||
|
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||||
|
@Threads(1) //单线程
|
||||||
|
@Fork(1) //
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||||
|
@State(Scope.Benchmark) // 共享域
|
||||||
|
public class JsonAddJmh {
|
||||||
|
|
||||||
|
List<String> testData;
|
||||||
|
private JSONArray hutoolJSON;
|
||||||
|
private JsonArray gson;
|
||||||
|
private com.alibaba.fastjson2.JSONArray fastJSON;
|
||||||
|
private ArrayNode jackson;
|
||||||
|
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
Console.log("准备数据。。。");
|
||||||
|
testData = new ArrayList<>(10);
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
testData.add(RandomUtil.randomString(20));
|
||||||
|
}
|
||||||
|
|
||||||
|
hutoolJSON = new JSONArray();
|
||||||
|
gson = new JsonArray();
|
||||||
|
fastJSON = new com.alibaba.fastjson2.JSONArray();
|
||||||
|
jackson = JsonNodeFactory.instance.arrayNode();
|
||||||
|
Console.log("数据完毕");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void gsonJmh() {
|
||||||
|
testData.forEach(gson::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void hutoolJmh() {
|
||||||
|
testData.forEach(hutoolJSON::addValue);
|
||||||
|
//hutoolJSON.putAllObj(testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UseBulkOperation")
|
||||||
|
@Benchmark
|
||||||
|
public void fastJSONJmh() {
|
||||||
|
testData.forEach(fastJSON::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void jacksonJmh(){
|
||||||
|
testData.forEach(jackson::add);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.dromara.hutool.json.serializer;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.date.DateUtil;
|
||||||
|
import org.dromara.hutool.core.date.StopWatch;
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
|
import org.dromara.hutool.json.JSONFactory;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class JSONMapperTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapper性能耗费较多
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void toJSONTest() {
|
||||||
|
final JSONFactory factory = JSONFactory.getInstance();
|
||||||
|
|
||||||
|
final JSONMapper mapper = factory.getMapper();
|
||||||
|
|
||||||
|
final StopWatch stopWatch = DateUtil.createStopWatch();
|
||||||
|
|
||||||
|
final int count = 1000;
|
||||||
|
stopWatch.start("use mapper");
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
mapper.toJSON("qbw123", false);
|
||||||
|
}
|
||||||
|
stopWatch.stop();
|
||||||
|
|
||||||
|
stopWatch.start("use ofPrimitive");
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
factory.ofPrimitive("qbw123");
|
||||||
|
}
|
||||||
|
stopWatch.stop();
|
||||||
|
|
||||||
|
Console.log(stopWatch.prettyPrint());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user