vavr-jackson
December 28, 2025 · View on GitHub
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.
Usage
- Jackson 2.x with Vavr 0.x → use vavr-jackson 0.x
- Jackson 3.x with Vavr 0.x → use vavr-jackson 1.x
Maven
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-jackson</artifactId>
<version>1.0.0</version>
</dependency>
Gradle
compile("io.vavr:vavr-jackson:1.0.0")
Registering the Module
Register the VavrModule with your Jackson ObjectMapper:
ObjectMapper mapper = JsonMapper.builder()
.addModule(new VavrModule())
.build();
Basic Usage
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)
Supported Types
The module provides serialization and deserialization support for:
Collections
- 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
Control Types
Option- Optional values that can beSomeorNoneEither- Values that can beLeftorRightLazy- Lazily evaluated values
Tuples
Tuple0throughTuple8- Immutable tuples with 0 to 8 elements
Functions
Function0throughFunction8CheckedFunction0throughCheckedFunction8
Configuration
You can customize the module behavior using VavrModule.Settings:
Plain Option Format
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: null
VavrModule.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"]
Deserialize Null as Empty Collection
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)