Skip to content

Commit

Permalink
Add adapter for java.time.OffsetDateTime (#4007)
Browse files Browse the repository at this point in the history
* add offsetdatetime adapter

* add tests for offsetdatetime adapter

* track api changes for offsetdatetime adapter
  • Loading branch information
benedict-lim authored and martinbonnin committed Apr 11, 2022
1 parent 78dc310 commit 833d8d2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions apollo-adapters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ A few convenient adapters:
| `com.apollographql.apollo3.adapter.JavaLocalDateAdapter` | For `java.time.LocalDate` ISO8601 dates |
| `com.apollographql.apollo3.adapter.KotlinxLocalDateTimeAdapter` | For `kotlinx.datetime.LocalDateTime` ISO8601 dates |
| `com.apollographql.apollo3.adapter.JavaLocalDateTimeAdapter` | For `java.time.LocalDateTime` ISO8601 dates |
| `com.apollographql.apollo3.adapter.JavaOffsetDateTimeAdapter` | For `java.time.OffsetDateTime` ISO8601 dates |
| `com.apollographql.apollo3.adapter.DateAdapter` | For `java.util.Date` ISO8601 dates |
| `com.apollographql.apollo3.adapter.BigDecimalAdapter` | For a Multiplatform `com.apollographql.apollo3.adapter.BigDecimal` class holding big decimal values |
8 changes: 8 additions & 0 deletions apollo-adapters/api/apollo-adapters.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public final class com/apollographql/apollo3/adapter/JavaLocalDateTimeAdapter :
public fun toJson (Lcom/apollographql/apollo3/api/json/JsonWriter;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/time/LocalDateTime;)V
}

public final class com/apollographql/apollo3/adapter/JavaOffsetDateTimeAdapter : com/apollographql/apollo3/api/Adapter {
public static final field INSTANCE Lcom/apollographql/apollo3/adapter/JavaOffsetDateTimeAdapter;
public synthetic fun fromJson (Lcom/apollographql/apollo3/api/json/JsonReader;Lcom/apollographql/apollo3/api/CustomScalarAdapters;)Ljava/lang/Object;
public fun fromJson (Lcom/apollographql/apollo3/api/json/JsonReader;Lcom/apollographql/apollo3/api/CustomScalarAdapters;)Ljava/time/OffsetDateTime;
public synthetic fun toJson (Lcom/apollographql/apollo3/api/json/JsonWriter;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/lang/Object;)V
public fun toJson (Lcom/apollographql/apollo3/api/json/JsonWriter;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/time/OffsetDateTime;)V
}

public final class com/apollographql/apollo3/adapter/KotlinxInstantAdapter : com/apollographql/apollo3/api/Adapter {
public static final field INSTANCE Lcom/apollographql/apollo3/adapter/KotlinxInstantAdapter;
public synthetic fun fromJson (Lcom/apollographql/apollo3/api/json/JsonReader;Lcom/apollographql/apollo3/api/CustomScalarAdapters;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter


/**
Expand Down Expand Up @@ -66,3 +67,21 @@ object JavaLocalDateTimeAdapter : Adapter<LocalDateTime> {
writer.value(value.toString())
}
}

/**
* An [Adapter] that converts a date and time to/from [java.time.OffsetDateTime]
*
* Examples:
* - "2010-06-01T22:19:44.475+01:00"
*
* It requires Android Gradle plugin 4.0 or newer and [core library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring).
*/
object JavaOffsetDateTimeAdapter : Adapter<OffsetDateTime> {
override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): OffsetDateTime {
return OffsetDateTime.parse(reader.nextString()!!)
}

override fun toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: OffsetDateTime) {
writer.value(value.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
}
}
13 changes: 13 additions & 0 deletions apollo-adapters/src/jvmTest/kotlin/JavaTimeAdaptersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.apollographql.apollo3.adapter.DateAdapter
import com.apollographql.apollo3.adapter.JavaInstantAdapter
import com.apollographql.apollo3.adapter.JavaLocalDateAdapter
import com.apollographql.apollo3.adapter.JavaLocalDateTimeAdapter
import com.apollographql.apollo3.adapter.JavaOffsetDateTimeAdapter
import com.apollographql.apollo3.adapter.KotlinxInstantAdapter
import com.apollographql.apollo3.adapter.KotlinxLocalDateAdapter
import com.apollographql.apollo3.adapter.KotlinxLocalDateTimeAdapter
Expand Down Expand Up @@ -44,6 +45,18 @@ class JavaTimeAdaptersTest {
assertEquals("2010-06-01T22:19:44.475Z", JavaInstantAdapter.toJson(instant))
}

@Test
fun offsetDateTime() {
var offsetDateTime = JavaOffsetDateTimeAdapter.fromJson("2010-06-01T23:19:44.475+01:00")
assertEquals(1275430784475, offsetDateTime.toInstant().toEpochMilli())
// Offset is retained
assertEquals("2010-06-01T23:19:44.475+01:00", JavaOffsetDateTimeAdapter.toJson(offsetDateTime))

offsetDateTime = JavaOffsetDateTimeAdapter.fromJson("2010-06-01T22:19:44.475Z")
assertEquals(1275430784475, offsetDateTime.toInstant().toEpochMilli())
assertEquals("2010-06-01T22:19:44.475Z", JavaOffsetDateTimeAdapter.toJson(offsetDateTime))
}

@Test
fun date() {
var date = DateAdapter.fromJson("2010-06-01T22:19:44.475Z")
Expand Down

0 comments on commit 833d8d2

Please sign in to comment.