From 64a4b43b16c4249dee163ab0c49af90197d82db0 Mon Sep 17 00:00:00 2001 From: Stephan van Rooij <1292510+svrooij@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:14:32 +0200 Subject: [PATCH] Parsable extensions Fixed Extension methods for make serializing easier #338 --- src/abstractions/serialization/IParsable.cs | 1 + .../serialization/ParsableExtensions.cs | 81 +++++++++++++++++++ .../json/ParsableJsonExtensions.cs | 72 +++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/abstractions/serialization/ParsableExtensions.cs create mode 100644 src/serialization/json/ParsableJsonExtensions.cs diff --git a/src/abstractions/serialization/IParsable.cs b/src/abstractions/serialization/IParsable.cs index 039a2ec8..01555fef 100644 --- a/src/abstractions/serialization/IParsable.cs +++ b/src/abstractions/serialization/IParsable.cs @@ -10,6 +10,7 @@ namespace Microsoft.Kiota.Abstractions.Serialization /// /// Defines a serializable model object. /// + /// In the Microsoft.Kiota.Serialization namespace, you can find extension methods for serializing this object. public interface IParsable { /// diff --git a/src/abstractions/serialization/ParsableExtensions.cs b/src/abstractions/serialization/ParsableExtensions.cs new file mode 100644 index 00000000..4aad0a8f --- /dev/null +++ b/src/abstractions/serialization/ParsableExtensions.cs @@ -0,0 +1,81 @@ +// ------------------------------------------------------------------------------ +// Copyright (c) Stephan van rooij. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +// ------------------------------------------------------------------------------ + +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Kiota.Abstractions.Serialization; + +namespace Microsoft.Kiota.Serialization; + +/// +/// Extension methods for instances. +/// +public static class ParsableExtensions +{ + /// + /// Serializes the given object into a stream based on the content type. + /// + /// Content type to serialize the object t + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsStream(this T value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable + => KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues); + + + /// + /// Serializes the given object into a string based on the content type. + /// + /// Content type to serialize the object to + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsStringAsync(this T value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken); + + /// + /// Serializes the given collection into a stream based on the content type. + /// + /// Content type to serialize the object t + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsStream(this IEnumerable value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable + => KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues); + + /// + /// Serializes the given collection into a string based on the content type. + /// + /// Content type to serialize the object to. + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsStringAsync(this IEnumerable value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken); + + /// + /// Serializes the given collection into a stream based on the content type. + /// + /// Content type to serialize the object t + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsStream(this T[] value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable + => KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues); + + /// + /// Serializes the given collection into a string based on the content type. + /// + /// Content type to serialize the object to. + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsStringAsync(this T[] value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken); +} \ No newline at end of file diff --git a/src/serialization/json/ParsableJsonExtensions.cs b/src/serialization/json/ParsableJsonExtensions.cs new file mode 100644 index 00000000..02a61307 --- /dev/null +++ b/src/serialization/json/ParsableJsonExtensions.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Kiota.Abstractions.Serialization; + +namespace Microsoft.Kiota.Serialization; + +/// +/// Extension methods for instances specifically for JSON. +/// +public static class ParsableJsonExtensions +{ + private const string _contentType = "application/json"; + + /// + /// Serializes the given object into a json stream + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsJsonStream(this T value, bool serializeOnlyChangedValues = false) where T : IParsable + => value.SerializeAsStream(_contentType, serializeOnlyChangedValues); + + /// + /// Serializes the given object into a json string. + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsJsonStringAsync(this T value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken); + + /// + /// Serializes the given collection into a json stream. + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsJsonStream(this IEnumerable value, bool serializeOnlyChangedValues = false) where T : IParsable + => value.SerializeAsStream(_contentType, serializeOnlyChangedValues); + + /// + /// Serializes the given collection into a json string. + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsJsonStringAsync(this IEnumerable value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken); + + /// + /// Serializes the given collection into a json stream. + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// The serialized representation as a stream. + public static Stream SerializeAsJsonStream(this T[] value, bool serializeOnlyChangedValues = false) where T : IParsable + => value.SerializeAsStream(_contentType, serializeOnlyChangedValues); + + /// + /// Serializes the given collection into a json string. + /// + /// The object to serialize. + /// If this object uses the , use this to control if you want all properties or just the changed once. + /// Cancel the request during execution. + /// The serialized representation as a string. + public static async Task SerializeAsJsonStringAsync(this T[] value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable + => await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken); +} \ No newline at end of file