diff --git a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.cpp b/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.cpp deleted file mode 100644 index 288d3c0699..0000000000 --- a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.cpp +++ /dev/null @@ -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 - -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 JsonBackend::CreateStreamWriter(AZ::IO::GenericStream& stream) - { - return Json::GetJsonStreamWriter(stream, Json::OutputFormatting::PrettyPrintedJson); - } -} diff --git a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.h b/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.h index 536fec7418..1ecfa3fb37 100644 --- a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.h +++ b/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonBackend.h @@ -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 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 CreateStreamWriter(AZ::IO::GenericStream& stream) override; + Visitor::Result ReadFromStringInPlace(AZStd::string& buffer, Visitor& visitor) override + { + return Json::VisitSerializedJsonInPlace(buffer, visitor); + } + + Visitor::Result ReadFromString(AZStd::string_view buffer, Lifetime lifetime, Visitor& visitor) override + { + return Json::VisitSerializedJson(buffer, lifetime, visitor); + } + + AZStd::unique_ptr CreateStreamWriter(AZ::IO::GenericStream& stream) override + { + return Json::GetJsonStreamWriter(stream, WriteFormat); + } }; } // namespace AZ::Dom diff --git a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonSerializationUtils.h b/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonSerializationUtils.h index be055d7f5c..4197209b8e 100644 --- a/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonSerializationUtils.h +++ b/Code/Framework/AzCore/AzCore/DOM/Backends/JSON/JsonSerializationUtils.h @@ -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 + template 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 + template 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 + template 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(stream, handler); + reader.Parse(parseFlags)>(stream, handler); return handler.TakeOutcome(); } - template + template 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(stream, handler); + reader.Parse(parseFlags) | rapidjson::kParseInsituFlag>(stream, handler); return handler.TakeOutcome(); } } // namespace AZ::Dom::Json diff --git a/Code/Framework/AzCore/AzCore/azcore_files.cmake b/Code/Framework/AzCore/AzCore/azcore_files.cmake index ecc7a41ba1..8557773c93 100644 --- a/Code/Framework/AzCore/AzCore/azcore_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcore_files.cmake @@ -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