-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Samples: Adds STJ LINQ Serializer Example (#4420)
* initial commit * bump sample version * update comments * sln file fix * update comment * Update Microsoft.Azure.Cosmos/src/Serializer/CosmosLinqSerializer.cs Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com> * remarks fix * xml fix --------- Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com>
- Loading branch information
1 parent
a76666d
commit 6131998
Showing
5 changed files
with
92 additions
and
114 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
109 changes: 23 additions & 86 deletions
109
Microsoft.Azure.Cosmos/src/Serializer/CosmosLinqSerializer.cs
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 |
---|---|---|
@@ -1,88 +1,25 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System.Reflection; | ||
|
||
/// <summary> | ||
/// This abstract class can be implemented to allow a custom serializer (Non [Json.NET serializer](https://www.newtonsoft.com/json/help/html/Introduction.htm)'s) | ||
/// to be used by the CosmosClient for LINQ queries. | ||
/// </summary> | ||
/// <example> | ||
/// This example implements the CosmosLinqSerializer contract. | ||
/// This example custom serializer will honor System.Text.Json attributes. | ||
/// <code language="c#"> | ||
/// <![CDATA[ | ||
/// class SystemTextJsonSerializer : CosmosLinqSerializer | ||
/// { | ||
/// private readonly JsonObjectSerializer systemTextJsonSerializer; | ||
/// | ||
/// public SystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions) | ||
/// { | ||
/// this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions); | ||
/// } | ||
/// | ||
/// public override T FromStream<T>(Stream stream) | ||
/// { | ||
/// if (stream == null) | ||
/// throw new ArgumentNullException(nameof(stream)); | ||
/// | ||
/// using (stream) | ||
/// { | ||
/// if (stream.CanSeek && stream.Length == 0) | ||
/// { | ||
/// return default; | ||
/// } | ||
/// | ||
/// if (typeof(Stream).IsAssignableFrom(typeof(T))) | ||
/// { | ||
/// return (T)(object)stream; | ||
/// } | ||
/// | ||
/// return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default); | ||
/// } | ||
/// } | ||
/// | ||
/// public override Stream ToStream<T>(T input) | ||
/// { | ||
/// MemoryStream streamPayload = new MemoryStream(); | ||
/// this.systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default); | ||
/// streamPayload.Position = 0; | ||
/// return streamPayload; | ||
/// } | ||
/// | ||
/// public override string SerializeMemberName(MemberInfo memberInfo) | ||
/// { | ||
/// System.Text.Json.Serialization.JsonExtensionDataAttribute jsonExtensionDataAttribute = | ||
/// memberInfo.GetCustomAttribute<System.Text.Json.Serialization.JsonExtensionDataAttribute>(true); | ||
/// if (jsonExtensionDataAttribute != null) | ||
/// { | ||
/// return null; | ||
/// } | ||
/// | ||
/// JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true); | ||
/// | ||
/// string memberName = !string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name) | ||
/// ? jsonPropertyNameAttribute.Name | ||
/// : memberInfo.Name; | ||
/// | ||
/// // Users must add handling for any additional attributes here | ||
/// | ||
/// return memberName; | ||
/// } | ||
/// } | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
public abstract class CosmosLinqSerializer : CosmosSerializer | ||
{ | ||
/// <summary> | ||
/// Convert a MemberInfo to a string for use in LINQ query translation. | ||
/// This must be implemented when using a custom serializer for LINQ queries. | ||
/// </summary> | ||
/// <param name="memberInfo">Any MemberInfo used in the query.</param> | ||
/// <returns>A serialized representation of the member.</returns> | ||
public abstract string SerializeMemberName(MemberInfo memberInfo); | ||
} | ||
} | ||
/// <summary> | ||
/// This abstract class can be implemented to allow a custom serializer (Non [Json.NET serializer](https://www.newtonsoft.com/json/help/html/Introduction.htm)'s) | ||
/// to be used by the CosmosClient for LINQ queries. | ||
/// </summary> | ||
/// <remarks> | ||
/// Refer to the <see href="https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs">sample project</see> for a full implementation. | ||
/// </remarks> | ||
public abstract class CosmosLinqSerializer : CosmosSerializer | ||
{ | ||
/// <summary> | ||
/// Convert a MemberInfo to a string for use in LINQ query translation. | ||
/// This must be implemented when using a custom serializer for LINQ queries. | ||
/// </summary> | ||
/// <param name="memberInfo">Any MemberInfo used in the query.</param> | ||
/// <returns>A serialized representation of the member.</returns> | ||
public abstract string SerializeMemberName(MemberInfo memberInfo); | ||
} | ||
} |
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