Coder Social home page Coder Social logo

jsonserializeexample's Introduction

Simple API to test epoch long value to be converted into UTC ISO 8601 — Date and time format

here is simple application where a person object with name and created (epoch long value) attribute is returned from the api expectation of the api response is to present person created value in UTC ISO 8601 — Date and time format String value (yyyy-MM-ddTHH:mm:ssZ)

Below is the code involved

@Data
public class Person {
    String name;
    
    // Serialize long to UTC ISO 8601 — Date and time STRING
    @JsonSerialize(using = CustomEpochSerializer.class)
    Long created;
}

public class CustomEpochSerializer extends StdSerializer<Long> {
    public CustomEpochSerializer() {
        this(null);
    }
    public CustomEpochSerializer(Class<Long> t) {
        super(t);
    }

    @Override
    public void serialize (Long value, JsonGenerator jsonGenerator, SerializerProvider arg)
            throws IOException, JsonProcessingException {
        Instant instant = Instant.ofEpochSecond(value);
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        jsonGenerator.writeString(zdt.toString());
    }
}

@RestController
public class AppController {
    @GetMapping("/person")
    public Person person() {
        Person person = new Person();
        person.setName("hello");
        person.setCreated(1639010825l); // epoch long value
        return person;
    }
}
Output of  API REQUEST GET  /person
        {   
            "name":"hello",
            "created":"2021-12-09T00:47:05Z"
        }
        
note : if there was NO @JsonSerialize for Person object : created property then long value will be returned as-is
        {
            "name":"hello",
            "created":1639010825
        }

Spring would convert the respone of Person java object to JSON API response

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.