Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions sdk/core/Azure.Core/src/Shared/RequestContentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,15 @@ public static RequestContent FromObject(object value)
content.JsonWriter.WriteObjectValue(value);
return content;
}
public static RequestContent FromObject(BinaryData value)
Comment thread
chunyu3 marked this conversation as resolved.
{
var content = new Utf8JsonRequestContent();
#if NET6_0_OR_GREATER
content.JsonWriter.WriteRawValue(value);
#else
JsonSerializer.Serialize(content.JsonWriter, JsonDocument.Parse(value).RootElement);
#endif
return content;
}
}
}
21 changes: 21 additions & 0 deletions sdk/core/Azure.Core/tests/RequestContentHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public static IEnumerable<TestCaseData> GetOneDateTimeData()
yield return new TestCaseData(DateTimeOffset.Parse("2022-08-26T18:38:00Z"));
}

public static object[] BinaryDataCases =
{
new object[] { BinaryData.FromString("\"test\"") },
new object[] { new BinaryData(1)},
new object[] { new BinaryData(1.1)},
new object[] { new BinaryData(true)},
new object[] { BinaryData.FromObjectAsJson(new {name="a", age=1})},
new object[] { new BinaryData(DateTimeOffset.Parse("2022-08-26T18:38:00Z"))}
};

[TestCase(1, 2)]
[TestCase("a", "b")]
[TestCase(true, false)]
Expand Down Expand Up @@ -186,5 +196,16 @@ public void TestFromObject<T>(T value)
break;
}
}

[TestCaseSource(nameof(BinaryDataCases))]
public void TestFromObjectForBinaryData(BinaryData value)
{
var content = RequestContentHelper.FromObject(value);
var stream = new MemoryStream();
content.WriteTo(stream, default);
stream.Position = 0;
var document = JsonDocument.Parse(stream);
Assert.AreEqual(value.ToObjectFromJson(), BinaryData.FromString(document.RootElement.GetRawText()).ToObjectFromJson());
}
}
}