Skip to content
A shim library to support mapping Java8 Optionals through Jackson.
Branch: master
Clone or download
Pull request Compare This branch is 87 commits ahead of realjenius:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
README.md
license.txt
pom.xml

README.md

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);

        }
    }
You can’t perform that action at this time.