Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsable extensions #396

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- Common default project properties for ALL projects-->
<PropertyGroup>
<VersionPrefix>1.12.4</VersionPrefix>
<VersionPrefix>1.13.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- This is overidden in test projects by setting to true-->
<IsTestProject>false</IsTestProject>
Expand Down
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);
}
Loading