JsonUtil Tutorial
This tutorial provides an overview of the JsonUtil
class, which is part of the lightweight-component/aj-util
library. The JsonUtil
class provides utility methods for working with JSON data in Java applications.
Introduction
The JsonUtil
class contains static methods for common JSON operations using Jackson library, including conversion between JSON strings, Java objects, Maps, and Lists.
Main Features
- Convert Java objects to JSON strings (pretty or compact)
- Parse JSON strings into Java objects
- Convert between JSON and Maps/Lists
- Type-safe JSON processing
- Java 8 date/time support
- Strict duplicate detection
Methods
1. Serialization (to JSON)
toJson(Object obj)
- Convert object to JSON stringtoJsonPretty(Object obj)
- Convert object to pretty-printed JSON
2. Deserialization (from JSON)
fromJson(String jsonStr, Class<T> valueType)
- Parse JSON to specified typefromJson(String jsonStr, JavaType valueType)
- Parse JSON with complex type
3. JSON ↔ Map Conversion
json2map(String jsonStr)
- JSON to Map<String, Object>json2map(String jsonStr, Class<T> clazz)
- JSON to Map with typed valuesjson2sortMap(String jsonStr, Class<T> clazz)
- JSON to LinkedHashMap preserving orderjson2StrMap(String jsonStr)
- JSON to Map<String, String>
4. JSON ↔ List Conversion
json2list(String jsonArrayStr, Class<T> clazz)
- JSON array to Listjson2mapList(String jsonArrayStr)
- JSON array to List
5. Object Conversion
convertValue(Object obj, Class<T> clazz)
- Convert object typespojo2map(Object obj)
- Convert POJO to Mappojo2map(Object obj, Class<T> clazz)
- Convert POJO to typed Mapmap2pojo(Map<String, Object> map, Class<T> clazz)
- Convert Map to POJO
6. JSON Tree Model
json2Node(String jsonStr)
- Parse JSON to JsonNode tree
Usage Examples
Basic Serialization
User user = new User("John", 30);
String json = JsonUtil.toJson(user);
// {"name":"John","age":30}
String prettyJson = JsonUtil.toJsonPretty(user);
/*
{
"name" : "John",
"age" : 30
}
*/
Basic Deserialization
String json = "{\"name\":\"John\",\"age\":30}";
User user = JsonUtil.fromJson(json, User.class);
Map Conversion
Map<String, Object> map = new HashMap<>();
map.put("name", "John");
map.put("age", 30);
String json = JsonUtil.toJson(map);
Map<String, Object> map2 = JsonUtil.json2map(json);
List Conversion
String jsonArray = "[{\"name\":\"John\"}, {\"name\":\"Alice\"}]";
List<User> users = JsonUtil.json2list(jsonArray, User.class);
Configuration
The JsonUtil
class is pre-configured with:
- Java 8 date/time support
- Strict duplicate detection
- Asia/Shanghai timezone
- Default ObjectMapper instance
Conclusion
The JsonUtil
class provides comprehensive utility methods for working with JSON data, making it easier to serialize, deserialize, and convert between JSON and Java objects.