add HutoolJSONSerializer

This commit is contained in:
Looly
2024-11-11 19:35:39 +08:00
parent 7d8b95f108
commit 3dc6314b56
4 changed files with 91 additions and 3 deletions

View File

@@ -17,6 +17,8 @@
package org.dromara.hutool.json.engine;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -39,4 +41,20 @@ public class JacksonTest {
" \"gender\" : true\n" +
"}", jsonString);
}
/**
* https://gitee.com/dromara/hutool/issues/IB3GM4<br>
* JSON和Jackson兼容
*/
@Test
void toJsonStringOfHutoolJsonTest() {
final JSONObject jsonObject = JSONUtil.ofObj()
.putValue("name", "张三")
.putValue("age", 18)
.putValue("sub", JSONUtil.ofObj()
.putValue("aaa", "aa1").putValue("bbb", "bb1"));
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
final String jsonString = engine.toJsonString(jsonObject);
Assertions.assertEquals("{\"name\":\"张三\",\"age\":18,\"sub\":{\"aaa\":\"aa1\",\"bbb\":\"bb1\"}}", jsonString);
}
}