Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/serialization/json/spotBugsExcludeFilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
<Or>
<Class name="com.microsoft.kiota.serialization.mocks.IntersectionTypeMock" />
<Class name="com.microsoft.kiota.serialization.mocks.UnionTypeMock" />
<Class name="com.microsoft.kiota.serialization.TestParsable" />
</Or>
</Match>
<Match>
Expand All @@ -31,4 +32,4 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
<Bug pattern="NP_BOOLEAN_RETURN_NULL" />
<Class name="com.microsoft.kiota.serialization.JsonParseNode" />
</Match>
</FindBugsFilter>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
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;
import java.util.Base64;

public class DefaultGsonBuilder {

private static final TypeAdapter<OffsetDateTime> OFFSET_DATE_TIME =
new TypeAdapter<OffsetDateTime>() {
@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<LocalDate>() {
@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<LocalTime>() {
@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<PeriodAndDuration>() {
@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 TypeAdapter<byte[]> BYTE_ARRAY =
new TypeAdapter<byte[]>() {
@Override
public byte[] read(JsonReader in) throws IOException {
String stringValue = in.nextString();
try {
if (stringValue.isEmpty()) {
return null;
}
return Base64.getDecoder().decode(stringValue);
} catch (IllegalArgumentException ex) {
throw new JsonSyntaxException(
"Failed parsing '"
+ stringValue
+ "' as byte[]; at path "
+ in.getPreviousPath(),
ex);
}
}

@Override
public void write(JsonWriter out, byte[] value) throws IOException {
out.value(Base64.getEncoder().encodeToString(value));
}
};

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())
.registerTypeAdapter(byte[].class, BYTE_ARRAY.nullSafe());
}
}
Loading
Loading