Git Product home page Git Product logo

jackson-databind-java-optional's Introduction

jackson-databind-java-optional

Build Status Coverage Status Maven Central

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.

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.4.5</version>
        </dependency>

SBT

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

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

        }
    }

jackson-databind-java-optional's People

Contributors

zapodot avatar michaelbitard avatar realjenius avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.