Git Product home page Git Product logo

gson-java8-datatype's Introduction

Java8 data type(Optional and DateTime) module for gson

Get from maven central repo

<dependency>
    <groupId>net.dongliu</groupId>
    <artifactId>gson-java8-datatype</artifactId>
    <version>1.1.0</version>
</dependency>

Register

Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()).create();

Optional Types

Empty Optional value is treated as null; Non-empty optional value is treat as optional.get()

gson.toJson(OptionalInt.of(10)); // 10
gson.fromJson("10", OptionalInt.class); // = OptionalInt.of(10)
gson.toJson(OptionalInt.empty()); // null
gson.fromJson("null", OptionalInt.class); // = OptionalInt.empty()

gson.toJson(Optional.of("test")); // "test"
gson.toJson(Optional.of(Optional.of("test"))); // "test"

gson.toJson(Optional.empty()); // null
gson.toJson(Optional.of(Optional.empty())); // null
gson.fromJson("\"test\"", new TypeToken<Optional<String>>() {}.getType()); // = Optional.of("test")
gson.fromJson("\"test\"", new TypeToken<Optional<Optional<String>>>() {}.getType()); // =Optional.of(Optional.of("test"))
gson.fromJson("null", new TypeToken<Optional<String>>() {}.getType()); // = Optional.empty()
gson.fromJson("null", new TypeToken<Optional<Optional<String>>>() {}.getType()); // = Optional.empty()
gson.fromJson("null", Optional.class); // = Optional.empty()

DateTime Types

Default Setting

Java8 new datetime types serialized using ISO-9601 format by default.

ZoneId zoneId = ZoneId.of("Asia/Shanghai");
Instant instant = Instant.ofEpochMilli(1457595643101L);
gson.toJson(instant); // "2016-03-10T07:40:43.101Z"
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
gson.toJson(zonedDateTime); // "2016-03-10T15:40:43.101+08:00[Asia/Shanghai]"
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
gson.toJson(localDateTime); // "2016-03-10T15:40:43.101"
LocalDate localDate = localDateTime.toLocalDate();
gson.toJson(localDate); // "2016-03-10"
LocalTime localTime = localDateTime.toLocalTime();
gson.toJson(localTime); // "15:40:43.101"
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, zoneId);
gson.toJson(offsetDateTime); // "2016-03-10T15:40:43.101+08:00"
OffsetTime offsetTime = offsetDateTime.toOffsetTime();
gson.toJson(offsetTime); // "15:40:43.101+08:00"

YearMonth yearMonth = YearMonth.of(2016, 3);
gson.toJson(yearMonth); // "2016-03"
Year year = Year.of(2016);
gson.toJson(year); // "2016"

Period period = Period.ofDays(1);
gson.toJson(period); // "P1D"
Duration duration = Duration.ofDays(1);
gson.toJson(period); // "P1D"

Custom DateTimeFormatter

GsonJava8TypeAdapterFactory has methods for setting custom DateTimeFormatter of Type Instant/LocalDateTime/OffsetDateTime etc..

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH)
        .withZone(ZoneId.of("Asia/Shanghai"));
GsonJava8TypeAdapterFactory typeAdapterFactory = new GsonJava8TypeAdapterFactory()
        .setInstantFormatter(formatter);
Gson gson = new GsonBuilder().registerTypeAdapterFactory(typeAdapterFactory).create();
Instant instant = Instant.ofEpochMilli(1457595643101L);
assertEquals("\"2016-03-10 15:40:43.101\"", gson.toJson(instant));
assertEquals(instant, gson.fromJson("\"2016-03-10 15:40:43.101\"", Instant.class));

gson-java8-datatype's People

Contributors

hsiafan avatar twz123 avatar

Watchers

 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.