forked from openhab/openhab-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[discovery] Fix
Instant
serialization/deserialization regression (o…
…penhab#4029) * Fix Instant serialization/deserialization regression Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> * Consolidate serialization and deserialization in same type adapter Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> * Simplify deserializer Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> --------- Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...storage.json/src/main/java/org/openhab/core/storage/json/internal/InstantTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.storage.json.internal; | ||
|
||
import java.lang.reflect.Type; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
/** | ||
* The {@link InstantTypeAdapter} implements serialization and deserialization of {@link Instant}. | ||
* as formatted UTC strings. | ||
* | ||
* Deserialization supports milliseconds since epoch as well. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class InstantTypeAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> { | ||
/** | ||
* Converts an {@link Instant} to a formatted UTC string. | ||
*/ | ||
@Override | ||
public JsonElement serialize(Instant instant, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(instant.toString()); | ||
} | ||
|
||
/** | ||
* Converts a formatted UTC string to {@link Instant}. | ||
* As fallback, milliseconds since epoch is supported as well. | ||
*/ | ||
@Override | ||
public @Nullable Instant deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) | ||
throws JsonParseException { | ||
try { | ||
return Instant.parse(element.getAsString()); | ||
} catch (DateTimeParseException e) { | ||
// Fallback to milliseconds since epoch for backwards compatibility. | ||
return Instant.ofEpochMilli(element.getAsLong()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters