Merge pull request #1200 from aws-lumberyard-dev/JsonSerialization/UnsupportedWarnings
Improved reporting on unsupported types by the Json Serializationmain
commit
463e0cfff3
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/Memory/SystemAllocator.h>
|
||||
#include <AzCore/Serialization/Json/UnsupportedTypesSerializer.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
AZ_CLASS_ALLOCATOR_IMPL(JsonUnsupportedTypesSerializer, SystemAllocator, 0);
|
||||
AZ_CLASS_ALLOCATOR_IMPL(JsonAnySerializer, SystemAllocator, 0);
|
||||
AZ_CLASS_ALLOCATOR_IMPL(JsonVariantSerializer, SystemAllocator, 0);
|
||||
AZ_CLASS_ALLOCATOR_IMPL(JsonOptionalSerializer, SystemAllocator, 0);
|
||||
|
||||
JsonSerializationResult::Result JsonUnsupportedTypesSerializer::Load(void*, const Uuid&, const rapidjson::Value&,
|
||||
JsonDeserializerContext& context)
|
||||
{
|
||||
namespace JSR = JsonSerializationResult;
|
||||
return context.Report(JSR::Tasks::ReadField, JSR::Outcomes::Invalid, GetMessage());
|
||||
}
|
||||
|
||||
JsonSerializationResult::Result JsonUnsupportedTypesSerializer::Store(rapidjson::Value&, const void*, const void*, const Uuid&,
|
||||
JsonSerializerContext& context)
|
||||
{
|
||||
namespace JSR = JsonSerializationResult;
|
||||
return context.Report(JSR::Tasks::WriteValue, JSR::Outcomes::Invalid, GetMessage());
|
||||
}
|
||||
|
||||
AZStd::string_view JsonAnySerializer::GetMessage() const
|
||||
{
|
||||
return "The Json Serialization doesn't support AZStd::any by design. The Json Serialization attempts to minimize the use of $type, "
|
||||
"in particular the guid version, but no way has yet been found to use AZStd::any without explicitly and always requiring "
|
||||
"one.";
|
||||
}
|
||||
|
||||
AZStd::string_view JsonVariantSerializer::GetMessage() const
|
||||
{
|
||||
return "The Json Serialization doesn't support AZStd::variant by design. The Json Serialization attempts to minimize the use of "
|
||||
"$type, in particular the guid version. While combinations of AZStd::variant can be constructed that don't require a $type, "
|
||||
"this cannot be guaranteed in general.";
|
||||
}
|
||||
|
||||
AZStd::string_view JsonOptionalSerializer::GetMessage() const
|
||||
{
|
||||
return "The Json Serialization doesn't support AZStd::optional by design. No JSON format has yet been found that wasn't deemed too "
|
||||
"complex or overly verbose.";
|
||||
}
|
||||
} // namespace AZ
|
||||
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/Memory/Memory.h>
|
||||
#include <AzCore/Serialization/Json/BaseJsonSerializer.h>
|
||||
#include <AzCore/std/string/string_view.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
class JsonUnsupportedTypesSerializer : public BaseJsonSerializer
|
||||
{
|
||||
public:
|
||||
AZ_RTTI(JsonUnsupportedTypesSerializer, "{AFCC76B9-1F28-429D-8B4E-020BFD95ADAC}", BaseJsonSerializer);
|
||||
AZ_CLASS_ALLOCATOR_DECL;
|
||||
|
||||
JsonSerializationResult::Result Load(
|
||||
void* outputValue,
|
||||
const Uuid& outputValueTypeId,
|
||||
const rapidjson::Value& inputValue,
|
||||
JsonDeserializerContext& context) override;
|
||||
JsonSerializationResult::Result Store(
|
||||
rapidjson::Value& outputValue,
|
||||
const void* inputValue,
|
||||
const void* defaultValue,
|
||||
const Uuid& valueTypeId,
|
||||
JsonSerializerContext& context) override;
|
||||
|
||||
protected:
|
||||
virtual AZStd::string_view GetMessage() const = 0;
|
||||
};
|
||||
|
||||
class JsonAnySerializer : public JsonUnsupportedTypesSerializer
|
||||
{
|
||||
public:
|
||||
AZ_RTTI(JsonAnySerializer, "{699A0864-C4E2-4266-8141-99793C76870F}", JsonUnsupportedTypesSerializer);
|
||||
AZ_CLASS_ALLOCATOR_DECL;
|
||||
|
||||
protected:
|
||||
AZStd::string_view GetMessage() const override;
|
||||
};
|
||||
|
||||
class JsonVariantSerializer : public JsonUnsupportedTypesSerializer
|
||||
{
|
||||
public:
|
||||
AZ_RTTI(JsonVariantSerializer, "{08F8E746-F8A4-4E83-8902-713E90F3F498}", JsonUnsupportedTypesSerializer);
|
||||
AZ_CLASS_ALLOCATOR_DECL;
|
||||
|
||||
protected:
|
||||
AZStd::string_view GetMessage() const override;
|
||||
};
|
||||
|
||||
class JsonOptionalSerializer : public JsonUnsupportedTypesSerializer
|
||||
{
|
||||
public:
|
||||
AZ_RTTI(JsonOptionalSerializer, "{F8AF1C95-BD1B-44D2-9B4A-F5726133A104}", JsonUnsupportedTypesSerializer);
|
||||
AZ_CLASS_ALLOCATOR_DECL;
|
||||
|
||||
protected:
|
||||
AZStd::string_view GetMessage() const override;
|
||||
};
|
||||
} // namespace AZ
|
||||
@ -0,0 +1,142 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/Serialization/Json/UnsupportedTypesSerializer.h>
|
||||
#include <AzCore/std/any.h>
|
||||
#include <AzCore/std/optional.h>
|
||||
#include <AzCore/std/containers/variant.h>
|
||||
#include <Tests/Serialization/Json/BaseJsonSerializerFixture.h>
|
||||
|
||||
namespace JsonSerializationTests
|
||||
{
|
||||
struct AnyInfo
|
||||
{
|
||||
using Type = AZStd::any;
|
||||
using Serializer = AZ::JsonAnySerializer;
|
||||
};
|
||||
|
||||
struct VariantInfo
|
||||
{
|
||||
using Type = AZStd::variant<AZStd::monostate, int, double>;
|
||||
using Serializer = AZ::JsonVariantSerializer;
|
||||
};
|
||||
|
||||
struct OptionalInfo
|
||||
{
|
||||
using Type = AZStd::optional<int>;
|
||||
using Serializer = AZ::JsonVariantSerializer;
|
||||
};
|
||||
|
||||
template<typename Info>
|
||||
class JsonUnsupportedTypesSerializerTests : public BaseJsonSerializerFixture
|
||||
{
|
||||
public:
|
||||
using Type = typename Info::Type;
|
||||
using Serializer = typename Info::Serializer;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
BaseJsonSerializerFixture::SetUp();
|
||||
this->m_serializer = AZStd::make_unique<Serializer>();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
this->m_serializer.reset();
|
||||
BaseJsonSerializerFixture::TearDown();
|
||||
}
|
||||
|
||||
protected:
|
||||
AZStd::unique_ptr<Serializer> m_serializer;
|
||||
Type m_instance{};
|
||||
};
|
||||
|
||||
using UnsupportedTypesTestTypes = ::testing::Types<AnyInfo, VariantInfo, OptionalInfo>;
|
||||
TYPED_TEST_CASE(JsonUnsupportedTypesSerializerTests, UnsupportedTypesTestTypes);
|
||||
|
||||
TYPED_TEST(JsonUnsupportedTypesSerializerTests, Load_CallDirectly_ReportsIssueAndHalts)
|
||||
{
|
||||
namespace JSR = AZ::JsonSerializationResult;
|
||||
|
||||
bool hasMessage = false;
|
||||
auto callback = [&hasMessage](AZStd::string_view message, JSR::ResultCode result, AZStd::string_view) -> JSR::ResultCode
|
||||
{
|
||||
hasMessage = !message.empty();
|
||||
return result;
|
||||
};
|
||||
this->m_jsonDeserializationContext->PushReporter(AZStd::move(callback));
|
||||
|
||||
JSR::Result result = this->m_serializer->Load(
|
||||
&this->m_instance, azrtti_typeid(this->m_instance), *this->m_jsonDocument, *this->m_jsonDeserializationContext);
|
||||
this->m_jsonDeserializationContext->PopReporter();
|
||||
|
||||
EXPECT_EQ(JSR::Processing::Halted, result.GetResultCode().GetProcessing());
|
||||
EXPECT_TRUE(hasMessage);
|
||||
}
|
||||
|
||||
TYPED_TEST(JsonUnsupportedTypesSerializerTests, Load_CallThroughFrontEnd_ReportsIssueAndHalts)
|
||||
{
|
||||
namespace JSR = AZ::JsonSerializationResult;
|
||||
|
||||
bool hasMessage = false;
|
||||
auto callback = [&hasMessage](AZStd::string_view message, JSR::ResultCode result, AZStd::string_view) -> JSR::ResultCode
|
||||
{
|
||||
hasMessage = !message.empty();
|
||||
return result;
|
||||
};
|
||||
this->m_deserializationSettings->m_reporting = AZStd::move(callback);
|
||||
|
||||
JSR::ResultCode result = AZ::JsonSerialization::Load(this->m_instance, *this->m_jsonDocument, *this->m_deserializationSettings);
|
||||
|
||||
EXPECT_EQ(JSR::Processing::Halted, result.GetProcessing());
|
||||
EXPECT_TRUE(hasMessage);
|
||||
}
|
||||
|
||||
TYPED_TEST(JsonUnsupportedTypesSerializerTests, Save_CallDirectly_ReportsIssueAndHalts)
|
||||
{
|
||||
namespace JSR = AZ::JsonSerializationResult;
|
||||
|
||||
bool hasMessage = false;
|
||||
auto callback = [&hasMessage](AZStd::string_view message, JSR::ResultCode result, AZStd::string_view) -> JSR::ResultCode
|
||||
{
|
||||
hasMessage = !message.empty();
|
||||
return result;
|
||||
};
|
||||
this->m_jsonSerializationContext->PushReporter(AZStd::move(callback));
|
||||
|
||||
JSR::Result result = this->m_serializer->Store(
|
||||
*this->m_jsonDocument, &this->m_instance, nullptr, azrtti_typeid(this->m_instance), *this->m_jsonSerializationContext);
|
||||
this->m_jsonSerializationContext->PopReporter();
|
||||
|
||||
EXPECT_EQ(JSR::Processing::Halted, result.GetResultCode().GetProcessing());
|
||||
EXPECT_TRUE(hasMessage);
|
||||
}
|
||||
|
||||
TYPED_TEST(JsonUnsupportedTypesSerializerTests, Save_CallThroughFrontEnd_ReportsIssueAndHalts)
|
||||
{
|
||||
namespace JSR = AZ::JsonSerializationResult;
|
||||
|
||||
bool hasMessage = false;
|
||||
auto callback = [&hasMessage](AZStd::string_view message, JSR::ResultCode result, AZStd::string_view) -> JSR::ResultCode
|
||||
{
|
||||
hasMessage = !message.empty();
|
||||
return result;
|
||||
};
|
||||
this->m_serializationSettings->m_reporting = AZStd::move(callback);
|
||||
|
||||
JSR::ResultCode result = AZ::JsonSerialization::Store(
|
||||
*this->m_jsonDocument, this->m_jsonDocument->GetAllocator(), this->m_instance, *this->m_serializationSettings);
|
||||
|
||||
EXPECT_EQ(JSR::Processing::Halted, result.GetProcessing());
|
||||
EXPECT_TRUE(hasMessage);
|
||||
}
|
||||
} // namespace JsonSerializationTests
|
||||
Loading…
Reference in New Issue