Jackson datatype module for Vavr library.
This module enables Jackson to seamlessly serialize and deserialize Vavr's functional data types, including immutable collections, tuples, and control types like Option and Either.
- Jackson 2.x with Vavr 0.x → use vavr-jackson 0.x
- Jackson 3.x with Vavr 0.x → use vavr-jackson 1.x
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-jackson</artifactId>
<version>1.0.0</version>
</dependency>compile("io.vavr:vavr-jackson:1.0.0")Register the VavrModule with your Jackson ObjectMapper:
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule())
.build();import io.vavr.collection.List;
import io.vavr.jackson.datatype.VavrModule;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule())
.build();
String json = mapper.writeValueAsString(List.of(1, 2, 3)); // Result: [1,2,3]
List<Integer> restored = mapper.readValue(json, new TypeReference<>() {}); // Result: List(1, 2, 3)The module provides serialization and deserialization support for:
- Sequences:
List,Array,Vector,Stream,Queue,IndexedSeq - Sets:
HashSet,LinkedHashSet,TreeSet,Set - Maps:
HashMap,LinkedHashMap,TreeMap,Map - Multimaps: All Vavr Multimap implementations
- Other:
PriorityQueue,CharSeq
Option- Optional values that can beSomeorNoneEither- Values that can beLeftorRightLazy- Lazily evaluated values
Tuple0throughTuple8- Immutable tuples with 0 to 8 elements
Function0throughFunction8CheckedFunction0throughCheckedFunction8
You can customize the module behavior using VavrModule.Settings:
By default, Option values are serialized in a plain format (just the value or null). You can change this behavior:
VavrModule.Settings settings = new VavrModule.Settings();
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule(settings))
.build();
mapper.writeValueAsString(Option.of(42)); // Result: 42
mapper.writeValueAsString(Option.none()); // Result: nullVavrModule.Settings settings = new VavrModule.Settings().useOptionInPlainFormat(false);
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule(settings))
.build();
mapper.writeValueAsString(Option.of(42)); // Result: ["defined",42]
mapper.writeValueAsString(Option.none()); // Result: ["undefined"]Configure whether null values should be deserialized as empty collections:
VavrModule.Settings settings = new VavrModule.Settings()
.deserializeNullAsEmptyCollection(true);
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule(settings))
.build();
List<Integer> result = mapper.readValue("null", new TypeReference<>() {});
// Result: List() (empty list instead of null)