Make JsonBackend templated

Signed-off-by: Nicholas Van Sickle <nvsickle@amazon.com>
This commit is contained in:
Nicholas Van Sickle
2021-12-01 12:10:22 -08:00
parent f87021d856
commit e0508ddefc
4 changed files with 41 additions and 43 deletions
@@ -1,27 +0,0 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/DOM/Backends/JSON/JsonBackend.h>
namespace AZ::Dom
{
Visitor::Result JsonBackend::ReadFromStringInPlace(AZStd::string& buffer, Visitor& visitor)
{
return Json::VisitSerializedJsonInPlace(buffer, visitor);
}
Visitor::Result JsonBackend::ReadFromString(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor)
{
return Json::VisitSerializedJson(buffer, lifetime, visitor);
}
AZStd::unique_ptr<Visitor> JsonBackend::CreateStreamWriter(AZ::IO::GenericStream& stream)
{
return Json::GetJsonStreamWriter(stream, Json::OutputFormatting::PrettyPrintedJson);
}
}
@@ -13,11 +13,26 @@
namespace AZ::Dom
{
//! A DOM backend for serializing and deserializing JSON <=> UTF-8 text
//! \param ParseFlags Controls how deserialized JSON is parsed.
//! \param WriteFormat Controls how serialized JSON is formatted.
template<Json::ParseFlags ParseFlags = Json::ParseFlags::ParseComments, Json::OutputFormatting WriteFormat = Json::OutputFormatting::PrettyPrintedJson>
class JsonBackend final : public Backend
{
public:
Visitor::Result ReadFromStringInPlace(AZStd::string& buffer, Visitor& visitor) override;
Visitor::Result ReadFromString(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor) override;
AZStd::unique_ptr<Visitor> CreateStreamWriter(AZ::IO::GenericStream& stream) override;
Visitor::Result ReadFromStringInPlace(AZStd::string& buffer, Visitor& visitor) override
{
return Json::VisitSerializedJsonInPlace<ParseFlags>(buffer, visitor);
}
Visitor::Result ReadFromString(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor) override
{
return Json::VisitSerializedJson<ParseFlags>(buffer, lifetime, visitor);
}
AZStd::unique_ptr<Visitor> CreateStreamWriter(AZ::IO::GenericStream& stream) override
{
return Json::GetJsonStreamWriter(stream, WriteFormat);
}
};
} // namespace AZ::Dom
@@ -26,6 +26,21 @@ namespace AZ::Dom::Json
PrettyPrintedJson, //!< Formats JSON in a pretty printed form, focusing on legibility to readers.
};
//! Specifies parsing behavior when deserializing JSON.
enum class ParseFlags : int
{
Null = 0,
StopWhenDone = rapidjson::kParseStopWhenDoneFlag,
FullFloatingPointPrecision = rapidjson::kParseFullPrecisionFlag,
ParseComments = rapidjson::kParseCommentsFlag,
ParseNumbersAsStrings = rapidjson::kParseNumbersAsStringsFlag,
ParseTrailingCommas = rapidjson::kParseTrailingCommasFlag,
ParseNanAndInfinity = rapidjson::kParseNanAndInfFlag,
ParseEscapedApostrophies = rapidjson::kParseEscapedApostropheFlag,
};
AZ_DEFINE_ENUM_BITWISE_OPERATORS(ParseFlags);
//! Visitor that feeds into a rapidjson::Value
class RapidJsonValueWriter final : public Visitor
{
@@ -128,16 +143,18 @@ namespace AZ::Dom::Json
//! \param lifetime Specifies the lifetime of the specified buffer. If the string specified by buffer might be deallocated,
//! ensure Lifetime::Temporary is specified.
//! \param visitor The visitor to visit with the JSON buffer's contents.
//! \param parseFlags (template) Settings for adjusting parser behavior.
//! \return The aggregate result specifying whether the visitor operations were successful.
template<int ParseFlags = rapidjson::kParseCommentsFlag>
template<ParseFlags parseFlags = ParseFlags::ParseComments>
Visitor::Result VisitSerializedJson(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor);
//! Reads serialized JSON from a string in-place and applies it to a visitor.
//! \param buffer The UTF-8 serialized JSON to read. This buffer will be modified as part of the deserialization process to
//! apply null terminators.
//! \param visitor The visitor to visit with the JSON buffer's contents. The strings provided to the visitor will only
//! be valid for the lifetime of buffer.
//! \param parseFlags (template) Settings for adjusting parser behavior.
//! \return The aggregate result specifying whether the visitor operations were successful.
template<int ParseFlags = rapidjson::kParseCommentsFlag | rapidjson::kParseInsituFlag>
template<ParseFlags parseFlags = ParseFlags::ParseComments>
Visitor::Result VisitSerializedJsonInPlace(AZStd::string& buffer, Visitor& visitor);
//! Takes a visitor specified by a callback and produces a rapidjson::Document.
@@ -159,31 +176,25 @@ namespace AZ::Dom::Json
//! \return The aggregate result specifying whether the visitor operations were successful.
Visitor::Result VisitRapidJsonValue(const rapidjson::Value& value, Visitor& visitor, Lifetime lifetime);
template<int ParseFlags>
template<ParseFlags parseFlags>
Visitor::Result VisitSerializedJson(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor)
{
static_assert(
(ParseFlags & rapidjson::kParseInsituFlag) == 0,
"VisitSerializedJsonInPlace requires kParseInSituFlag not to be set, use VisitSerializedJsonInPlace to parse in-situ");
rapidjson::Reader reader;
rapidjson::MemoryStream stream(buffer.data(), buffer.size());
RapidJsonReadHandler handler(&visitor, lifetime);
reader.Parse<ParseFlags>(stream, handler);
reader.Parse<static_cast<int>(parseFlags)>(stream, handler);
return handler.TakeOutcome();
}
template<int ParseFlags>
template<ParseFlags parseFlags>
Visitor::Result VisitSerializedJsonInPlace(AZStd::string& buffer, Visitor& visitor)
{
static_assert((ParseFlags & rapidjson::kParseInsituFlag) != 0, "VisitSerializedJsonInPlace requires kParseInSituFlag to be set");
rapidjson::Reader reader;
AzStringStream stream(buffer);
RapidJsonReadHandler handler(&visitor, Lifetime::Persistent);
reader.Parse<ParseFlags>(stream, handler);
reader.Parse<static_cast<int>(parseFlags) | rapidjson::kParseInsituFlag>(stream, handler);
return handler.TakeOutcome();
}
} // namespace AZ::Dom::Json
@@ -129,7 +129,6 @@ set(FILES
DOM/DomBackend.h
DOM/DomVisitor.cpp
DOM/DomVisitor.h
DOM/Backends/JSON/JsonBackend.cpp
DOM/Backends/JSON/JsonBackend.h
DOM/Backends/JSON/JsonSerializationUtils.cpp
DOM/Backends/JSON/JsonSerializationUtils.h