Skip to content

Commit

Permalink
Serialize Instant as epoch milli in json
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Feb 19, 2025
1 parent 4f6a2fa commit 0950471
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@
*/
package org.openhab.core.events;

import java.io.IOException;
import java.time.Instant;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

/**
* The {@link AbstractEventFactory} defines an abstract implementation of the {@link EventFactory} interface. Subclasses
Expand All @@ -38,8 +31,7 @@ public abstract class AbstractEventFactory implements EventFactory {

private final Set<String> supportedEventTypes;

private static final Gson JSONCONVERTER = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantAdapter())
.create();
private static final Gson JSONCONVERTER = new Gson();

/**
* Must be called in subclass constructor to define the supported event types.
Expand Down Expand Up @@ -128,25 +120,4 @@ protected static void checkNotNullOrEmpty(@Nullable String string, String argume
throw new IllegalArgumentException("The argument '" + argumentName + "' must not be null or empty.");
}
}

public static class InstantAdapter extends TypeAdapter<Instant> {

@Override
public void write(JsonWriter out, @Nullable Instant value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(value.toString());
}
}

@Override
public @Nullable Instant read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return Instant.parse(in.nextString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public String getValue() {
private static class ItemStateUpdatedEventPayloadBean {
private @NonNullByDefault({}) String type;
private @NonNullByDefault({}) String value;
private @Nullable Instant lastUpdate;
private @Nullable Long lastUpdate;

/**
* Default constructor for deserialization e.g. by Gson.
Expand All @@ -591,7 +591,7 @@ protected ItemStateUpdatedEventPayloadBean() {
public ItemStateUpdatedEventPayloadBean(String type, String value, @Nullable Instant lastUpdate) {
this.type = type;
this.value = value;
this.lastUpdate = lastUpdate;
this.lastUpdate = lastUpdate != null ? lastUpdate.toEpochMilli() : null;
}

public String getType() {
Expand All @@ -603,7 +603,7 @@ public String getValue() {
}

public @Nullable Instant getLastUpdate() {
return lastUpdate;
return lastUpdate != null ? Instant.ofEpochMilli(lastUpdate) : null;
}
}

Expand Down Expand Up @@ -649,8 +649,8 @@ private static class ItemStateChangedEventPayloadBean {
private @NonNullByDefault({}) String value;
private @NonNullByDefault({}) String oldType;
private @NonNullByDefault({}) String oldValue;
private @Nullable Instant lastStateUpdate;
private @Nullable Instant lastStateChange;
private @Nullable Long lastStateUpdate;
private @Nullable Long lastStateChange;

/**
* Default constructor for deserialization e.g. by Gson.
Expand All @@ -665,8 +665,8 @@ public ItemStateChangedEventPayloadBean(String type, String value, String oldTyp
this.value = value;
this.oldType = oldType;
this.oldValue = oldValue;
this.lastStateUpdate = lastStateUpdate;
this.lastStateChange = lastStateChange;
this.lastStateUpdate = lastStateUpdate != null ? lastStateUpdate.toEpochMilli() : null;
this.lastStateChange = lastStateChange != null ? lastStateChange.toEpochMilli() : null;
}

public String getType() {
Expand All @@ -686,11 +686,11 @@ public String getOldValue() {
}

public @Nullable Instant getLastStateUpdate() {
return lastStateUpdate;
return lastStateUpdate != null ? Instant.ofEpochMilli(lastStateUpdate) : null;
}

public @Nullable Instant getLastStateChange() {
return lastStateChange;
return lastStateChange != null ? Instant.ofEpochMilli(lastStateChange) : null;
}
}

Expand Down

0 comments on commit 0950471

Please sign in to comment.