0

I have a JSON object which contains primitive fields like int and string and also date field and time field (also as string). Examle of my JSON:

{  
         "auditorium":"506",
         "beginLesson":"10:30",
         "date":"2016.09.12",
         "dayOfWeekString":"Monday",
         "discipline":"Math",
         "endLesson":"11:10",
         "kindOfWork":"Lesson",
         "lecturer":"John Smith"
      }

I am using GSON lib, and I am implementing custom deserializer because I want to get "beginLesson" and "endLesson" fields not as string, but as LocalTime (I am using JodaTime lib). So I get these fields and convert them to LocalTime, but I want other fields to be deserialized as normal (If I simply write fromJson() method, so every field in my class will be set as same value in json). Is there a simple way to get these fields deserialized automatically? Or the only way I have - is to write lots of lines like

MyClass.auditorium = json.get("auditorium"); MyClass.kindOfWork = json.get("kindOfWork");

1
  • 1
    Try checking here. stackoverflow.com/questions/9296427/… I'm not familiar with Gson, Jackson is my JSON library of choice. But I'm sure there is some sort of mechanism for deserializing directly to a class by passing the class object. Jackson uses annotations to mark fields for serialization. So in essense you say something like @JsonProperty("beginLesson") private LocalTime beginLessonLocalTime_; Commented Oct 24, 2016 at 19:43

1 Answer 1

1

The LocalTime deserialization can be handled using custom TypeAdapter. Please refer LocalTimeDeserializer class.

1) Date format is set as "yyyy.MM.dd"

2) org.joda.time.LocalTime - Formatter "HH:mm"

Main Method:-

public static void main(String[] args) {
        String jsonString = "{\"auditorium\":\"506\",\"beginLesson\":\"10:30\",\"date\":\"2016.09.12\",\"dayOfWeekString\":\"Monday\",\"discipline\":\"Math\",\"endLesson\":\"11:10\",\"kindOfWork\":\"Lesson\",\"lecturer\":\"John Smith\"}";
        Gson gson = new GsonBuilder().setDateFormat("yyyy.MM.dd")
                .registerTypeAdapter(LocalTime.class, new LocalTimeDeserializer()).create();

        Auditorium auditorium = gson.fromJson(jsonString, Auditorium.class);
        System.out.println(auditorium.toString());

    }

Auditorium Class:-

public class Auditorium implements Serializable{

    private static final long serialVersionUID = 6752903482239784124L;

    private final DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm");

    private String auditorium;
    private LocalTime beginLesson;
    private Date date;
    private String dayOfWeekString;
    private String discipline;
    private LocalTime endLesson;
    private String kindOfWork;
    private String lecturer;
    @Override
    public String toString() {
        return "Auditorium [auditorium=" + auditorium + ", beginLesson=" + beginLesson.toString(fmt) + ", date=" + date
                + ", dayOfWeekString=" + dayOfWeekString + ", discipline=" + discipline + ", endLesson=" + endLesson.toString(fmt)
                + ", kindOfWork=" + kindOfWork + ", lecturer=" + lecturer + "]";
    }

    ... getter and setters

}

LocalTime Deserializer:-

public class LocalTimeDeserializer implements JsonDeserializer<LocalTime> {

    final DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm");

    @Override
    public LocalTime deserialize(JsonElement paramJsonElement, Type paramType,
            JsonDeserializationContext paramJsonDeserializationContext) throws JsonParseException {

        return LocalTime.parse(paramJsonElement.getAsString(), fmt);
    }

}

Output:-

Auditorium [auditorium=506, beginLesson=10:30, date=Mon Sep 12 00:00:00 BST 2016, dayOfWeekString=Monday, discipline=Math, endLesson=11:10, kindOfWork=Lesson, lecturer=John Smith]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, one more question - why you implemented Serializable in Auditorium class? This is something I should do too or it's just convinient code style for you?
Refer this stackoverflow.com/questions/4548816/…. If your use case doesn't need it, you can remove the Serializable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.