private static void VerifyWriter <T>(JsonSerializationFormat jsonSerializationFormat, T expectedDeserializedValue) { using (CosmosDBToNewtonsoftWriter writer = new CosmosDBToNewtonsoftWriter(jsonSerializationFormat)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(writer, expectedDeserializedValue); ReadOnlyMemory <byte> result = writer.GetResult(); string actualSerializedValue; if (jsonSerializationFormat == JsonSerializationFormat.Binary) { actualSerializedValue = JsonTestUtils.ConvertBinaryToText(result); } else { actualSerializedValue = Utf8Span.UnsafeFromUtf8BytesNoValidation(result.Span).ToString(); } actualSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(actualSerializedValue); string expectedSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat( JsonConvert.SerializeObject( expectedDeserializedValue)); Assert.AreEqual(expectedSerializedValue, actualSerializedValue); } }
private static void VerifyWriter <T>(JsonSerializationFormat jsonSerializationFormat, T expectedDeserializedValue) { using (CosmosDBToNewtonsoftWriter writer = new CosmosDBToNewtonsoftWriter(jsonSerializationFormat)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, expectedDeserializedValue); byte[] result = writer.GetResult().ToArray(); string actualSerializedValue; if (jsonSerializationFormat == JsonSerializationFormat.Binary) { actualSerializedValue = JsonTestUtils.ConvertBinaryToText(result); } else { actualSerializedValue = Encoding.UTF8.GetString(result); } actualSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(actualSerializedValue); string expectedSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue)); Assert.AreEqual(expectedSerializedValue, actualSerializedValue); } }
private static void PerformRoundTrip( SerializationFormat sourceFormat, SerializationFormat destinationFormat, string json, bool writeAsRootNode) { IJsonReader reader = sourceFormat switch { SerializationFormat.Text => JsonReader.Create(Encoding.UTF8.GetBytes(json)), SerializationFormat.Binary => JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)), SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBReader.CreateFromString(json), _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"), }; IJsonNavigator navigator = sourceFormat switch { SerializationFormat.Text => JsonNavigator.Create(Encoding.UTF8.GetBytes(json)), SerializationFormat.Binary => JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)), SerializationFormat.NewtonsoftText => new JsonNewtonsoftNavigator(json), _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"), }; foreach (object source in new object[] { reader, navigator }) { IJsonWriter writer = destinationFormat switch { SerializationFormat.Text => JsonWriter.Create(JsonSerializationFormat.Text), SerializationFormat.Binary => JsonWriter.Create(JsonSerializationFormat.Binary), SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBWriter.CreateTextWriter(), _ => throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"), }; switch (source) { case IJsonReader sourceReader: sourceReader.WriteAll(writer); break; case IJsonNavigator sourceNavigator: if (writeAsRootNode) { sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer); } else { IJsonNavigatorNode rootNode = sourceNavigator.GetRootNode(); JsonNodeType jsonNodeType = sourceNavigator.GetNodeType(rootNode); switch (jsonNodeType) { case JsonNodeType.Array: writer.WriteArrayStart(); foreach (IJsonNavigatorNode arrayItem in sourceNavigator.GetArrayItems(rootNode)) { sourceNavigator.WriteNode(arrayItem, writer); } writer.WriteArrayEnd(); break; case JsonNodeType.Object: writer.WriteObjectStart(); foreach (ObjectProperty objectProperty in sourceNavigator.GetObjectProperties(rootNode)) { sourceNavigator.WriteNode(objectProperty.NameNode, writer); sourceNavigator.WriteNode(objectProperty.ValueNode, writer); } writer.WriteObjectEnd(); break; default: sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer); break; } } break; default: Assert.Fail("Failed to downcast source type."); break; } string result = writer.SerializationFormat switch { JsonSerializationFormat.Text => Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString(), JsonSerializationFormat.Binary => JsonTestUtils.ConvertBinaryToText(writer.GetResult()), _ => throw new ArgumentException(), }; string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result); string normalizedJson = JsonRoundTripsTests.NewtonsoftFormat(json); Assert.AreEqual(normalizedJson, normalizedResult); } }
private void MultiSerializationRoundTrip(string json) { // Normalize the json to get rid of any formatting issues json = this.NewtonsoftFormat(json); foreach (SerializationFormat sourceFormat in Enum.GetValues(typeof(SerializationFormat))) { foreach (SerializationFormat destinationFormat in Enum.GetValues(typeof(SerializationFormat))) { IJsonReader reader; switch (sourceFormat) { case SerializationFormat.Text: reader = JsonReader.Create(Encoding.UTF8.GetBytes(json)); break; case SerializationFormat.Binary: reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)); break; case SerializationFormat.NewtonsoftText: reader = NewtonsoftToCosmosDBReader.CreateFromString(json); break; case SerializationFormat.BinaryWithDictionaryEncoding: JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128); reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } IJsonNavigator navigator; switch (sourceFormat) { case SerializationFormat.Text: navigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(json)); break; case SerializationFormat.Binary: navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)); break; case SerializationFormat.NewtonsoftText: navigator = new JsonNewtonsoftNavigator(json); break; case SerializationFormat.BinaryWithDictionaryEncoding: JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128); navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } object[] sources = new object[] { reader, navigator }; foreach (object source in sources) { IJsonWriter writer; JsonStringDictionary jsonStringDictionary; switch (destinationFormat) { case SerializationFormat.Text: writer = JsonWriter.Create(JsonSerializationFormat.Text); jsonStringDictionary = null; break; case SerializationFormat.Binary: writer = JsonWriter.Create(JsonSerializationFormat.Binary); jsonStringDictionary = null; break; case SerializationFormat.NewtonsoftText: writer = NewtonsoftToCosmosDBWriter.CreateTextWriter(); jsonStringDictionary = null; break; case SerializationFormat.BinaryWithDictionaryEncoding: jsonStringDictionary = new JsonStringDictionary(capacity: 128); writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"); } Stopwatch stopwatch = Stopwatch.StartNew(); switch (source) { case IJsonReader sourceReader: writer.WriteAll(sourceReader); break; case IJsonNavigator sourceNavigator: writer.WriteJsonNode(sourceNavigator, sourceNavigator.GetRootNode()); break; default: Assert.Fail("Failed to downcast source type."); break; } stopwatch.Stop(); string result; switch (writer.SerializationFormat) { case JsonSerializationFormat.Text: result = Encoding.UTF8.GetString(writer.GetResult().ToArray()); break; case JsonSerializationFormat.Binary: result = JsonTestUtils.ConvertBinaryToText(writer.GetResult().ToArray(), jsonStringDictionary); break; default: throw new ArgumentException(); } result = this.NewtonsoftFormat(result); Assert.AreEqual(json, result); string sourceType = (source is IJsonReader) ? "Reader" : "Navigator"; Console.WriteLine($"{sourceFormat} {sourceType} to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms"); } } } }
private void NewtonsoftWrapperRoundTrip(string json) { // Normalize the json to get rid of any formatting issues json = this.NewtonsoftFormat(json); foreach (NewtonsoftWrapperFormat sourceFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat))) { foreach (NewtonsoftWrapperFormat destinationFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat))) { IJsonReader reader; switch (sourceFormat) { case NewtonsoftWrapperFormat.NewtonsoftText: reader = NewtonsoftToCosmosDBReader.CreateFromString(json); break; case NewtonsoftWrapperFormat.CosmosDBText: reader = JsonReader.Create(Encoding.UTF8.GetBytes(json)); break; case NewtonsoftWrapperFormat.CosmosDBBinary: reader = JsonReader.Create(JsonPerfMeasurement.ConvertTextToBinary(json)); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } IJsonWriter writer; switch (destinationFormat) { case NewtonsoftWrapperFormat.NewtonsoftText: writer = NewtonsoftToCosmosDBWriter.CreateTextWriter(); break; case NewtonsoftWrapperFormat.CosmosDBText: writer = JsonWriter.Create(JsonSerializationFormat.Text); break; case NewtonsoftWrapperFormat.CosmosDBBinary: writer = JsonWriter.Create(JsonSerializationFormat.Binary); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } Stopwatch stopwatch = Stopwatch.StartNew(); writer.WriteAll(reader); stopwatch.Stop(); string result; switch (writer.SerializationFormat) { case JsonSerializationFormat.Text: result = Encoding.UTF8.GetString(writer.GetResult().ToArray()); break; case JsonSerializationFormat.Binary: result = JsonTestUtils.ConvertBinaryToText(writer.GetResult().ToArray()); break; default: throw new ArgumentException(); } result = this.NewtonsoftFormat(result); Assert.AreEqual(json, result); Console.WriteLine($"{sourceFormat} Reader to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms"); } } }
private static void PerformRoundTrip( SerializationFormat sourceFormat, SerializationFormat destinationFormat, string json) { JsonStringDictionary sourceDictionary = null; IJsonReader reader; switch (sourceFormat) { case SerializationFormat.Text: reader = JsonReader.Create(Encoding.UTF8.GetBytes(json)); break; case SerializationFormat.Binary: reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)); break; case SerializationFormat.NewtonsoftText: reader = NewtonsoftToCosmosDBReader.CreateFromString(json); break; case SerializationFormat.BinaryWithDictionaryEncoding: sourceDictionary = new JsonStringDictionary(capacity: 128); reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } IJsonNavigator navigator; switch (sourceFormat) { case SerializationFormat.Text: navigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(json)); break; case SerializationFormat.Binary: navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)); break; case SerializationFormat.NewtonsoftText: navigator = new JsonNewtonsoftNavigator(json); break; case SerializationFormat.BinaryWithDictionaryEncoding: sourceDictionary = new JsonStringDictionary(capacity: 128); navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"); } object[] sources = new object[] { reader, navigator }; foreach (object source in sources) { IJsonWriter writer; JsonStringDictionary jsonStringDictionary; switch (destinationFormat) { case SerializationFormat.Text: writer = JsonWriter.Create(JsonSerializationFormat.Text); jsonStringDictionary = null; break; case SerializationFormat.Binary: writer = JsonWriter.Create(JsonSerializationFormat.Binary); jsonStringDictionary = null; break; case SerializationFormat.NewtonsoftText: writer = NewtonsoftToCosmosDBWriter.CreateTextWriter(); jsonStringDictionary = null; break; case SerializationFormat.BinaryWithDictionaryEncoding: jsonStringDictionary = new JsonStringDictionary(capacity: 128); if (sourceFormat == SerializationFormat.BinaryWithDictionaryEncoding) { int index = 0; while (sourceDictionary.TryGetStringAtIndex(index++, out UtfAllString value)) { Assert.IsTrue(jsonStringDictionary.TryAddString(value.Utf16String, out _)); } } writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary); break; default: throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"); } switch (source) { case IJsonReader sourceReader: sourceReader.WriteAll(writer); break; case IJsonNavigator sourceNavigator: sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer); break; default: Assert.Fail("Failed to downcast source type."); break; } string result; switch (writer.SerializationFormat) { case JsonSerializationFormat.Text: result = Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString(); break; case JsonSerializationFormat.Binary: result = JsonTestUtils.ConvertBinaryToText(writer.GetResult(), jsonStringDictionary); break; default: throw new ArgumentException(); } string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result); string normalizedJson = JsonRoundTripsTests.NewtonsoftFormat(json); Assert.AreEqual(normalizedJson, normalizedResult); } }