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

Enabling serialization customization through CosmosSerializerOptions #650

Merged
merged 29 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e25d205
Made JsonSerializerSettings internal. Exposed a new constructor with …
Aug 7, 2019
2adf438
Fixed contract UT and updated changelog
Aug 7, 2019
82f1b4a
Made CosmosJsonDotNetSerializer internal. Added CosmosSerializerOptio…
Aug 7, 2019
8077e8e
Fixed naming
Aug 7, 2019
87b9993
Updating file name and changelog
Aug 7, 2019
885b5dc
Merge remote-tracking branch 'origin/master' into users/jawilley/seri…
Aug 7, 2019
e121a93
Update contract test
Aug 7, 2019
530b883
Fixed spacing
Aug 7, 2019
52dd1c7
Fixing contract enforcement test
Aug 8, 2019
18e50c2
Updated naming
Aug 8, 2019
b3ef7a4
Additional contract checks
Aug 8, 2019
7413ec0
Merge to latest
Aug 8, 2019
b8697d0
Adding additional info to ValidateAsync test to understand transient …
Aug 8, 2019
d9cc7fd
Updating contract from VS2017
Aug 8, 2019
c220603
Fixed contract test for VS2019 to skip is IsReadOnlyAttribute
Aug 8, 2019
e17ba0b
Update Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializerOptions.cs
j82w Aug 8, 2019
a90ff94
Update Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializerOptions.cs
j82w Aug 8, 2019
fd468ef
Update Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializerOptions.cs
j82w Aug 8, 2019
e176b82
Renamed CosmosSerializerOptions to CosmosSerializationOptions
Aug 8, 2019
3cdb52b
Merge to latest
Aug 8, 2019
5de82a6
Added more comments
Aug 8, 2019
febd523
Reverting DocumentClientWithUriParameters changes
Aug 9, 2019
c43f7a8
Revert "Reverting DocumentClientWithUriParameters changes"
Aug 9, 2019
3151f60
Reverting accidental changes
Aug 9, 2019
c7ac8db
Adding comment examples, Converted options to be nullable
Aug 9, 2019
9a340fd
Updated contract test
Aug 9, 2019
fa1c98c
Fixed Unit test
Aug 9, 2019
02185ef
Converted to a class with default values and updated changelog
Aug 9, 2019
5fa508e
Updated contract api
Aug 9, 2019
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: 1 addition & 4 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ public Collection<RequestHandler> CustomHandlers
/// <example>
/// // An example on how to configure the serializer to ignore null values
/// CosmosSerializer ignoreNullSerializer = new CosmosJsonDotNetSerializer(
/// new JsonSerializerSettings()
/// {
/// NullValueHandling = NullValueHandling.Ignore
/// });
/// NullValueHandling = NullValueHandling.Ignore);
///
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace Microsoft.Azure.Cosmos
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

/// <summary>
/// The default Cosmos JSON.NET serializer
/// The default Cosmos JSON.NET serializer.
/// </summary>
public sealed class CosmosJsonDotNetSerializer : CosmosSerializer
{
Expand All @@ -19,17 +20,35 @@ public sealed class CosmosJsonDotNetSerializer : CosmosSerializer
/// <summary>
/// Create a serializer that uses the JSON.net serializer
/// </summary>
/// <param name="jsonSerializerSettings">Optional serializer settings</param>
public CosmosJsonDotNetSerializer(JsonSerializerSettings jsonSerializerSettings = null)
/// <param name="ignoreNullValues">Ignore null values</param>
/// <param name="indented">Indent the JSON</param>
/// <param name="propertyNamingPolicy">Support different naming policy like CamelCase</param>
public CosmosJsonDotNetSerializer(
j82w marked this conversation as resolved.
Show resolved Hide resolved
bool ignoreNullValues = false,
j82w marked this conversation as resolved.
Show resolved Hide resolved
bool indented = false,
CosmosNamingPolicy propertyNamingPolicy = CosmosNamingPolicy.Default)
{
if (jsonSerializerSettings == null)
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
jsonSerializerSettings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Include
};
}
NullValueHandling = ignoreNullValues ? NullValueHandling.Ignore : NullValueHandling.Include,
Formatting = indented ? Formatting.Indented : Formatting.None,
ContractResolver = propertyNamingPolicy == CosmosNamingPolicy.CamelCase
? new CamelCasePropertyNamesContractResolver() : null,
j82w marked this conversation as resolved.
Show resolved Hide resolved
};

this.Serializer = JsonSerializer.Create(jsonSerializerSettings);
}

/// <summary>
/// Create a serializer that uses the JSON.net serializer
/// </summary>
/// <remarks>
/// This is internal to reduce exposure of JSON.net types so
/// it is easier to convert to System.Text.Json
/// </remarks>
internal CosmosJsonDotNetSerializer(
j82w marked this conversation as resolved.
Show resolved Hide resolved
JsonSerializerSettings jsonSerializerSettings)
{
this.Serializer = JsonSerializer.Create(jsonSerializerSettings);
}

Expand All @@ -45,7 +64,7 @@ public override T FromStream<T>(Stream stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)(stream);
return (T)(object)stream;
}

using (StreamReader sr = new StreamReader(stream))
Expand Down
25 changes: 25 additions & 0 deletions Microsoft.Azure.Cosmos/src/Serializer/CosmosNamingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System.IO;

/// <summary>
/// This class provides a way to configure basic
/// serializer settings.
/// </summary>
public enum CosmosNamingPolicy
j82w marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// The default naming policy
/// </summary>
Default = 0,
j82w marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Naming policy uses Camel Casing
/// </summary>
CamelCase = 1,
}
}
29 changes: 29 additions & 0 deletions Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
/// <summary>
/// This class provides a way to configure basic
/// serializer settings.
/// </summary>
public struct CosmosSerializerSettings
simplynaveen20 marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Get's if the serializer should ignore null properties
/// </summary>
public bool IgnoreNullValues { get; set; }

/// <summary>
/// Get's if the serializer should ignore null properties
/// </summary>
public bool Indented { get; set; }

/// <summary>
/// The naming policy of the serializer. This is used to configure
/// camel casing
/// </summary>
public CosmosNamingPolicy NamingPolicy { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1527,10 +1527,31 @@
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream)"
},
"Void .ctor(Newtonsoft.Json.JsonSerializerSettings)": {
"Void .ctor(Boolean, Boolean, Microsoft.Azure.Cosmos.CosmosNamingPolicy)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "Void .ctor(Newtonsoft.Json.JsonSerializerSettings)"
"MethodInfo": "Void .ctor(Boolean, Boolean, Microsoft.Azure.Cosmos.CosmosNamingPolicy)"
}
},
"NestedTypes": {}
},
"CosmosNamingPolicy": {
"Subclasses": {},
"Members": {
"Int32 value__": {
"Type": "Field",
"Attributes": [],
"MethodInfo": null
},
"Microsoft.Azure.Cosmos.CosmosNamingPolicy CamelCase": {
"Type": "Field",
"Attributes": [],
"MethodInfo": null
},
"Microsoft.Azure.Cosmos.CosmosNamingPolicy Default": {
"Type": "Field",
"Attributes": [],
"MethodInfo": null
}
},
"NestedTypes": {}
Expand All @@ -1550,10 +1571,10 @@
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream)"
},
"Void .ctor(Newtonsoft.Json.JsonSerializerSettings)": {
"Void .ctor(Boolean, Boolean, Microsoft.Azure.Cosmos.CosmosNamingPolicy)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "Void .ctor(Newtonsoft.Json.JsonSerializerSettings)"
"MethodInfo": "Void .ctor(Boolean, Boolean, Microsoft.Azure.Cosmos.CosmosNamingPolicy)"
}
},
"NestedTypes": {}
Expand All @@ -1573,6 +1594,72 @@
},
"NestedTypes": {}
},
"CosmosSerializerSettings": {
"Subclasses": {},
"Members": {
"Boolean get_IgnoreNullValues()[System.Runtime.CompilerServices.IsReadOnlyAttribute()]-[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute",
"IsReadOnlyAttribute"
],
"MethodInfo": "Boolean get_IgnoreNullValues()"
},
"Boolean get_Indented()[System.Runtime.CompilerServices.IsReadOnlyAttribute()]-[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute",
"IsReadOnlyAttribute"
],
"MethodInfo": "Boolean get_Indented()"
},
"Boolean IgnoreNullValues": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"Boolean Indented": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"Microsoft.Azure.Cosmos.CosmosNamingPolicy get_NamingPolicy()[System.Runtime.CompilerServices.IsReadOnlyAttribute()]-[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute",
"IsReadOnlyAttribute"
],
"MethodInfo": "Microsoft.Azure.Cosmos.CosmosNamingPolicy get_NamingPolicy()"
},
"Microsoft.Azure.Cosmos.CosmosNamingPolicy NamingPolicy": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"Void set_IgnoreNullValues(Boolean)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_IgnoreNullValues(Boolean)"
},
"Void set_Indented(Boolean)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_Indented(Boolean)"
},
"Void set_NamingPolicy(Microsoft.Azure.Cosmos.CosmosNamingPolicy)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_NamingPolicy(Microsoft.Azure.Cosmos.CosmosNamingPolicy)"
}
},
"NestedTypes": {}
},
"Database": {
"Subclasses": {},
"Members": {
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed
- [#650](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/650) CosmosJsonDotNetSerializer changed constructor to use flags instead of JsonSerializerSettings
### Fixed

- [#612](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/612) Bug fix for ReadFeed with partition-key
Expand Down