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

Improving serialization #311

Merged
merged 12 commits into from
Aug 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ public static partial class KiotaJsonSerializer
/// </summary>
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(T value) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value);
public static Stream SerializeAsStream<T>(T value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the specified object as a string based JSON stream.
/// </summary>
/// <typeparam name="T">The type of the value to serialize.</typeparam>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties</param>
/// <returns>A <see cref="Stream"/> containing the serialized JSON data.</returns>

public static Stream SerializeAsJsonStream<T>(this T value, bool serializeOnlyChangedValues = true) where T : IParsable
svrooij marked this conversation as resolved.
Show resolved Hide resolved
=> SerializeAsStream(value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a string based on the content type.
Expand All @@ -49,9 +61,21 @@ public static Task<string> SerializeAsStringAsync<T>(T value, CancellationToken
/// </summary>
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(IEnumerable<T> value) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value);
public static Stream SerializeAsStream<T>(IEnumerable<T> value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the specified object as a string based JSON stream.
/// </summary>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <param name="value">The enumerable of objects to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties</param>
/// <returns>A <see cref="Stream"/> containing the serialized JSON data.</returns>

public static Stream SerializeAsJsonStream<T>(this IEnumerable<T> value, bool serializeOnlyChangedValues = true) where T : IParsable
=> SerializeAsStream(value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a string based on the content type.
Expand Down
40 changes: 34 additions & 6 deletions src/abstractions/serialization/KiotaSerializer.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Kiota.Abstractions.Store;

#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
Expand All @@ -25,12 +27,25 @@ public static partial class KiotaSerializer
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="contentType">Content type to serialize the object to </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(string contentType, T value) where T : IParsable
public static Stream SerializeAsStream<T>(string contentType, T value, bool serializeOnlyChangedValues = true) where T : IParsable
{
bool restoreInitializationCompleted = false;
if(!serializeOnlyChangedValues && value is IBackedModel backedModel)
svrooij marked this conversation as resolved.
Show resolved Hide resolved
{
// reset the initialization completed flag to ensure all properties are serialized
restoreInitializationCompleted = backedModel.BackingStore.InitializationCompleted;
backedModel.BackingStore.InitializationCompleted = false;
}
using var writer = GetSerializationWriter(contentType, value);
writer.WriteObjectValue(string.Empty, value);
return writer.GetSerializedContent();
writer.WriteObjectValue(null, value);
var stream = writer.GetSerializedContent();
if(restoreInitializationCompleted)
{
(value as IBackedModel)!.BackingStore.InitializationCompleted = true;
}
return stream;
}
/// <summary>
/// Serializes the given object into a string based on the content type.
Expand Down Expand Up @@ -64,12 +79,25 @@ public static Task<string> SerializeAsStringAsync<T>(string contentType, T value
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="contentType">Content type to serialize the object to </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">By default you'll only get the changed properties.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(string contentType, IEnumerable<T> value) where T : IParsable
public static Stream SerializeAsStream<T>(string contentType, IEnumerable<T> value, bool serializeOnlyChangedValues = true) where T : IParsable
{
bool resetInitializationCompleted = false;
if(!serializeOnlyChangedValues && value is IBackedModel backedModel)
{
// reset the initialization completed flag to ensure all properties are serialized
backedModel.BackingStore.InitializationCompleted = false;
resetInitializationCompleted = true;
}
using var writer = GetSerializationWriter(contentType, value);
writer.WriteCollectionOfObjectValues(string.Empty, value);
return writer.GetSerializedContent();
writer.WriteCollectionOfObjectValues(null, value);
var stream = writer.GetSerializedContent();
if(resetInitializationCompleted)
{
(value as IBackedModel)!.BackingStore.InitializationCompleted = true;
}
return stream;
}
/// <summary>
/// Serializes the given object into a string based on the content type.
Expand Down
Loading