Skip to content
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

Add support to JsonSerializationWriter.WriteNonParsableObjectValue for anonymous objects. #307

Merged
merged 8 commits into from
Jul 31, 2024
33 changes: 32 additions & 1 deletion src/serialization/json/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections;

#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -360,6 +361,33 @@ public void WriteCollectionOfEnumValues<T>(string? key, IEnumerable<T?>? values)
}
}
/// <summary>
/// Writes the specified dictionary to the stream with an optional given key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="values">The dictionary of values to be written.</param>
#if NET5_0_OR_GREATER
public void WriteDictionaryValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string? key, T values) where T : IDictionary
andrueastman marked this conversation as resolved.
Show resolved Hide resolved
#else
public void WriteDictionaryValue<T>(string? key, T values) where T : IDictionary
#endif
{
if(values != null)
{
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
var type = values.GetType();
if(!type.IsGenericType || type.GetGenericArguments()[0] != typeof(string))
throw new InvalidOperationException($"error serialization additional data value with key {key}, unsupported type {type}");

writer.WriteStartObject();
foreach(DictionaryEntry entry in values)
{
WriteAnyValue((string)entry.Key, entry.Value);
}
baywet marked this conversation as resolved.
Show resolved Hide resolved
writer.WriteEndObject();
}
}
/// <summary>
/// Writes the specified byte array as a base64 string to the stream with an optional given key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
Expand Down Expand Up @@ -444,7 +472,7 @@ private void WriteNonParsableObjectValue<T>(string? key, T value)
if(value == null)
writer.WriteNullValue();
else
foreach(var oProp in typeof(T).GetProperties())
foreach(var oProp in value.GetType().GetProperties())
baywet marked this conversation as resolved.
Show resolved Hide resolved
WriteAnyValue(oProp.Name, oProp.GetValue(value));
writer.WriteEndObject();
}
Expand Down Expand Up @@ -512,6 +540,9 @@ private void WriteAnyValue<T>(string? key, T value)
writer.WritePropertyName(key!);
jsonElement.WriteTo(writer);
break;
case IDictionary dictionary:
WriteDictionaryValue(key, dictionary);
break;
case object o:
WriteNonParsableObjectValue(key, o);
break;
Expand Down
9 changes: 8 additions & 1 deletion tests/serialization/json/JsonSerializationWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ public void WritesSampleObjectValue()
{"businessPhones", new List<string>() {"+1 412 555 0109"}}, // write collection of primitives value
{"endDateTime", new DateTime(2023,03,14,0,0,0,DateTimeKind.Utc) }, // ensure the DateTime doesn't crash
{"manager", new TestEntity{Id = "48d31887-5fad-4d73-a9f5-3c356e68a038"}}, // write nested object value
{"anonymousObject", new {Value1 = true, Value2 = "", Value3 = new List<string>{ "Value3.1", "Value3.2"}}}, // write nested object value
{"dictionaryString", new Dictionary<string, string>{{"91bbe8e2-09b2-482b-a90e-00f8d7e81636", "b7992f48-a51b-41a1-ace5-4cebb7f111d0"}, { "ed64c116-2776-4012-94d1-a348b9d241bd", "55e1b4d0-2959-4c71-89b5-385ba5338a1c" }, }}, // write a Dictionary
{"dictionaryTestEntity", new Dictionary<string, TestEntity>{{ "dd476fc9-7e97-4a4e-8d40-6c3de7432eb3", new TestEntity { Id = "dd476fc9-7e97-4a4e-8d40-6c3de7432eb3" } }, { "ffa5c351-7cf5-43df-9b55-e12455cf6eb2", new TestEntity { Id = "ffa5c351-7cf5-43df-9b55-e12455cf6eb2" } }, }}, // write a Dictionary
}
};

using var jsonSerializerWriter = new JsonSerializationWriter();
// Act
jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity);
Expand All @@ -65,7 +69,10 @@ public void WritesSampleObjectValue()
"\"weightInKgs\":51.80," +
"\"businessPhones\":[\"\\u002B1 412 555 0109\"]," +
"\"endDateTime\":\"2023-03-14T00:00:00+00:00\"," +
"\"manager\":{\"id\":\"48d31887-5fad-4d73-a9f5-3c356e68a038\"}" +
"\"manager\":{\"id\":\"48d31887-5fad-4d73-a9f5-3c356e68a038\"}," +
"\"anonymousObject\":{\"Value1\":true,\"Value2\":\"\",\"Value3\":[\"Value3.1\",\"Value3.2\"]}," +
"\"dictionaryString\":{\"91bbe8e2-09b2-482b-a90e-00f8d7e81636\":\"b7992f48-a51b-41a1-ace5-4cebb7f111d0\",\"ed64c116-2776-4012-94d1-a348b9d241bd\":\"55e1b4d0-2959-4c71-89b5-385ba5338a1c\"}," +
"\"dictionaryTestEntity\":{\"dd476fc9-7e97-4a4e-8d40-6c3de7432eb3\":{\"id\":\"dd476fc9-7e97-4a4e-8d40-6c3de7432eb3\"},\"ffa5c351-7cf5-43df-9b55-e12455cf6eb2\":{\"id\":\"ffa5c351-7cf5-43df-9b55-e12455cf6eb2\"}}" +
"}";
Assert.Equal(expectedString, serializedJsonString);
}
Expand Down
Loading