forked from Azure/azure-cosmos-dotnet-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchOperationsJsonConverter.cs
More file actions
145 lines (123 loc) · 5.49 KB
/
PatchOperationsJsonConverter.cs
File metadata and controls
145 lines (123 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
/// <summary>
/// A custom serializer converter for list of Patch operations.>
/// </summary>
internal sealed class PatchOperationsJsonConverter : JsonConverter
{
private readonly CosmosSerializer userSerializer;
public PatchOperationsJsonConverter(CosmosSerializer userSerializer)
{
this.userSerializer = userSerializer ?? throw new ArgumentNullException(nameof(userSerializer));
}
public override bool CanConvert(Type objectType)
{
return true;
}
public override bool CanRead => false;
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override void WriteJson(
JsonWriter writer,
object value,
JsonSerializer serializer)
{
if (!(value is PatchSpec patchSpec))
{
throw new ArgumentOutOfRangeException("nameof(value) should be of type PatchSpec.");
}
IReadOnlyList<PatchOperation> patchOperations = patchSpec.PatchOperations;
writer.WriteStartObject();
patchSpec.RequestOptions.Match(
(PatchItemRequestOptions patchRequestOptions) =>
{
if (patchRequestOptions != null)
{
if (!String.IsNullOrWhiteSpace(patchRequestOptions.FilterPredicate))
{
writer.WritePropertyName(PatchConstants.PatchSpecAttributes.Condition);
writer.WriteValue(patchRequestOptions.FilterPredicate);
}
}
},
(TransactionalBatchPatchItemRequestOptions transactionalBatchPatchRequestOptions) =>
{
if (transactionalBatchPatchRequestOptions != null)
{
if (!String.IsNullOrWhiteSpace(transactionalBatchPatchRequestOptions.FilterPredicate))
{
writer.WritePropertyName(PatchConstants.PatchSpecAttributes.Condition);
writer.WriteValue(transactionalBatchPatchRequestOptions.FilterPredicate);
}
}
});
writer.WritePropertyName(PatchConstants.PatchSpecAttributes.Operations);
writer.WriteStartArray();
foreach (PatchOperation operation in patchOperations)
{
writer.WriteStartObject();
writer.WritePropertyName(PatchConstants.PropertyNames.OperationType);
writer.WriteValue(operation.OperationType.ToEnumMemberString());
writer.WritePropertyName(PatchConstants.PropertyNames.Path);
writer.WriteValue(operation.Path);
if (operation.OperationType == PatchOperationType.Move)
{
writer.WritePropertyName(PatchConstants.PropertyNames.From);
writer.WriteValue(operation.From);
}
else if (operation.TrySerializeValueParameter(this.userSerializer, out Stream valueStream))
{
string valueParam;
using (valueStream)
{
using (StreamReader streamReader = new StreamReader(valueStream))
{
valueParam = streamReader.ReadToEnd();
}
}
writer.WritePropertyName(PatchConstants.PropertyNames.Value);
writer.WriteRawValue(valueParam);
}
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
/// <summary>
/// Only create a custom PatchOperations serializer if there is a customer serializer else
/// use the default properties serializer
/// </summary>
internal static CosmosSerializer CreatePatchOperationsSerializer(
CosmosSerializer cosmosSerializer,
CosmosSerializer propertiesSerializer)
{
// If both serializers are the same no need for the custom converter
if (object.ReferenceEquals(cosmosSerializer, propertiesSerializer))
{
return propertiesSerializer;
}
JsonSerializerSettings settings = new JsonSerializerSettings()
{
Converters = new List<JsonConverter>()
{
new PatchOperationsJsonConverter(cosmosSerializer)
},
MaxDepth = 64, // https://github.com/advisories/GHSA-5crp-9r3c-p9vr
};
return new CosmosJsonSerializerWrapper(new CosmosJsonDotNetSerializer(settings));
}
}
}