Skip to content

Commit

Permalink
Merge pull request #396 from svrooij/feature/serialize-extensions
Browse files Browse the repository at this point in the history
Parsable extensions
  • Loading branch information
andrueastman authored Sep 25, 2024
2 parents 427e3ff + b91a70a commit 3fd3340
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
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

Check warning on line 25 in src/abstractions/serialization/ParsableExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'SerializeAsStream' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
=> 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

Check warning on line 37 in src/abstractions/serialization/ParsableExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'SerializeAsStringAsync' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
=> 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 3fd3340

Please sign in to comment.