<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
</dependency>
public class Cat {
private String name;
private String color;
}
ObjectMapper objectMapper = new ObjectMapper();
Cat cat = new Cat("Barsik", "Black");
objectMapper.writeValue(new File("cat.json"), cat);
{
"name":"Barsik",
"color":"black"
}
String catAsString = objectMapper.writeValueAsString(cat);
String json = "{ \"color\" : \"Black\", \"name\" : \"Barsik\" }";
Cat cat = objectMapper.readValue(json, Cat.class);
Cat cat1 = objectMapper.readValue(new File("json_cat.json"), Cat.class);
Cat cat2 = objectMapper.readValue(new URL("http://test.com/json_cat.json"), Cat.class);
String json = "{ \"color\" : \"Black\", \"name\" : \"Barsik\" }";
JsonNode jsonNode = objectMapper.readTree(json);
String color = jsonNode.get("color").asText();
// результат: color -> Black
String jsonCatArray =
"[{ \"color\" : \"Black\", \"name\" : \"Barsik\" }, { \"color\" : \"White\", \"name\" : \"Vasya\" }]";
List<Cat> listCat = objectMapper.readValue(jsonCatArray, new TypeReference<List<Cat>>(){});
String json = "{ \"color\" : \"Black\", \"name\" : \"Barsik\" }";
Map<String, Object> map
= objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
String jsonString
= "{ \"color\" : \"Black\", \"name\" : \"Barsik\", \"age\" : \"5\" }";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Cat cat = objectMapper.readValue(jsonString, Cat.class);
JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);
JsonNode jsonNodeAge = jsonNodeRoot.get("age");
String age = jsonNodeAge.asText();
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
public class CustomCatSerializer extends StdSerializer<Cat> {
public CustomCatSerializer() {
this(null);
}
public CustomCatSerializer(Class<Cat> t) {
super(t);
}
@Override
public void serialize(
Cat cat, JsonGenerator jsonGenerator, SerializerProvider serializer) {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("name", cat.getName());
jsonGenerator.writeStringField("cat_breed", "Scottish Fold");
jsonGenerator.writeEndObject();
}
}
ObjectMapper mapper = new ObjectMapper();
SimpleModule module =
new SimpleModule("CustomCatSerializer", new Version(1, 0, 0, null, null, null));
module.addSerializer(Cat.class, new CustomCatSerializer());
mapper.registerModule(module);
Cat cat = new Cat("white", "Vasya");
String catJson = mapper.writeValueAsString(cat);
var carJson = {"name":"Vasya","cat_breed":"Scottish Fold"}
public class CustomCatDeserializer extends StdDeserializer<Cat> {
public CustomCatDeserializer() {
this(null);
}
public CustomCatDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Cat deserialize(JsonParser parser, DeserializationContext deserializer) {
Cat cat = new Cat();
ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser);
// try catch block
JsonNode colorNode = node.get("color");
String color = colorNode.asText();
cat.setColor(color);
return cat;
}
}
String json = "{ \"color\" : \"Black\", \"name\" : \"Barsik\" }";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module =
new SimpleModule("CustomCatDeserializer", new Version(1, 0, 0, null, null, null));
module.addDeserializer(Cat.class, new CustomCatDeserializer());
mapper.registerModule(module);
Cat cat = mapper.readValue(json, Cat.class);
public class Student
{
private String name;
private Date birthday;
// getters setters...
}
ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objectMapper.setDateFormat(df);
String studentAsString = objectMapper.writeValueAsString(student);
// результат: {"name":"John", "birthday":"2001-06-18 15:44 AM CEST"}
String jsonCatArray =
"[{ \"color\" : \"Black\", \"name\" : \"Barsik\" }, { \"color\" : \"White\", \"name\" : \"Vasya\" }]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
Cat[] cats = objectMapper.readValue(jsonCatArray, Cat[].class);
List<Cat> listCat = objectMapper.readValue(jsonCatArray, new TypeReference<List<Cat>>(){});