carlosjai.me

Tip: [Java / REST] Creating a Jackson multi date format deserializer.

In a microservice that is used by several portals, I have found the need that in certain endpoints, the dates received in the JSON of the requests can have different formats.
This is a problem since the Jackson @JsonFormat tag only allows us to declare a single pattern, like:

	@JsonFormat(pattern = "yyyy-MM-dd")
	private LocalDate incidentDate;

Luckily, there is another tag that allows us to carry out this development: @JsonDeserialize.

	@JsonDeserialize(using = MultiDateDeserializer.class)
	private LocalDate incidentDate;

A@JsonDeserialize allows us to specify a class that will perform custom deserialization. This is very useful in cases like this, where we need to perform additional steps in our requests.

package me.carlosjai.example.utils;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;

import static me.carlosjai.example.utils.DateFormats.INTERNATIONAL_DATE_FORMATS;

public class MultiDateDeserializer extends StdDeserializer<LocalDate> {
	private static final long serialVersionUID = 1L;

	public MultiDateDeserializer() {
		this(null);
	}

	public MultiDateDeserializer(Class<?> vc) {
		super(vc);
	}

	@Override
	/**
	 * Deserializes a custom date in several international date formats
	 */
	public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
		final String date =((JsonNode)jp.getCodec().readTree(jp)).textValue();
		if(date== null || StringUtils.isBlank(date)) {
			return null;
		}
		for (String dateFormat : INTERNATIONAL_DATE_FORMATS) {
			try {
				return LocalDate.parse(date, DateTimeFormatter.ofPattern(dateFormat));
			} catch (DateTimeParseException e) {
			}
		}
		throw new JsonParseException(jp, "Unparseable date: \"" + date + "\". Supported formats: " + Arrays.toString(INTERNATIONAL_DATE_FORMATS));
	}
}

And the DateFormats class:

package me.carlosjai.example.utils;

public class DateFormats {
	public static final String LITHUANIA_DATE_FORMAT = "yyyy-MM-dd";
	public static final String LATVIA_DATE_FORMAT = "dd.MM.yyyy.";
	public static final String[] INTERNATIONAL_DATE_FORMATS = new String[] {
		LITHUANIA_DATE_FORMAT,
		LATVIA_DATE_FORMAT,
	};
}

And… Its done!

The swap memory after this process

How to increase swap size in Ubuntu 20.04

I’m my actual job, I have a very powerful laptop. A Dell Inspiron 15 5000. This laptop was delivered to me the laptop with an Ubuntu distro installed, so I only had to setup all the environments.

Last month I had a lot of performance issues and the system was constantly freezing. First only with Slack notifications, but the last weeks, also with compilations of projects, or chrome tab loadings.

What was the reason for these performance problems? Shit! I only had 2GB of swap memory available. For a computer with 16 GB of RAM, the recomendations are the same amount of swap (1,5x or 2x if you want hibernation), so, I decided to increase my swap file size to 32GB. The steps to do it, are the next:

  • We need to disable the swapping.
$ sudo sudo swapoff -a
  • Next, we need to resize the swap file
$ sudo dd if=/dev/zero of=/swapfile bs=4G count=16
  • Now, we have to change the swap file permissions
$ sudo chmod 600 /swapfile
  • And convert it in swap
$ sudo mkswap /swapfile
  • Activate it
$ sudo swapon /swapfile
  • And add to /etc/fstab if not present
/swapfile none swap sw 0 0

And all my problems of perfomance are solved! You can see the available memory with:

$ sudo free -h

Well, a blog…

I will try to keep this blog updated, I promise. My idea is to post content about travels, food, and well, lots of tips of programming and technology. I want to improve my English, so I think practice is the best way to do it.

See you soon!