Representing dates in JSON

JSON is a wonderful interchangeable format to transmit data across heterogeneous services. The JSON spec is really simple and straight forward. However, JSON falls short when representing some primitive data types like date and that gives way to our problem statement: “how do we represent a date in JSON“.

Say we have a JSON blob as follows:

{
  "createdOn" : "2017-07-21"
}

Is the value of createdOn a date? Certainly looks like it but we can’t say for certain. For instance, there is no timezone attached. Or the counts of hours, minutes and seconds for that matter. I suppose two questions arise from this:

  1. How do we define a standard way of defining dates in JSON.
  2. How do we distinguish a string from a date string in JSON.

The first question is a known problem and there has been several proposed solutions. Of those, two are widely used.

  1. Represent the date in ISO 8601 format.
  2. Represent it in epoch time.

The second question is a tad bit trickier. In my opinion, there is no clear solution to it and what you end up with will be very tied to your usecase. For instance, say we have two services communicating with each other in JSON. Both of them are written in JAVA and uses the primitive type Date internally. In that case, we could have a data structure like so

@EqualsAndHashCode
@ToString
@AllArgsConstructor
class MyDate {
  
  @Getter
  private String timestamp;

  public MyDate(final Date date) {
   this.timestamp = dateToEpochTime(date);
  }

All timestamps are communicated as epoch time since human readability is not of concern and the formats are easily interchangeable.

A usecase contrary to this would be a date that’s generated from a browser. It’s expensive to encapsulate the date in an object then. Microsoft came up with a way to distinguish the date strings with liberals that don’t need to be escaped like ‘/’. Of course there are several other schools of thought on the topic. 

To conclude this blog post, I want to put forward an advise: do not use arbitrary types while serializing your JSON payload. It just sets things up for a lot of pain down the line. 

Setup static website hosting with Gitlab pages

Introduction

This article covers how to setup a static website (HTML/Jekyll) with Gitlab and Gitlab-CI. Gitlab is a git repository hosting solution and comes in two flavors: a community edition and an enterprise edition. What’s described in this article is supported by both flavors. Gitlab-CI is an addendum to Gitlab and may be used to setup pipelines and provide continuous integration for code deployments.

Continue reading “Setup static website hosting with Gitlab pages”

My take on tech interview preparation

Happy New Year! One of my resolutions this year is to write more. This is not my first blog, but I feel this will be a blog revamped. I want to write more verbose, meaningful and original posts. With that in mind, for my first post, I wanted to write on a topic that is very close to my heart: tech interviews. They are fun to both take and give, and there are rewards to reap as well. What more can one ask for, right!
So what to expect from this blog post:

  • We will delve into some of the not so well known facets of a tech interview.
  • We will talk about the different directions an interview question may take and discuss that.

Continue reading “My take on tech interview preparation”