private void WriteValueRecursive(OpenApiJsonWriter writer, object value) { if (value == null || value.GetType().IsPrimitive || value is decimal || value is string || value is DateTimeOffset || value is DateTime) { writer.WriteValue(value); } else if (value.GetType().IsGenericType&& (typeof(IDictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition()) || typeof(Dictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition()))) { writer.WriteStartObject(); foreach (var elementValue in (dynamic)(value)) { writer.WritePropertyName(elementValue.Key); WriteValueRecursive(writer, elementValue.Value); } writer.WriteEndObject(); } else if (typeof(IEnumerable).IsAssignableFrom(value.GetType())) { writer.WriteStartArray(); foreach (var elementValue in (IEnumerable)value) { WriteValueRecursive(writer, elementValue); } writer.WriteEndArray(); } }
public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues, bool produceTerseOutput) { // Arrange var outputString = new StringWriter(CultureInfo.InvariantCulture); var writer = new OpenApiJsonWriter(outputString, new OpenApiJsonWriterSettings { Terse = produceTerseOutput }); // Act writer.WriteStartArray(); foreach (var stringValue in stringValues) { writer.WriteValue(stringValue); } writer.WriteEndArray(); writer.Flush(); var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString()); var expectedObject = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new List <string>(stringValues))); // Assert parsedObject.Should().BeEquivalentTo(expectedObject); }
public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues) { // Arrange var outputString = new StringWriter(); var writer = new OpenApiJsonWriter(outputString); // Act writer.WriteStartArray(); foreach (var stringValue in stringValues) { writer.WriteValue(stringValue); } writer.WriteEndArray(); writer.Flush(); var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString()); var expectedObject = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new List <string>(stringValues))); // Assert parsedObject.ShouldBeEquivalentTo(expectedObject); }