66 lines
2.7 KiB
Java
66 lines
2.7 KiB
Java
package com.ankurm.jackson3.part4custom;
|
|
|
|
import tools.jackson.core.JsonParser;
|
|
import tools.jackson.databind.DeserializationContext;
|
|
import tools.jackson.databind.ValueDeserializer;
|
|
import tools.jackson.databind.json.JsonMapper;
|
|
import tools.jackson.databind.module.SimpleModule;
|
|
|
|
/**
|
|
* BEYOND THE POST — the deserialising half of E05ValueSerializerDirect.
|
|
*
|
|
* E05 showed ValueSerializer<T> written directly, without the StdSerializer detour.
|
|
* This is the other half: ValueDeserializer<T> written directly, without StdDeserializer.
|
|
* Together they round-trip UserId as a bare JSON string ("u-42") instead of the
|
|
* structural {"value":"u-42"} a record would otherwise produce.
|
|
*/
|
|
public class E06ValueDeserializerDirect {
|
|
|
|
public record UserId(String value) { }
|
|
|
|
static class UserIdSerializer extends tools.jackson.databind.ValueSerializer<UserId> {
|
|
@Override
|
|
public void serialize(UserId id, tools.jackson.core.JsonGenerator gen,
|
|
tools.jackson.databind.SerializationContext ctxt) {
|
|
gen.writeString(id.value());
|
|
}
|
|
}
|
|
|
|
/** Reads the bare JSON string straight back into the wrapper type. */
|
|
static class UserIdDeserializer extends ValueDeserializer<UserId> {
|
|
@Override
|
|
public UserId deserialize(JsonParser p, DeserializationContext ctxt) {
|
|
return new UserId(p.getString());
|
|
}
|
|
}
|
|
|
|
public record Ticket(UserId assignee, String title) { }
|
|
|
|
public static void main(String[] args) {
|
|
SimpleModule module = new SimpleModule("UserIdModule");
|
|
module.addSerializer(UserId.class, new UserIdSerializer());
|
|
module.addDeserializer(UserId.class, new UserIdDeserializer());
|
|
|
|
JsonMapper mapper = JsonMapper.builder().addModule(module).build();
|
|
|
|
Ticket original = new Ticket(new UserId("u-42"), "Fix build");
|
|
String json = mapper.writeValueAsString(original);
|
|
System.out.println("serialised : " + json);
|
|
|
|
Ticket restored = mapper.readValue(json, Ticket.class);
|
|
System.out.println("round-trip : " + restored);
|
|
System.out.println("equal : " + original.equals(restored));
|
|
|
|
// Without the module, Jackson can't turn a bare JSON string "u-42" into a
|
|
// one-component record - it expects {"value":"u-42"} and this throws.
|
|
JsonMapper plain = JsonMapper.builder().build();
|
|
try {
|
|
plain.readValue("{\"assignee\":\"u-42\",\"title\":\"Fix build\"}", Ticket.class);
|
|
System.out.println("no module : (unexpectedly succeeded)");
|
|
} catch (Exception e) {
|
|
System.out.println("no module : " + e.getClass().getSimpleName()
|
|
+ " - " + e.getMessage().lines().findFirst().orElse(""));
|
|
}
|
|
}
|
|
}
|