Java LocalDateTime deserialized using Gson

Java LocalDateTime deserialized using Gson

In Java, you can use the Gson library to serialize and deserialize objects to/from JSON format. When deserializing JSON data that contains a LocalDateTime object, you need to provide a custom deserialization adapter because LocalDateTime does not have a default JSON representation.

Here's an example of how to deserialize a LocalDateTime object using Gson:

  • First, make sure you have the Gson library added to your project's dependencies.

  • Create a custom deserialization adapter for LocalDateTime. You can implement the JsonDeserializer interface and override its deserialize method to parse the LocalDateTime from a JSON string:

import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import java.lang.reflect.Type; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); @Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String dateTimeStr = json.getAsString(); return LocalDateTime.parse(dateTimeStr, formatter); } } 

In this example, we're assuming that the LocalDateTime objects in your JSON data are represented as strings in the format "yyyy-MM-dd'T'HH:mm:ss". You can adjust the DateTimeFormatter pattern according to the actual format used in your JSON data.

  • Use Gson with the custom deserialization adapter when parsing your JSON data:
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.time.LocalDateTime; public class Main { public static void main(String[] args) { // Create Gson instance with custom deserializer Gson gson = new GsonBuilder() .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()) .create(); // JSON data containing LocalDateTime String json = "{\"dateTime\": \"2023-11-27T10:15:30\"}"; // Deserialize JSON to Java object YourClassWithLocalDateTime obj = gson.fromJson(json, YourClassWithLocalDateTime.class); // Access the LocalDateTime object LocalDateTime dateTime = obj.getDateTime(); System.out.println("Parsed LocalDateTime: " + dateTime); } } 

In this example, replace YourClassWithLocalDateTime with the actual class that contains the LocalDateTime field in your JSON data. Gson will use the custom deserialization adapter to convert the JSON string into a LocalDateTime object during deserialization.


More Tags

environment maatwebsite-excel field-description coordinator-layout database-table xcode4 python-datetime word2vec catmull-rom-curve quartz.net

More Java Questions

More Auto Calculators

More Stoichiometry Calculators

More General chemistry Calculators

More Fitness-Health Calculators