Jackson Databind Module for Java 8 Optional

Jackson Databind module for serializing and deserializing Java 8 java.util.Option objects. This tool is forked from original source created by @realjenius

License

License

Apache License, Version 2.0
Categories

Categories

Java Languages Data Jackson JSON
GroupId

GroupId

org.zapodot
ArtifactId

ArtifactId

jackson-databind-java-optional
Last Version

Last Version

2.6.1
Release Date

Release Date

Type

Type

jar
Description

Description

Jackson Databind Module for Java 8 Optional
Jackson Databind module for serializing and deserializing Java 8 java.util.Option objects. This tool is forked from original source created by @realjenius
Project URL

Project URL

https://github.com/zapodot/jackson-databind-java-optional
Source Code Management

Source Code Management

https://github.com/zapodot/jackson-databind-java8

Download jackson-databind-java-optional

How to add to project

<!-- https://jarcasting.com/artifacts/org.zapodot/jackson-databind-java-optional/ -->
<dependency>
    <groupId>org.zapodot</groupId>
    <artifactId>jackson-databind-java-optional</artifactId>
    <version>2.6.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.zapodot/jackson-databind-java-optional/
implementation 'org.zapodot:jackson-databind-java-optional:2.6.1'
// https://jarcasting.com/artifacts/org.zapodot/jackson-databind-java-optional/
implementation ("org.zapodot:jackson-databind-java-optional:2.6.1")
'org.zapodot:jackson-databind-java-optional:jar:2.6.1'
<dependency org="org.zapodot" name="jackson-databind-java-optional" rev="2.6.1">
  <artifact name="jackson-databind-java-optional" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.zapodot', module='jackson-databind-java-optional', version='2.6.1')
)
libraryDependencies += "org.zapodot" % "jackson-databind-java-optional" % "2.6.1"
[org.zapodot/jackson-databind-java-optional "2.6.1"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-core jar 2.6.1
com.fasterxml.jackson.core : jackson-databind jar 2.6.1

test (3)

Group / Artifact Type Version
com.fasterxml.jackson.module : jackson-module-parameter-names jar 2.6.1
com.fasterxml.jackson.module : jackson-module-afterburner jar 2.6.1
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

jackson-databind-java-optional

Build Status Coverage Status Maven Central Analytics

A shim library to support mapping Java 8 Optional through Jackson. Forked from @realjenuis jackson-databind-java8 project.

This library is compiled with Java 8 and will thus only be useful in a Java 8 (or higher) runtime environment.

Alternatives

If you are starting a new project that requires support for Java 8 combined with Jackson Databind, you should probably have a look at FasterXML's JDK 8 module.

Usage

The library is distributed through Sonatype's OSS repo

Maven dependency

        <dependency>
            <groupId>org.zapodot</groupId>
            <artifactId>jackson-databind-java-optional</artifactId>
            <version>2.6.1</version>
        </dependency>

SBT

    libraryDependencies += "org.zapodot" % "jackson-databind-java-optional" % "2.6.1"

Registering module

The module is auto-discoverable using the Jackson ObjectMappers's findAndRegisterModules method

final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();

If you are not to crazy about using auto discovery, you can always register the module manually

final ObjectMapper objectMapper = new ObjectMapper()
                                          .registerModule(
                                                new JavaOptionalModule());

Serialization

Empty Optionals will be serialized as JSON nulls. Example:

    public class Bean {

        public static final String PRESENT_VALUE = "present";
        @JsonProperty
        private Optional<String> empty = Optional.empty();

        @JsonProperty
        private Optional<String> notSet;

        @JsonProperty
        private Optional<String> present = Optional.of(PRESENT_VALUE);

        public static void serialize() {
            final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
            final String json = mapper.writeValueAsString(new Bean());
            System.out.println(json); // will print '{"empty":null,"notSet":null,"present":"present"}'
        }


    }

Deserialization

Nulls will be deserialized as Optional.empty() Example:

    public class JavaOptionalDeserializeTest {

        public static class Bean {

            public static final String PRESENT_VALUE = "present";

            @JsonProperty
            private Optional<String> empty = Optional.empty();

            @JsonProperty
            private Optional<String> notSet;

            @JsonProperty
            private Optional<String> present = Optional.of(PRESENT_VALUE);

        }

        @Test
        public void testDeserialize() throws Exception {
            final Bean bean = new ObjectMapper().findAndRegisterModules()
                                                .readValue("{\"empty\":null,\"notSet\":null}", Bean.class);
            assertNotNull(bean.empty);
            assertEquals(Optional.empty(), bean.empty);
            assertNotNull(bean.notSet);
            assertEquals(Optional.empty(), bean.notSet);
            assertEquals(Optional.of(Bean.PRESENT_VALUE), bean.present);

        }
    }

Versions

Version
2.6.1
2.6.0
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0