Make backend write API string-based
Signed-off-by: Nicholas Van Sickle <nvsickle@amazon.com>
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <AzCore/DOM/Backends/JSON/JsonSerializationUtils.h>
|
||||
#include <AzCore/DOM/DomBackend.h>
|
||||
#include <AzCore/IO/ByteContainerStream.h>
|
||||
|
||||
namespace AZ::Dom
|
||||
{
|
||||
@@ -32,8 +33,9 @@ namespace AZ::Dom
|
||||
return Json::VisitSerializedJsonInPlace<ParseFlags>(buffer, visitor);
|
||||
}
|
||||
|
||||
Visitor::Result WriteToStream(AZ::IO::GenericStream& stream, WriteCallback callback) override
|
||||
Visitor::Result WriteToBuffer(AZStd::string& buffer, WriteCallback callback)
|
||||
{
|
||||
AZ::IO::ByteContainerStream<AZStd::string> stream{ &buffer };
|
||||
AZStd::unique_ptr<Visitor> visitor = Json::CreateJsonStreamWriter(stream, WriteFormat);
|
||||
return callback(*visitor);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace AZ::Dom
|
||||
//! A callback that accepts a Visitor, making DOM calls to inform the serializer, and returns an
|
||||
//! aggregate error code to indicate whether or not the operation succeeded.
|
||||
using WriteCallback = AZStd::function<Visitor::Result(Visitor&)>;
|
||||
//! Attempt to write a value to a stream using a write callback.
|
||||
virtual Visitor::Result WriteToStream(AZ::IO::GenericStream& stream, WriteCallback callback) = 0;
|
||||
//! Attempt to write a value to the specified string using a write callback.
|
||||
virtual Visitor::Result WriteToBuffer(AZStd::string& buffer, WriteCallback callback) = 0;
|
||||
};
|
||||
} // namespace AZ::Dom
|
||||
|
||||
@@ -21,10 +21,4 @@ namespace AZ::Dom::Utils
|
||||
{
|
||||
return backend.ReadFromBufferInPlace(string.data(), string.size(), visitor);
|
||||
}
|
||||
|
||||
Visitor::Result WriteToString(Backend& backend, AZStd::string& buffer, Backend::WriteCallback writeCallback)
|
||||
{
|
||||
AZ::IO::ByteContainerStream<AZStd::string> stream{ &buffer };
|
||||
return backend.WriteToStream(stream, writeCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,4 @@ namespace AZ::Dom::Utils
|
||||
{
|
||||
Visitor::Result ReadFromString(Backend& backend, AZStd::string_view string, AZ::Dom::Lifetime lifetime, Visitor& visitor);
|
||||
Visitor::Result ReadFromStringInPlace(Backend& backend, AZStd::string& string, Visitor& visitor);
|
||||
|
||||
Visitor::Result WriteToString(Backend& backend, AZStd::string& buffer, Backend::WriteCallback writeCallback);
|
||||
} // namespace AZ::Dom::Utils
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace AZ::Dom::Tests
|
||||
{
|
||||
AZStd::string serializedDocument;
|
||||
JsonBackend backend;
|
||||
auto result = Dom::Utils::WriteToString(backend, serializedDocument, visitDocumentFn);
|
||||
auto result = backend.WriteToBuffer(serializedDocument, visitDocumentFn);
|
||||
EXPECT_TRUE(result.IsSuccess());
|
||||
EXPECT_EQ(canonicalSerializedDocument, serializedDocument);
|
||||
}
|
||||
@@ -92,8 +92,8 @@ namespace AZ::Dom::Tests
|
||||
{
|
||||
AZStd::string serializedDocument;
|
||||
JsonBackend backend;
|
||||
auto result = Dom::Utils::WriteToString(
|
||||
backend, serializedDocument,
|
||||
auto result = backend.WriteToBuffer(
|
||||
serializedDocument,
|
||||
[&backend, &canonicalSerializedDocument](AZ::Dom::Visitor& visitor)
|
||||
{
|
||||
return Dom::Utils::ReadFromString(backend, canonicalSerializedDocument, Lifetime::Temporary, visitor);
|
||||
|
||||
Reference in New Issue
Block a user