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 1/2] 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 From b91a70a3dc517a20685ce0bf3bb85da114f15970 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Wed, 25 Sep 2024 12:39:09 +0300 Subject: [PATCH 2/2] adds changelog entry. format code --- CHANGELOG.md | 5 + Directory.Build.props | 2 +- .../serialization/ParsableExtensions.cs | 114 +++++++++--------- 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8704dbec..a61895c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.13.0] - 2024-09-25 + +### Changed + +- Adds extension methods for make serializing easier [#338](https://github.com/microsoft/kiota-dotnet/issues/338). ## [1.12.4] - 2024-09-05 diff --git a/Directory.Build.props b/Directory.Build.props index f202c97d..9b7955bd 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.12.4 + 1.13.0 false diff --git a/src/abstractions/serialization/ParsableExtensions.cs b/src/abstractions/serialization/ParsableExtensions.cs index 4aad0a8f..94280dd2 100644 --- a/src/abstractions/serialization/ParsableExtensions.cs +++ b/src/abstractions/serialization/ParsableExtensions.cs @@ -15,67 +15,67 @@ namespace Microsoft.Kiota.Serialization; /// 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 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 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 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 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 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); + /// + /// 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