MaterialPropertyGroupSerializer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/MaterialPropertyGroupSerializer.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 JsonMaterialPropertyGroupSerializerInternal
  19. {
  20. namespace Field
  21. {
  22. static constexpr const char name[] = "name";
  23. static constexpr const char id[] = "id"; // For backward compatibility
  24. static constexpr const char displayName[] = "displayName";
  25. static constexpr const char description[] = "description";
  26. }
  27. static const AZStd::string_view AcceptedFields[] =
  28. {
  29. Field::name,
  30. Field::id,
  31. Field::displayName,
  32. Field::description
  33. };
  34. }
  35. AZ_CLASS_ALLOCATOR_IMPL(JsonMaterialPropertyGroupSerializer, SystemAllocator);
  36. JsonSerializationResult::Result JsonMaterialPropertyGroupSerializer::Load(void* outputValue, const Uuid& outputValueTypeId,
  37. const rapidjson::Value& inputValue, JsonDeserializerContext& context)
  38. {
  39. namespace JSR = JsonSerializationResult;
  40. using namespace JsonMaterialPropertyGroupSerializerInternal;
  41. AZ_Assert(azrtti_typeid<MaterialTypeSourceData::GroupDefinition>() == outputValueTypeId,
  42. "Unable to deserialize material property group to json because the provided type is %s",
  43. outputValueTypeId.ToString<AZStd::string>().c_str());
  44. AZ_UNUSED(outputValueTypeId);
  45. MaterialTypeSourceData::GroupDefinition* propertyGroup = reinterpret_cast<MaterialTypeSourceData::GroupDefinition*>(outputValue);
  46. AZ_Assert(propertyGroup, "Output value for JsonMaterialPropertyGroupSerializer can't be null.");
  47. JSR::ResultCode result(JSR::Tasks::ReadField);
  48. if (!inputValue.IsObject())
  49. {
  50. return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, "Property group must be a JSON object.");
  51. }
  52. MaterialUtils::CheckForUnrecognizedJsonFields(AcceptedFields, AZ_ARRAY_SIZE(AcceptedFields), inputValue, context, result);
  53. JsonSerializationResult::ResultCode nameResult = ContinueLoadingFromJsonObjectField(&propertyGroup->m_name, azrtti_typeid<AZStd::string>(), inputValue, Field::name, context);
  54. if (nameResult.GetOutcome() == JsonSerializationResult::Outcomes::DefaultsUsed)
  55. {
  56. // This "id" key is for backward compatibility.
  57. result.Combine(ContinueLoadingFromJsonObjectField(&propertyGroup->m_name, azrtti_typeid<AZStd::string>(), inputValue, Field::id, context));
  58. }
  59. else
  60. {
  61. result.Combine(nameResult);
  62. }
  63. result.Combine(ContinueLoadingFromJsonObjectField(&propertyGroup->m_displayName, azrtti_typeid<AZStd::string>(), inputValue, Field::displayName, context));
  64. result.Combine(ContinueLoadingFromJsonObjectField(&propertyGroup->m_description, azrtti_typeid<AZStd::string>(), inputValue, Field::description, context));
  65. if (result.GetProcessing() == JsonSerializationResult::Processing::Completed)
  66. {
  67. return context.Report(result, "Successfully loaded property group.");
  68. }
  69. else
  70. {
  71. return context.Report(result, "Partially loaded property group.");
  72. }
  73. }
  74. JsonSerializationResult::Result JsonMaterialPropertyGroupSerializer::Store(rapidjson::Value& outputValue, const void* inputValue,
  75. [[maybe_unused]] const void* defaultValue, const Uuid& valueTypeId, JsonSerializerContext& context)
  76. {
  77. namespace JSR = JsonSerializationResult;
  78. using namespace JsonMaterialPropertyGroupSerializerInternal;
  79. AZ_Assert(azrtti_typeid<MaterialTypeSourceData::GroupDefinition>() == valueTypeId,
  80. "Unable to serialize material property group to json because the provided type is %s",
  81. valueTypeId.ToString<AZStd::string>().c_str());
  82. AZ_UNUSED(valueTypeId);
  83. const MaterialTypeSourceData::GroupDefinition* propertyGroup = reinterpret_cast<const MaterialTypeSourceData::GroupDefinition*>(inputValue);
  84. AZ_Assert(propertyGroup, "Input value for JsonMaterialPropertyGroupSerializer can't be null.");
  85. JSR::ResultCode result(JSR::Tasks::WriteValue);
  86. outputValue.SetObject();
  87. AZStd::string defaultEmpty;
  88. result.Combine(ContinueStoringToJsonObjectField(outputValue, Field::name, &propertyGroup->m_name, &defaultEmpty, azrtti_typeid<AZStd::string>(), context));
  89. result.Combine(ContinueStoringToJsonObjectField(outputValue, Field::displayName, &propertyGroup->m_displayName, &defaultEmpty, azrtti_typeid<AZStd::string>(), context));
  90. result.Combine(ContinueStoringToJsonObjectField(outputValue, Field::description, &propertyGroup->m_description, &defaultEmpty, azrtti_typeid<AZStd::string>(), context));
  91. if (result.GetProcessing() == JsonSerializationResult::Processing::Completed)
  92. {
  93. return context.Report(result, "Successfully stored property group.");
  94. }
  95. else
  96. {
  97. return context.Report(result, "Partially stored property group.");
  98. }
  99. }
  100. } // namespace RPI
  101. } // namespace AZ