Skip to content

Commit 16964b2

Browse files
committed
Delegate byte[] to gson as well for parsing and writing
1 parent 2ef01a1 commit 16964b2

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

components/serialization/json/src/main/java/com/microsoft/kiota/serialization/DefaultGsonBuilder.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.time.OffsetDateTime;
1616
import java.time.ZoneOffset;
1717
import java.time.format.DateTimeParseException;
18+
import java.util.Base64;
1819

1920
public class DefaultGsonBuilder {
2021

@@ -116,6 +117,32 @@ public void write(JsonWriter out, PeriodAndDuration value) throws IOException {
116117
}
117118
};
118119

120+
private static final TypeAdapter<byte[]> BYTE_ARRAY =
121+
new TypeAdapter<>() {
122+
@Override
123+
public byte[] read(JsonReader in) throws IOException {
124+
String stringValue = in.nextString();
125+
try {
126+
if (stringValue.isEmpty()) {
127+
return null;
128+
}
129+
return Base64.getDecoder().decode(stringValue);
130+
} catch (IllegalArgumentException ex) {
131+
throw new JsonSyntaxException(
132+
"Failed parsing '"
133+
+ stringValue
134+
+ "' as byte[]; at path "
135+
+ in.getPreviousPath(),
136+
ex);
137+
}
138+
}
139+
140+
@Override
141+
public void write(JsonWriter out, byte[] value) throws IOException {
142+
out.value(Base64.getEncoder().encodeToString(value));
143+
}
144+
};
145+
119146
private static final Gson defaultInstance = getDefaultBuilder().create();
120147

121148
public static Gson getDefaultInstance() {
@@ -127,6 +154,7 @@ public static GsonBuilder getDefaultBuilder() {
127154
.registerTypeAdapter(OffsetDateTime.class, OFFSET_DATE_TIME.nullSafe())
128155
.registerTypeAdapter(LocalDate.class, LOCAL_DATE.nullSafe())
129156
.registerTypeAdapter(LocalTime.class, LOCAL_TIME.nullSafe())
130-
.registerTypeAdapter(PeriodAndDuration.class, PERIOD_AND_DURATION.nullSafe());
157+
.registerTypeAdapter(PeriodAndDuration.class, PERIOD_AND_DURATION.nullSafe())
158+
.registerTypeAdapter(byte[].class, BYTE_ARRAY.nullSafe());
131159
}
132160
}

components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.time.LocalTime;
1616
import java.time.OffsetDateTime;
1717
import java.util.ArrayList;
18-
import java.util.Base64;
1918
import java.util.EnumSet;
2019
import java.util.HashMap;
2120
import java.util.Iterator;
@@ -312,10 +311,6 @@ public void setOnAfterAssignFieldValues(@Nullable final Consumer<Parsable> value
312311
}
313312

314313
@Nullable public byte[] getByteArrayValue() {
315-
final String base64 = this.getStringValue();
316-
if (base64 == null || base64.isEmpty()) {
317-
return null;
318-
}
319-
return Base64.getDecoder().decode(base64);
314+
return gson.fromJson(currentNode, byte[].class);
320315
}
321316
}

components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonSerializationWriter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.time.LocalDate;
1919
import java.time.LocalTime;
2020
import java.time.OffsetDateTime;
21-
import java.util.Base64;
2221
import java.util.EnumSet;
2322
import java.util.List;
2423
import java.util.Map;
@@ -540,6 +539,14 @@ public void setOnStartObjectSerialization(
540539
}
541540

542541
public void writeByteArrayValue(@Nullable final String key, @Nullable final byte[] value) {
543-
if (value != null) this.writeStringValue(key, Base64.getEncoder().encodeToString(value));
542+
if (value != null)
543+
try {
544+
if (key != null && !key.isEmpty()) {
545+
writer.name(key);
546+
}
547+
gson.getAdapter(byte[].class).write(writer, value);
548+
} catch (IOException ex) {
549+
throw new RuntimeException("could not serialize value", ex);
550+
}
544551
}
545552
}

components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonSerializationWriterTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.kiota.serialization;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56

@@ -268,6 +269,10 @@ void parseWrittenValues() throws IOException {
268269
ParseNode::getPeriodAndDurationValue,
269270
SerializationWriter::writePeriodAndDurationValue,
270271
PeriodAndDuration.of(Period.ofYears(3), Duration.ofHours(6)));
272+
writeAndParse(
273+
ParseNode::getByteArrayValue,
274+
SerializationWriter::writeByteArrayValue,
275+
new byte[] {8, 10, 127, -50, 0});
271276
}
272277

273278
private <T> void writeAndParse(
@@ -283,7 +288,11 @@ private <T> void writeAndParse(
283288
var parseNode =
284289
parseNodeFactory.getParseNode("application/json", writer.getSerializedContent());
285290
var result = parseNode.getObjectValue(TestParsable.factory(parseMethod, writeMethod));
286-
assertEquals(value, result.getRealValue());
291+
if (value instanceof byte[] bytea) {
292+
assertArrayEquals(bytea, (byte[]) result.getRealValue());
293+
} else {
294+
assertEquals(value, result.getRealValue());
295+
}
287296
assertNull(result.getNullValue());
288297
writer.close();
289298
}

0 commit comments

Comments
 (0)