Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.microsoft.kiota.serialization;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.stream.JsonWriter;
import com.microsoft.kiota.PeriodAndDuration;

Expand Down Expand Up @@ -474,12 +475,22 @@ private void writeNonParsableObject(@Nullable final String key, @Nullable final
}
}

private void writeJsonElement(@Nullable final String key, @Nonnull final JsonElement value) {
try {
if (key != null && !key.isEmpty()) writer.name(key);
gson.getAdapter(JsonElement.class).write(writer, value);
} catch (IOException ex) {
throw new RuntimeException("could not serialize value", ex);
}
}

private void writeAnyValue(@Nullable final String key, @Nullable final Object value) {
if (value == null) {
this.writeNullValue(key);
} else {
final Class<?> valueClass = value.getClass();
if (valueClass.equals(String.class)) this.writeStringValue(key, (String) value);
if (value instanceof JsonElement) this.writeJsonElement(key, (JsonElement) value);
else if (valueClass.equals(String.class)) this.writeStringValue(key, (String) value);
else if (valueClass.equals(Boolean.class)) this.writeBooleanValue(key, (Boolean) value);
else if (valueClass.equals(Byte.class)) this.writeByteValue(key, (Byte) value);
else if (valueClass.equals(Short.class)) this.writeShortValue(key, (Short) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.google.gson.JsonParser;
import com.microsoft.kiota.Compatibility;
import com.microsoft.kiota.PeriodAndDuration;
import com.microsoft.kiota.serialization.mocks.MyEnum;
import com.microsoft.kiota.serialization.mocks.TestEntity;
import com.microsoft.kiota.serialization.mocks.UntypedTestEntity;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -26,6 +29,31 @@
import java.util.UUID;

class JsonSerializationWriterTests {
@ParameterizedTest
@ValueSource(
strings = {
"{\"@odata.type\":\"#CustomSecurityAttributeValue\",\"ids\":[\"123\",\"321\"],\"nested\":{\"nil\":null}}",
"[1,false,null,{\"name\":\"test\"}]",
"\"quoted \\\" text\"",
"false",
"123456789012345678901234567890",
"null",
"{}",
"[]"
})
void serializesJsonElementsInAdditionalData(String json) throws IOException {
var entity = new TestEntity();
entity.getAdditionalData().put("extra", JsonParser.parseString(json));
try (var serializer = new JsonSerializationWriter()) {
serializer.writeObjectValue(null, entity);
var actual =
new String(
Compatibility.readAllBytes(serializer.getSerializedContent()), "UTF-8");
assertEquals(
JsonParser.parseString("{\"extra\":" + json + "}"),
JsonParser.parseString(actual));
}
}

@Test
void writesSampleObjectValueWithPrimitivesInAdditionalData() throws IOException {
Expand Down
Loading