-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from AzureCosmosDB/feature/type-metadata-pass…
…through Fixes #97 for outputting JSON metadata properties on child objects
- Loading branch information
Showing
4 changed files
with
126 additions
and
10 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
62 changes: 62 additions & 0 deletions
62
Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension/RawJsonCosmosSerializer.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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.Azure.Cosmos; | ||
using Newtonsoft.Json; | ||
using System.Text; | ||
|
||
namespace Cosmos.DataTransfer.CosmosExtension; | ||
|
||
/// <summary> | ||
/// Serializer for Cosmos allowing access to internal JsonSerializer settings. | ||
/// </summary> | ||
/// <remarks> | ||
/// Defaults to disabling metadata handling to allow passthrough of recognized properties like "$type". | ||
/// </remarks> | ||
public class RawJsonCosmosSerializer : CosmosSerializer | ||
{ | ||
private static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true); | ||
|
||
public static JsonSerializerSettings GetDefaultSettings() => | ||
new() | ||
{ | ||
DateParseHandling = DateParseHandling.None, | ||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | ||
ContractResolver = null, | ||
MaxDepth = 64, | ||
}; | ||
|
||
public JsonSerializerSettings SerializerSettings { get; } = GetDefaultSettings(); | ||
|
||
public override T FromStream<T>(Stream stream) | ||
{ | ||
using (stream) | ||
{ | ||
if (typeof(Stream).IsAssignableFrom(typeof(T))) | ||
{ | ||
return (T)(object)stream; | ||
} | ||
|
||
using var streamReader = new StreamReader(stream); | ||
using var jsonReader = new JsonTextReader(streamReader); | ||
var serializer = JsonSerializer.Create(SerializerSettings); | ||
return serializer.Deserialize<T>(jsonReader); | ||
} | ||
} | ||
|
||
public override Stream ToStream<T>(T input) | ||
{ | ||
var memoryStream = new MemoryStream(); | ||
using (var streamWriter = new StreamWriter(memoryStream, DefaultEncoding, bufferSize: 1024, leaveOpen: true)) | ||
{ | ||
using (var jsonWriter = new JsonTextWriter(streamWriter)) | ||
{ | ||
jsonWriter.Formatting = Formatting.None; | ||
var serializer = JsonSerializer.Create(SerializerSettings); | ||
serializer.Serialize(jsonWriter, input); | ||
jsonWriter.Flush(); | ||
streamWriter.Flush(); | ||
} | ||
} | ||
|
||
memoryStream.Position = 0; | ||
return memoryStream; | ||
} | ||
} |
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