diff --git a/src/abstractions/serialization/IParsable.cs b/src/abstractions/serialization/IParsable.cs index 039a2ec..01555fe 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 0000000..4aad0a8 --- /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 0000000..02a6130 --- /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