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:
List, Array, Vector, Stream, Queue, IndexedSeqHashSet, LinkedHashSet, TreeSet, SetHashMap, LinkedHashMap, TreeMap, MapPriorityQueue, CharSeqOption - Optional values that can be Some or NoneEither - Values that can be Left or RightLazy - Lazily evaluated valuesTuple0 through Tuple8 - Immutable tuples with 0 to 8 elementsFunction0 through Function8CheckedFunction0 through CheckedFunction8You 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: 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"]
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)
Java
99.9%
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:
List, Array, Vector, Stream, Queue, IndexedSeqHashSet, LinkedHashSet, TreeSet, SetHashMap, LinkedHashMap, TreeMap, MapPriorityQueue, CharSeqOption - Optional values that can be Some or NoneEither - Values that can be Left or RightLazy - Lazily evaluated valuesTuple0 through Tuple8 - Immutable tuples with 0 to 8 elementsFunction0 through Function8CheckedFunction0 through CheckedFunction8You 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: 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"]
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)
Java
99.9%