http-api: encode json Instants as millis since epoch
This commit is contained in:
@@ -43,12 +43,7 @@ public class InstantTypeAdapter extends TypeAdapter<Instant>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.beginObject()
|
out.value(value.toEpochMilli());
|
||||||
.name("seconds")
|
|
||||||
.value(value.getEpochSecond())
|
|
||||||
.name("nanos")
|
|
||||||
.value(value.getNano())
|
|
||||||
.endObject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -60,6 +55,12 @@ public class InstantTypeAdapter extends TypeAdapter<Instant>
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (in.peek() == JsonToken.NUMBER)
|
||||||
|
{
|
||||||
|
long jsTime = in.nextLong();
|
||||||
|
return Instant.ofEpochMilli(jsTime);
|
||||||
|
}
|
||||||
|
|
||||||
long seconds = 0;
|
long seconds = 0;
|
||||||
int nanos = 0;
|
int nanos = 0;
|
||||||
in.beginObject();
|
in.beginObject();
|
||||||
|
|||||||
@@ -34,15 +34,20 @@ public class InstantTypeAdapterTest
|
|||||||
@Test
|
@Test
|
||||||
public void test()
|
public void test()
|
||||||
{
|
{
|
||||||
test("null", null);
|
test("null", null, true);
|
||||||
test("{\"seconds\":1609538310,\"nanos\":291698903}", Instant.ofEpochSecond(1609538310, 291698903));
|
test("{\"seconds\":1609538310,\"nanos\":291000000}", Instant.ofEpochSecond(1609538310, 291_000_000), false);
|
||||||
|
test("1609538310291", Instant.ofEpochSecond(1609538310, 291_000_000), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void test(String json, Instant object)
|
private void test(String json, Instant object, boolean exactEncoding)
|
||||||
{
|
{
|
||||||
Instant parsed = RuneLiteAPI.GSON.fromJson(json, Instant.class);
|
Instant parsed = RuneLiteAPI.GSON.fromJson(json, Instant.class);
|
||||||
Assert.assertEquals(object, parsed);
|
Assert.assertEquals(object, parsed);
|
||||||
String serialized = RuneLiteAPI.GSON.toJson(object);
|
String serialized = RuneLiteAPI.GSON.toJson(object);
|
||||||
|
if (exactEncoding)
|
||||||
|
{
|
||||||
|
Assert.assertEquals(json, serialized);
|
||||||
|
}
|
||||||
Instant roundTripped = RuneLiteAPI.GSON.fromJson(serialized, Instant.class);
|
Instant roundTripped = RuneLiteAPI.GSON.fromJson(serialized, Instant.class);
|
||||||
Assert.assertEquals(object, roundTripped);
|
Assert.assertEquals(object, roundTripped);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ package net.runelite.http.service.loottracker;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoField;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -88,7 +89,7 @@ public class LootTrackerControllerTest
|
|||||||
{
|
{
|
||||||
LootRecord lootRecord = new LootRecord();
|
LootRecord lootRecord = new LootRecord();
|
||||||
lootRecord.setType(LootRecordType.NPC);
|
lootRecord.setType(LootRecordType.NPC);
|
||||||
lootRecord.setTime(Instant.now());
|
lootRecord.setTime(Instant.now().with(ChronoField.NANO_OF_SECOND, 0));
|
||||||
lootRecord.setDrops(Collections.singletonList(new GameItem(4151, 1)));
|
lootRecord.setDrops(Collections.singletonList(new GameItem(4151, 1)));
|
||||||
|
|
||||||
String data = RuneLiteAPI.GSON.toJson(Collections.singletonList(lootRecord));
|
String data = RuneLiteAPI.GSON.toJson(Collections.singletonList(lootRecord));
|
||||||
|
|||||||
Reference in New Issue
Block a user