Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discovery] Fix Instant serialization/deserialization regression #4029

Merged
merged 3 commits into from Jan 12, 2024

Conversation

jlaur
Copy link
Contributor

@jlaur jlaur commented Jan 9, 2024

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
@jlaur jlaur requested a review from a team as a code owner January 9, 2024 20:23
@jlaur
Copy link
Contributor Author

jlaur commented Jan 9, 2024

@J-N-K - this supports:

backwards compatibility:

      "timestamp": 1699728096882,

current:

      "timestamp": "2024-01-09T20:17:27.818829400Z",

But doesn't support:

      "timestamp": {
        "seconds": 1704747106,
        "nanos": 550136800
      },

which by mistake was the result of serialization since yesterday's snapshot. Do you think we need to support this as well? If not, inbox results produced since yesterday cannot be parsed and will need to be manually removed from the JSON file. OTOH, this would introduce some technical debt for supporting a few days snapshot issues. WDYT?

@J-N-K J-N-K added the rebuild Triggers the Jenkins PR build label Jan 10, 2024
public class InstantSerializer implements JsonSerializer<Instant> {
@Override
public JsonElement serialize(Instant instant, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(instant.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the standard serializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default this would be produced (in Java 17):

"timestamp": {
  "seconds": 1704747106,
  "nanos": 550136800
}

Since Java 17 JDK internals are strongly encapsulated so this is from using a reflection-based type adapter, i.e. we would be relying on implementation details outside of our control.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I agree. But why don't we use Instant.toEpochMilli und Instant.fromEpochMilli? This is immediately backward compatible.

Copy link
Contributor Author

@jlaur jlaur Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean in the type adapter while still keeping Instant in the DTO:

?

We could, but the only advantage IMO would be that the deserializer is a bit simpler because it doesn't need to support both old and new format. Over time, the code for converting the old format will be executed less and less since the new format is parsed first, so in terms of performance it shouldn't be a big deal.

The disadvantage would be still keeping epoch milliseconds which are less readable when inspecting the JSON file.

In both cases we need to parse a string, so the epoch version of the deserializer would still need to be:

        try {
            return Instant.ofEpochMilli(Long.parseLong(element.getAsString()));
        } catch (NumberFormatException e) {
            throw new JsonParseException("Could not parse as Instant: " + content, e);
        }

(like in the fallback in the current implementation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing that made me wonder about other possibilities. so just pushed a simplification:

return Instant.ofEpochMilli(element.getAsLong());

@J-N-K J-N-K added bug An unexpected problem or unintended behavior of the Core and removed rebuild Triggers the Jenkins PR build labels Jan 10, 2024
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/latest-snapshot-error/152934/2

@J-N-K J-N-K merged commit 6867f1e into openhab:main Jan 12, 2024
3 checks passed
@J-N-K J-N-K added this to the 4.2 milestone Jan 12, 2024
@jlaur jlaur deleted the discovery-storage branch January 12, 2024 19:48
cipianpascu pushed a commit to cipianpascu/openhab-core that referenced this pull request Jan 17, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of the Core regression
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants