-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Extension methods for make serializing easier #338
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |