Hallo,
ich versuche eine Klasse mit einer LocalDateTime Variable zu serialisieren und dann wieder deserialisieren.
Wo habe ich meinen Fehler?
Bekomme aber immer den Error:
java.time.format.DateTimeParseException: Text '18::Mai::2021 11::33::37' could not be parsed at index 4
at energy.csv.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:17)
[CODE lang="java" title="Main Klasse"]private class jsonTest {
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").enableComplexMapKeySerialization()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).setPrettyPrinting().create();
private Values values = new Values();
...
String gs = gson.toJson(values);
...
values = gson.fromJson(gs, Values.class);
}
public class Values {
public HashMap<LocalDateTime, Value> values = new HashMap<LocalDateTime, Value>();
public HashMap<Integer, String> names = new HashMap<Integer, String>();
}
public class Value {
public HashMap<Integer, Double> value = new HashMap<Integer, Double>();
}
class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss");
@Override
public JsonElement serialize(LocalDateTime localDateTime, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(formatter.format(localDateTime));
}
}
class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return LocalDateTime.parse(json.getAsString(),
DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss").withLocale(Locale.ENGLISH));
}
}[/CODE]
ich versuche eine Klasse mit einer LocalDateTime Variable zu serialisieren und dann wieder deserialisieren.
Wo habe ich meinen Fehler?
Bekomme aber immer den Error:
java.time.format.DateTimeParseException: Text '18::Mai::2021 11::33::37' could not be parsed at index 4
at energy.csv.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:17)
[CODE lang="java" title="Main Klasse"]private class jsonTest {
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").enableComplexMapKeySerialization()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).setPrettyPrinting().create();
private Values values = new Values();
...
String gs = gson.toJson(values);
...
values = gson.fromJson(gs, Values.class);
}
public class Values {
public HashMap<LocalDateTime, Value> values = new HashMap<LocalDateTime, Value>();
public HashMap<Integer, String> names = new HashMap<Integer, String>();
}
public class Value {
public HashMap<Integer, Double> value = new HashMap<Integer, Double>();
}
class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss");
@Override
public JsonElement serialize(LocalDateTime localDateTime, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(formatter.format(localDateTime));
}
}
class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return LocalDateTime.parse(json.getAsString(),
DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss").withLocale(Locale.ENGLISH));
}
}[/CODE]