forked from GoogleCloudPlatform/spring-cloud-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: GoogleCloudPlatform#2576 Added typeadapter to read and write In…
…stant values.
- Loading branch information
Showing
6 changed files
with
224 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ava/com/google/cloud/spring/data/spanner/core/mapping/typeadapter/InstantTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.google.cloud.spring.data.spanner.core.mapping.typeadapter; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
|
||
public class InstantTypeAdapter extends TypeAdapter<Instant> { | ||
@Override | ||
public void write(JsonWriter jsonWriter, Instant instant) throws IOException { | ||
if (instant == null) { | ||
jsonWriter.nullValue(); | ||
} else { | ||
jsonWriter.value(instant.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public Instant read(JsonReader jsonReader) throws IOException { | ||
System.out.println(jsonReader.peek()); | ||
if (jsonReader.peek() == JsonToken.NULL) { | ||
return null; | ||
} | ||
return Instant.parse(jsonReader.nextString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.