Skip to content

Commit

Permalink
Parsable extensions
Browse files Browse the repository at this point in the history
Fixed Extension methods for make serializing easier #338
  • Loading branch information
svrooij committed Sep 24, 2024
1 parent 427e3ff commit 64a4b43
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/abstractions/serialization/IParsable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Kiota.Abstractions.Serialization
/// <summary>
/// Defines a serializable model object.
/// </summary>
/// <remarks>In the Microsoft.Kiota.Serialization namespace, you can find extension methods for serializing this object.</remarks>
public interface IParsable
{
/// <summary>
Expand Down
81 changes: 81 additions & 0 deletions src/abstractions/serialization/ParsableExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Extension methods for <see cref="IParsable"/> instances.
/// </summary>
public static class ParsableExtensions
{
/// <summary>
/// Serializes the given object into a stream based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object t </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(this T value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable
=> KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues);


/// <summary>
/// Serializes the given object into a string based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object to </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsStringAsync<T>(this T value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given collection into a stream based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object t </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(this IEnumerable<T> value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable
=> KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given collection into a string based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object to.</param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsStringAsync<T>(this IEnumerable<T> value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given collection into a stream based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object t </param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(this T[] value, string contentType, bool serializeOnlyChangedValues = false) where T : IParsable
=> KiotaSerializer.SerializeAsStream(contentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given collection into a string based on the content type.
/// </summary>
/// <param name="contentType">Content type to serialize the object to.</param>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Microsoft.Kiota.Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsStringAsync<T>(this T[] value, string contentType, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await KiotaSerializer.SerializeAsStringAsync(contentType, value, serializeOnlyChangedValues, cancellationToken);
}
72 changes: 72 additions & 0 deletions src/serialization/json/ParsableJsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Extension methods for <see cref="IParsable"/> instances specifically for JSON.
/// </summary>
public static class ParsableJsonExtensions
{
private const string _contentType = "application/json";

/// <summary>
/// Serializes the given object into a json stream
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this T value, bool serializeOnlyChangedValues = false) where T : IParsable
=> value.SerializeAsStream(_contentType, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a json string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsJsonStringAsync<T>(this T value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given collection into a json stream.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this IEnumerable<T> value, bool serializeOnlyChangedValues = false) where T : IParsable
=> value.SerializeAsStream(_contentType, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given collection into a json string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsJsonStringAsync<T>(this IEnumerable<T> value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given collection into a json stream.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this T[] value, bool serializeOnlyChangedValues = false) where T : IParsable
=> value.SerializeAsStream(_contentType, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given collection into a json string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="serializeOnlyChangedValues">If this object uses the <see cref="Abstractions.Store.IBackingStore"/>, use this to control if you want all properties or just the changed once.</param>
/// <param name="cancellationToken">Cancel the request during execution.</param>
/// <returns>The serialized representation as a string.</returns>
public static async Task<string> SerializeAsJsonStringAsync<T>(this T[] value, bool serializeOnlyChangedValues = false, CancellationToken cancellationToken = default) where T : IParsable
=> await value.SerializeAsStringAsync(_contentType, serializeOnlyChangedValues, cancellationToken);
}

0 comments on commit 64a4b43

Please sign in to comment.