json deserialization fail for valid format

405 Views Asked by At

I have a json file with a date like this:

{
...
  "dob":"20001010",
  "registerdate":"20001010121212",
...
}

i am using in class:

public class User implements Serializable {
... some other
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd
    public java.util.Date dob; // this works

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
    public java.util.Date registerdate;
}

then in my test i use Gson to convert the file

User user = new Gson().fromJson("user.json", User.class);

but when this run, i get:

com.google.gson.JsonSyntaxException: 20001010121212

I treid with: yyyyMMddHHmmss or YYYYMMddHHmmss or YYYYMMDDHHmmss same results

pom:

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

2

There are 2 best solutions below

2
On BEST ANSWER

Basically com.google.gson.Gson does not recognize annotation com.fasterxml.jackson.annotation.JsonFormat (two different libs)

Here is another solution using just ObjectMapper from fasterxml lib

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.Serializable;
import java.util.Date;

public class DemoApplication {
    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\"dob\":\"20001010121212\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user.dob);
    }
}

class User implements Serializable {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
    public Date dob;
}
3
On

If you are using GSON, you need to parse date something like

import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GSONExample {

    private static String json = "{\r\n" + "  \"dob\":\"20001010121212\"\r\n" + "}";

    public static void main(String[] args) {
        Gson gson = new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create();
        ItemDob itemDob = gson.fromJson(json, ItemDob.class);
        System.out.println(itemDob.getDob());
    }

}

class ItemDob {
    private Date dob;

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }
}

Output

Tue Oct 10 12:12:12 IST 2000

EDIT If you want to specify date formats using annotations, use jackson. For eg,

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonExample {

    private static String json = "{\r\n" + 
            "  \"dob\": \"20001010121212\"\r\n" + 
            "}";

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        ItemDob itemDob = new ObjectMapper().readValue(json, ItemDob.class);
        System.out.println(itemDob.getDob());
    }

}

class ItemDob {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
    private Date dob;

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }
}

Output:

Tue Oct 10 17:42:12 IST 2000