-
Notifications
You must be signed in to change notification settings - Fork 31
Delegate parsing and writing of value types to Gson #2054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
60f1e70
Delegate parsing and writing of value types to Gson
papegaaij bb6e01d
Use Gson for parsing all types, minor code simplification
papegaaij 89f34d6
Make getPrimitiveValue protected to allow sublcasses to change it's b…
papegaaij 5c1ab08
Removed checks for json primitive types before call to Gson
papegaaij 2ef01a1
Delegate writing to gson as well, add tests for writing and parsing a…
papegaaij 16964b2
Delegate byte[] to gson as well for parsing and writing
papegaaij 17dcc48
Revert changes to mock, reintroduce checks for primitive values
papegaaij 0527191
Make code compile with java8 profile
papegaaij e444fda
docs: duplicated doc comment for the gson parameter
baywet c2ac9d6
Fix serialization of date/time types using ISO formats
papegaaij 593bbf7
Add Nonnull to default gson instances
papegaaij File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...erialization/json/src/main/java/com/microsoft/kiota/serialization/DefaultGsonBuilder.java
This file contains hidden or 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,132 @@ | ||
| package com.microsoft.kiota.serialization; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonSyntaxException; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import com.microsoft.kiota.PeriodAndDuration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.LocalTime; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.ZoneOffset; | ||
| import java.time.format.DateTimeParseException; | ||
|
|
||
| public class DefaultGsonBuilder { | ||
|
|
||
| private static final TypeAdapter<OffsetDateTime> OFFSET_DATE_TIME = | ||
| new TypeAdapter<>() { | ||
| @Override | ||
| public OffsetDateTime read(JsonReader in) throws IOException { | ||
| String stringValue = in.nextString(); | ||
| try { | ||
| return OffsetDateTime.parse(stringValue); | ||
| } catch (DateTimeParseException ex) { | ||
| // Append UTC offset if it's missing | ||
| try { | ||
| LocalDateTime localDateTime = LocalDateTime.parse(stringValue); | ||
| return localDateTime.atOffset(ZoneOffset.UTC); | ||
| } catch (DateTimeParseException ex2) { | ||
| throw new JsonSyntaxException( | ||
| "Failed parsing '" | ||
| + stringValue | ||
| + "' as OffsetDateTime; at path " | ||
| + in.getPreviousPath(), | ||
| ex2); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, OffsetDateTime value) throws IOException { | ||
| out.value(value.toString()); | ||
| } | ||
| }; | ||
|
|
||
| private static final TypeAdapter<LocalDate> LOCAL_DATE = | ||
| new TypeAdapter<>() { | ||
| @Override | ||
| public LocalDate read(JsonReader in) throws IOException { | ||
| String stringValue = in.nextString(); | ||
| try { | ||
| return LocalDate.parse(stringValue); | ||
| } catch (DateTimeParseException ex) { | ||
| throw new JsonSyntaxException( | ||
| "Failed parsing '" | ||
| + stringValue | ||
| + "' as LocalDate; at path " | ||
| + in.getPreviousPath(), | ||
| ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, LocalDate value) throws IOException { | ||
| out.value(value.toString()); | ||
| } | ||
| }; | ||
|
|
||
| private static final TypeAdapter<LocalTime> LOCAL_TIME = | ||
| new TypeAdapter<>() { | ||
| @Override | ||
| public LocalTime read(JsonReader in) throws IOException { | ||
| String stringValue = in.nextString(); | ||
| try { | ||
| return LocalTime.parse(stringValue); | ||
| } catch (DateTimeParseException ex) { | ||
| throw new JsonSyntaxException( | ||
| "Failed parsing '" | ||
| + stringValue | ||
| + "' as LocalTime; at path " | ||
| + in.getPreviousPath(), | ||
| ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, LocalTime value) throws IOException { | ||
| out.value(value.toString()); | ||
| } | ||
| }; | ||
|
|
||
| private static final TypeAdapter<PeriodAndDuration> PERIOD_AND_DURATION = | ||
| new TypeAdapter<>() { | ||
| @Override | ||
| public PeriodAndDuration read(JsonReader in) throws IOException { | ||
| String stringValue = in.nextString(); | ||
| try { | ||
| return PeriodAndDuration.parse(stringValue); | ||
| } catch (DateTimeParseException ex) { | ||
| throw new JsonSyntaxException( | ||
| "Failed parsing '" | ||
| + stringValue | ||
| + "' as PeriodAndDuration; at path " | ||
| + in.getPreviousPath(), | ||
| ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, PeriodAndDuration value) throws IOException { | ||
| out.value(value.toString()); | ||
| } | ||
| }; | ||
|
|
||
| private static final Gson defaultInstance = getDefaultBuilder().create(); | ||
|
|
||
| public static Gson getDefaultInstance() { | ||
| return defaultInstance; | ||
| } | ||
|
|
||
| public static GsonBuilder getDefaultBuilder() { | ||
| return new GsonBuilder() | ||
| .registerTypeAdapter(OffsetDateTime.class, OFFSET_DATE_TIME.nullSafe()) | ||
| .registerTypeAdapter(LocalDate.class, LOCAL_DATE.nullSafe()) | ||
| .registerTypeAdapter(LocalTime.class, LOCAL_TIME.nullSafe()) | ||
| .registerTypeAdapter(PeriodAndDuration.class, PERIOD_AND_DURATION.nullSafe()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.