Skip to content

Commit

Permalink
Merge pull request #424 from zhx828/hot-fix
Browse files Browse the repository at this point in the history
Handle when Instant json element is a json object
  • Loading branch information
zhx828 authored Aug 22, 2024
2 parents 3663def + ce5ea2e commit c3ce224
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ public class InstantTypeAdapter implements JsonSerializer<Instant>, JsonDeserial

@Override
public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return Instant.parse(json.getAsString());
if (json.isJsonPrimitive()) {
// Handle the ISO 8601 string format
return Instant.parse(json.getAsString());
} else if (json.isJsonObject()) {
// Handle the {"seconds":..., "nanos":...} object format
JsonObject jsonObject = json.getAsJsonObject();
long seconds = jsonObject.get("seconds").getAsLong();
int nanos = jsonObject.get("nanos").getAsInt();
return Instant.ofEpochSecond(seconds, nanos);
} else {
throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName());
}
}

@Override
Expand Down

0 comments on commit c3ce224

Please sign in to comment.