Visitar URL original
Support timestamp extension in jackson-dataformat-msgpack by komamitsu · Pull Request #677 · msgpack/msgpack-java · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add how to use TimestampExtensionModule in the doc
  • Loading branch information
komamitsu committed Sep 14, 2022
commit ab5284f04163d2152a325f1bea4abf9c36255cea
20 changes: 19 additions & 1 deletion msgpack-jackson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,27 @@ When you want to use non-String value as a key of Map, use `MessagePackKeySerial
System.out.println(mapper.readValue(converted, Pojo.class)); // => Pojo{value=1234567890.98765432100}
```

### Serialize and deserialize Instant instances as MessagePack extension type

`timestamp` extension type is defined in MessagePack as type:-1. Registering `TimestampExtensionModule.INSTANCE` module enables automatic serialization and deserialization of java.time.Instant to/from the MessagePack extension type.

```java
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory())
.registerModule(TimestampExtensionModule.INSTANCE);
Pojo pojo = new Pojo();
// The type of `timestamp` variable is Instant
pojo.timestamp = Instant.now();
byte[] bytes = objectMapper.writeValueAsBytes(pojo);

// The Instant instance is serialized as MessagePack extension type (type: -1)

Pojo deserialized = objectMapper.readValue(bytes, Pojo.class);
System.out.println(deserialized); // "2022-09-14T08:47:24.922Z"
```

### Deserialize extension types with ExtensionTypeCustomDeserializers

`ExtensionTypeCustomDeserializers` helps you to deserialize extension types easily.
`ExtensionTypeCustomDeserializers` helps you to deserialize your own custom extension types easily.

#### Deserialize extension type value directly

Expand Down