/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include namespace AZ { namespace RPI { AZ_CLASS_ALLOCATOR_IMPL(JsonMaterialPropertyValueSerializer, SystemAllocator, 0); template JsonSerializationResult::ResultCode JsonMaterialPropertyValueSerializer::LoadVariant( MaterialPropertyValue& intoValue, const T& defaultValue, const rapidjson::Value& inputValue, JsonDeserializerContext& context) { T value = defaultValue; JsonSerializationResult::ResultCode result = ContinueLoading(&value, azrtti_typeid(), inputValue, context); intoValue = value; return result; } JsonSerializationResult::Result JsonMaterialPropertyValueSerializer::Load(void* outputValue, const Uuid& outputValueTypeId, const rapidjson::Value& inputValue, JsonDeserializerContext& context) { namespace JSR = JsonSerializationResult; AZ_Assert(azrtti_typeid() == outputValueTypeId, "Unable to deserialize material property value to json because the provided type is %s", outputValueTypeId.ToString().c_str()); AZ_UNUSED(outputValueTypeId); MaterialPropertyValue* property = reinterpret_cast(outputValue); AZ_Assert(property, "Output value for JsonMaterialPropertyValueSerializer can't be null."); JSR::ResultCode result(JSR::Tasks::ReadField); if (inputValue.IsBool()) { result.Combine(LoadVariant(*property, false, inputValue, context)); } else if (inputValue.IsInt() || inputValue.IsInt64()) { result.Combine(LoadVariant(*property, 0, inputValue, context)); } else if (inputValue.IsUint() || inputValue.IsUint64()) { result.Combine(LoadVariant(*property, 0u, inputValue, context)); } else if (inputValue.IsFloat() || inputValue.IsDouble()) { result.Combine(LoadVariant(*property, 0.0f, inputValue, context)); } else if (inputValue.IsArray() && inputValue.Size() == 4) { result.Combine(LoadVariant(*property, Vector4{0.0f, 0.0f, 0.0f, 0.0f}, inputValue, context)); } else if (inputValue.IsArray() && inputValue.Size() == 3) { result.Combine(LoadVariant(*property, Vector3{0.0f, 0.0f, 0.0f}, inputValue, context)); } else if (inputValue.IsArray() && inputValue.Size() == 2) { result.Combine(LoadVariant(*property, Vector2{0.0f, 0.0f}, inputValue, context)); } else if (inputValue.IsObject()) { JsonSerializationResult::ResultCode resultCode = LoadVariant(*property, Color::CreateZero(), inputValue, context); if(resultCode.GetProcessing() != JsonSerializationResult::Processing::Completed) { resultCode = LoadVariant(*property, Vector4::CreateZero(), inputValue, context); } if(resultCode.GetProcessing() != JsonSerializationResult::Processing::Completed) { resultCode = LoadVariant(*property, Vector3::CreateZero(), inputValue, context); } if(resultCode.GetProcessing() != JsonSerializationResult::Processing::Completed) { resultCode = LoadVariant(*property, Vector2::CreateZero(), inputValue, context); } if(resultCode.GetProcessing() == JsonSerializationResult::Processing::Completed) { result.Combine(resultCode); } else { return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, "Unknown data type"); } } else if (inputValue.IsString()) { result.Combine(LoadVariant(*property, AZStd::string{}, inputValue, context)); } else { return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, "Unknown data type"); } if (result.GetProcessing() == JsonSerializationResult::Processing::Completed) { return context.Report(result, "Successfully loaded property value."); } else { return context.Report(result, "Partially loaded property value."); } } JsonSerializationResult::Result JsonMaterialPropertyValueSerializer::Store(rapidjson::Value& outputValue, const void* inputValue, [[maybe_unused]] const void* defaultValue, const Uuid& valueTypeId, JsonSerializerContext& context) { namespace JSR = JsonSerializationResult; AZ_Assert(azrtti_typeid() == valueTypeId, "Unable to serialize material property value to json because the provided type is %s", valueTypeId.ToString().c_str()); AZ_UNUSED(valueTypeId); const MaterialPropertyValue* property = reinterpret_cast(inputValue); AZ_Assert(property, "Input value for JsonMaterialPropertyValueSerializer can't be null."); JSR::ResultCode result(JSR::Tasks::WriteValue); if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->Is()) { result.Combine(ContinueStoring(outputValue, &property->GetValue(), nullptr, azrtti_typeid(), context)); } if (result.GetProcessing() == JsonSerializationResult::Processing::Completed) { return context.Report(result, "Successfully stored property value."); } else { return context.Report(result, "Partially stored property value."); } } } // namespace RPI } // namespace AZ