3
0

MaterialPropertyConnectionSerializer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Edit/Material/MaterialPropertyConnectionSerializer.h>
  9. #include <Atom/RPI.Edit/Material/MaterialUtils.h>
  10. #include <AzCore/Serialization/Json/BaseJsonSerializer.h>
  11. #include <AzCore/Serialization/Json/JsonSerializationResult.h>
  12. #include <AzCore/Serialization/Json/JsonSerialization.h>
  13. #include <AzCore/Serialization/Json/StackedString.h>
  14. namespace AZ
  15. {
  16. namespace RPI
  17. {
  18. namespace JsonMaterialPropertyConnectionSerializerInternal
  19. {
  20. namespace Field
  21. {
  22. static constexpr const char type[] = "type";
  23. static constexpr const char name[] = "name";
  24. static constexpr const char id[] = "id"; // For backward compatibility
  25. }
  26. static const AZStd::string_view AcceptedFields[] =
  27. {
  28. Field::type,
  29. Field::name,
  30. Field::id
  31. };
  32. }
  33. AZ_CLASS_ALLOCATOR_IMPL(JsonMaterialPropertyConnectionSerializer, SystemAllocator);
  34. JsonSerializationResult::Result JsonMaterialPropertyConnectionSerializer::Load(void* outputValue, const Uuid& outputValueTypeId,
  35. const rapidjson::Value& inputValue, JsonDeserializerContext& context)
  36. {
  37. namespace JSR = JsonSerializationResult;
  38. using namespace JsonMaterialPropertyConnectionSerializerInternal;
  39. AZ_Assert(azrtti_typeid<MaterialPropertySourceData::Connection>() == outputValueTypeId,
  40. "Unable to deserialize material property connection to json because the provided type is %s",
  41. outputValueTypeId.ToString<AZStd::string>().c_str());
  42. AZ_UNUSED(outputValueTypeId);
  43. MaterialPropertySourceData::Connection* propertyConnection = reinterpret_cast<MaterialPropertySourceData::Connection*>(outputValue);
  44. AZ_Assert(propertyConnection, "Output value for JsonMaterialPropertyConnectionSerializer can't be null.");
  45. JSR::ResultCode result(JSR::Tasks::ReadField);
  46. if (!inputValue.IsObject())
  47. {
  48. return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, "Property connection must be a JSON object.");
  49. }
  50. MaterialUtils::CheckForUnrecognizedJsonFields(AcceptedFields, AZ_ARRAY_SIZE(AcceptedFields), inputValue, context, result);
  51. result.Combine(ContinueLoadingFromJsonObjectField(&propertyConnection->m_type, azrtti_typeid<MaterialPropertyOutputType>(), inputValue, Field::type, context));
  52. JsonSerializationResult::ResultCode nameResult = ContinueLoadingFromJsonObjectField(&propertyConnection->m_name, azrtti_typeid<AZStd::string>(), inputValue, Field::name, context);
  53. if (nameResult.GetOutcome() == JsonSerializationResult::Outcomes::DefaultsUsed)
  54. {
  55. // This "id" key is for backward compatibility.
  56. result.Combine(ContinueLoadingFromJsonObjectField(&propertyConnection->m_name, azrtti_typeid<AZStd::string>(), inputValue, Field::id, context));
  57. }
  58. else
  59. {
  60. result.Combine(nameResult);
  61. }
  62. if (result.GetProcessing() == JsonSerializationResult::Processing::Completed)
  63. {
  64. return context.Report(result, "Successfully loaded property connection.");
  65. }
  66. else
  67. {
  68. return context.Report(result, "Partially loaded property connection.");
  69. }
  70. }
  71. JsonSerializationResult::Result JsonMaterialPropertyConnectionSerializer::Store(rapidjson::Value& outputValue, const void* inputValue,
  72. [[maybe_unused]] const void* defaultValue, const Uuid& valueTypeId, JsonSerializerContext& context)
  73. {
  74. namespace JSR = JsonSerializationResult;
  75. using namespace JsonMaterialPropertyConnectionSerializerInternal;
  76. AZ_Assert(azrtti_typeid<MaterialPropertySourceData::Connection>() == valueTypeId,
  77. "Unable to serialize material property connection to json because the provided type is %s",
  78. valueTypeId.ToString<AZStd::string>().c_str());
  79. AZ_UNUSED(valueTypeId);
  80. const MaterialPropertySourceData::Connection* propertyConnection = reinterpret_cast<const MaterialPropertySourceData::Connection*>(inputValue);
  81. AZ_Assert(propertyConnection, "Input value for JsonMaterialPropertyConnectionSerializer can't be null.");
  82. JSR::ResultCode result(JSR::Tasks::WriteValue);
  83. outputValue.SetObject();
  84. MaterialPropertySourceData::Connection defaultConnection;
  85. result.Combine(ContinueStoringToJsonObjectField(outputValue, Field::type, &propertyConnection->m_type, &defaultConnection.m_type, azrtti_typeid<MaterialPropertyOutputType>(), context));
  86. result.Combine(ContinueStoringToJsonObjectField(outputValue, Field::name, &propertyConnection->m_name, &defaultConnection.m_name, azrtti_typeid<AZStd::string>(), context));
  87. if (result.GetProcessing() == JsonSerializationResult::Processing::Completed)
  88. {
  89. return context.Report(result, "Successfully stored property connection.");
  90. }
  91. else
  92. {
  93. return context.Report(result, "Partially stored property connection.");
  94. }
  95. }
  96. } // namespace RPI
  97. } // namespace AZ