| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Serialization |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Allows injection of arbitrary JSON during serialization via a callback. |
| | 12 | | /// </summary> |
| | 13 | | internal class InjectingJsonWriter : JsonWriter |
| | 14 | | { |
| | 15 | | private JsonWriter _innerWriter; |
| | 16 | | private bool _wasStartProcessed; |
| | 17 | |
|
| 27508 | 18 | | public InjectingJsonWriter(JsonWriter innerWriter) |
| | 19 | | { |
| 27508 | 20 | | _innerWriter = innerWriter; |
| 27508 | 21 | | _wasStartProcessed = false; |
| 27508 | 22 | | } |
| | 23 | |
|
| 82524 | 24 | | public Action<JsonWriter> OnStart { get; set; } |
| | 25 | |
|
| | 26 | | public override void Close() |
| | 27 | | { |
| 0 | 28 | | _innerWriter.Close(); |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public override void Flush() |
| | 32 | | { |
| 0 | 33 | | _innerWriter.Flush(); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public override void WriteComment(string text) |
| | 37 | | { |
| 0 | 38 | | _innerWriter.WriteComment(text); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | public override void WriteEnd() |
| | 42 | | { |
| 0 | 43 | | _innerWriter.WriteEnd(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void WriteEndArray() |
| | 47 | | { |
| 5866 | 48 | | _innerWriter.WriteEndArray(); |
| 5866 | 49 | | } |
| | 50 | |
|
| | 51 | | public override void WriteEndConstructor() |
| | 52 | | { |
| 0 | 53 | | _innerWriter.WriteEndConstructor(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public override void WriteEndObject() |
| | 57 | | { |
| 31412 | 58 | | _innerWriter.WriteEndObject(); |
| 31412 | 59 | | } |
| | 60 | |
|
| | 61 | | public override void WriteNull() |
| | 62 | | { |
| 58 | 63 | | _innerWriter.WriteNull(); |
| 58 | 64 | | } |
| | 65 | |
|
| | 66 | | public override void WritePropertyName(string name) |
| | 67 | | { |
| 4104 | 68 | | _innerWriter.WritePropertyName(name); |
| 4104 | 69 | | } |
| | 70 | |
|
| | 71 | | public override void WritePropertyName(string name, bool escape) |
| | 72 | | { |
| 63108 | 73 | | _innerWriter.WritePropertyName(name, escape); |
| 63108 | 74 | | } |
| | 75 | |
|
| | 76 | | public override void WriteRaw(string json) |
| | 77 | | { |
| 0 | 78 | | _innerWriter.WriteRaw(json); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public override void WriteRawValue(string json) |
| | 82 | | { |
| 0 | 83 | | _innerWriter.WriteRawValue(json); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public override void WriteStartArray() |
| | 87 | | { |
| 5866 | 88 | | _innerWriter.WriteStartArray(); |
| 5866 | 89 | | AfterStart(); |
| 5866 | 90 | | } |
| | 91 | |
|
| | 92 | | public override void WriteStartConstructor(string name) |
| | 93 | | { |
| 0 | 94 | | _innerWriter.WriteStartConstructor(name); |
| 0 | 95 | | AfterStart(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public override void WriteStartObject() |
| | 99 | | { |
| 31412 | 100 | | _innerWriter.WriteStartObject(); |
| 31412 | 101 | | AfterStart(); |
| 31412 | 102 | | } |
| | 103 | |
|
| | 104 | | public override void WriteUndefined() |
| | 105 | | { |
| 0 | 106 | | _innerWriter.WriteUndefined(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public override void WriteValue(bool value) |
| | 110 | | { |
| 86 | 111 | | _innerWriter.WriteValue(value); |
| 86 | 112 | | } |
| | 113 | |
|
| | 114 | | public override void WriteValue(bool? value) |
| | 115 | | { |
| 5084 | 116 | | _innerWriter.WriteValue(value); |
| 5084 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void WriteValue(byte value) |
| | 120 | | { |
| 0 | 121 | | _innerWriter.WriteValue(value); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public override void WriteValue(byte? value) |
| | 125 | | { |
| 0 | 126 | | _innerWriter.WriteValue(value); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public override void WriteValue(byte[] value) |
| | 130 | | { |
| 0 | 131 | | _innerWriter.WriteValue(value); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public override void WriteValue(char value) |
| | 135 | | { |
| 0 | 136 | | _innerWriter.WriteValue(value); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | public override void WriteValue(char? value) |
| | 140 | | { |
| 0 | 141 | | _innerWriter.WriteValue(value); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public override void WriteValue(DateTime value) |
| | 145 | | { |
| 0 | 146 | | _innerWriter.WriteValue(value); |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | public override void WriteValue(DateTime? value) |
| | 150 | | { |
| 0 | 151 | | _innerWriter.WriteValue(value); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | public override void WriteValue(DateTimeOffset value) |
| | 155 | | { |
| 80 | 156 | | _innerWriter.WriteValue(value); |
| 80 | 157 | | } |
| | 158 | |
|
| | 159 | | public override void WriteValue(DateTimeOffset? value) |
| | 160 | | { |
| 1970 | 161 | | _innerWriter.WriteValue(value); |
| 1970 | 162 | | } |
| | 163 | |
|
| | 164 | | public override void WriteValue(decimal value) |
| | 165 | | { |
| 0 | 166 | | _innerWriter.WriteValue(value); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | public override void WriteValue(decimal? value) |
| | 170 | | { |
| 0 | 171 | | _innerWriter.WriteValue(value); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | public override void WriteValue(double value) |
| | 175 | | { |
| 5200 | 176 | | _innerWriter.WriteValue(value); |
| 5200 | 177 | | } |
| | 178 | |
|
| | 179 | | public override void WriteValue(double? value) |
| | 180 | | { |
| 0 | 181 | | _innerWriter.WriteValue(value); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | public override void WriteValue(float value) |
| | 185 | | { |
| 0 | 186 | | _innerWriter.WriteValue(value); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public override void WriteValue(float? value) |
| | 190 | | { |
| 0 | 191 | | _innerWriter.WriteValue(value); |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | public override void WriteValue(Guid value) |
| | 195 | | { |
| 0 | 196 | | _innerWriter.WriteValue(value); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | public override void WriteValue(Guid? value) |
| | 200 | | { |
| 0 | 201 | | _innerWriter.WriteValue(value); |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | public override void WriteValue(int value) |
| | 205 | | { |
| 90 | 206 | | _innerWriter.WriteValue(value); |
| 90 | 207 | | } |
| | 208 | |
|
| | 209 | | public override void WriteValue(int? value) |
| | 210 | | { |
| 3138 | 211 | | _innerWriter.WriteValue(value); |
| 3138 | 212 | | } |
| | 213 | |
|
| | 214 | | public override void WriteValue(long value) |
| | 215 | | { |
| 34 | 216 | | _innerWriter.WriteValue(value); |
| 34 | 217 | | } |
| | 218 | |
|
| | 219 | | public override void WriteValue(long? value) |
| | 220 | | { |
| 0 | 221 | | _innerWriter.WriteValue(value); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public override void WriteValue(object value) |
| | 225 | | { |
| 0 | 226 | | _innerWriter.WriteValue(value); |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | public override void WriteValue(sbyte value) |
| | 230 | | { |
| 0 | 231 | | _innerWriter.WriteValue(value); |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public override void WriteValue(sbyte? value) |
| | 235 | | { |
| 0 | 236 | | _innerWriter.WriteValue(value); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | public override void WriteValue(short value) |
| | 240 | | { |
| 0 | 241 | | _innerWriter.WriteValue(value); |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | public override void WriteValue(short? value) |
| | 245 | | { |
| 0 | 246 | | _innerWriter.WriteValue(value); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | public override void WriteValue(string value) |
| | 250 | | { |
| 53580 | 251 | | _innerWriter.WriteValue(value); |
| 53580 | 252 | | } |
| | 253 | |
|
| | 254 | | public override void WriteValue(TimeSpan value) |
| | 255 | | { |
| 0 | 256 | | _innerWriter.WriteValue(value); |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | public override void WriteValue(TimeSpan? value) |
| | 260 | | { |
| 0 | 261 | | _innerWriter.WriteValue(value); |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | public override void WriteValue(uint value) |
| | 265 | | { |
| 0 | 266 | | _innerWriter.WriteValue(value); |
| 0 | 267 | | } |
| | 268 | |
|
| | 269 | | public override void WriteValue(uint? value) |
| | 270 | | { |
| 0 | 271 | | _innerWriter.WriteValue(value); |
| 0 | 272 | | } |
| | 273 | |
|
| | 274 | | public override void WriteValue(ulong value) |
| | 275 | | { |
| 0 | 276 | | _innerWriter.WriteValue(value); |
| 0 | 277 | | } |
| | 278 | |
|
| | 279 | | public override void WriteValue(ulong? value) |
| | 280 | | { |
| 0 | 281 | | _innerWriter.WriteValue(value); |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | public override void WriteValue(Uri value) |
| | 285 | | { |
| 0 | 286 | | _innerWriter.WriteValue(value); |
| 0 | 287 | | } |
| | 288 | |
|
| | 289 | | public override void WriteValue(ushort value) |
| | 290 | | { |
| 0 | 291 | | _innerWriter.WriteValue(value); |
| 0 | 292 | | } |
| | 293 | |
|
| | 294 | | public override void WriteWhitespace(string ws) |
| | 295 | | { |
| 0 | 296 | | _innerWriter.WriteWhitespace(ws); |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | private void AfterStart() |
| | 300 | | { |
| 37278 | 301 | | if (!_wasStartProcessed && OnStart != null) |
| | 302 | | { |
| 27508 | 303 | | OnStart(_innerWriter); |
| | 304 | | } |
| | 305 | |
|
| 37278 | 306 | | _wasStartProcessed = true; |
| 37278 | 307 | | } |
| | 308 | | } |
| | 309 | | } |