Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FString ObjectToJsonString(const UObject* Obj);
- FString StructToJsonString(const UStruct* Struct, const void* StructData);
- FString PropertyValueToJsonString(const UProperty* Prop, const void* ValuePtr);
- FString ObjectToJsonString(const UObject* Obj)
- {
- FString ObjJsonString;
- ObjJsonString += TEXT("{ ");
- bool bIsFirst = true;
- for (TFieldIterator<const UProperty> PropIt(Obj->GetClass(), EFieldIteratorFlags::IncludeSuper, EFieldIteratorFlags::ExcludeDeprecated, EFieldIteratorFlags::IncludeInterfaces); PropIt; ++PropIt)
- {
- if (!bIsFirst)
- {
- ObjJsonString += TEXT(", ");
- }
- bIsFirst = false;
- ObjJsonString += PropIt->GetName();
- ObjJsonString += TEXT(": ");
- ObjJsonString += PropertyValueToJsonString(*PropIt, PropIt->ContainerPtrToValuePtr<void>(Obj));
- }
- ObjJsonString += TEXT(" }");
- return ObjJsonString;
- }
- FString StructToJsonString(const UStruct* Struct, const void* StructData)
- {
- FString StructJsonString;
- StructJsonString += TEXT("{ ");
- bool bIsFirst = true;
- for (TFieldIterator<const UProperty> PropIt(Struct, EFieldIteratorFlags::IncludeSuper, EFieldIteratorFlags::ExcludeDeprecated, EFieldIteratorFlags::IncludeInterfaces); PropIt; ++PropIt)
- {
- if (!bIsFirst)
- {
- StructJsonString += TEXT(", ");
- }
- bIsFirst = false;
- StructJsonString += PropIt->GetName();
- StructJsonString += TEXT(": ");
- StructJsonString += PropertyValueToJsonString(*PropIt, PropIt->ContainerPtrToValuePtr<void>(StructData));
- }
- StructJsonString += TEXT(" }");
- return StructJsonString;
- }
- FString PropertyValueToJsonString(const UProperty* Prop, const void* ValuePtr)
- {
- FString PropJsonString;
- if (Prop->ArrayDim > 1)
- {
- PropJsonString += TEXT("[ ");
- }
- for (int32 i = 0; i < Prop->ArrayDim; ++i)
- {
- const void* const ElementValueAddress = reinterpret_cast<const uint8*>(ValuePtr) + (Prop->ElementSize * i);
- if (i > 0)
- {
- PropJsonString += TEXT(", ");
- }
- if (const UArrayProperty* const ArrayProp = Cast<const UArrayProperty>(Prop))
- {
- PropJsonString += TEXT("[ ");
- FScriptArrayHelper ScriptArrayHelper(ArrayProp, ElementValueAddress);
- const int32 ElementCount = ScriptArrayHelper.Num();
- for (int32 j = 0; j < ElementCount; ++j)
- {
- if (j > 0)
- {
- PropJsonString += TEXT(", ");
- }
- PropJsonString += PropertyValueToJsonString(ArrayProp->Inner, ScriptArrayHelper.GetRawPtr(j));
- }
- PropJsonString += TEXT(" ]");
- }
- else if (const UMapProperty* const MapProp = Cast<const UMapProperty>(Prop))
- {
- PropJsonString += TEXT("{ ");
- bool bIsFirstMapEntry = true;
- FScriptMapHelper ScriptMapHelper(MapProp, ElementValueAddress);
- const int32 ElementCount = ScriptMapHelper.Num();
- for (int32 j = 0; j < ElementCount; ++j)
- {
- if (!ScriptMapHelper.IsValidIndex(j))
- {
- continue;
- }
- if (!bIsFirstMapEntry)
- {
- PropJsonString += TEXT(", ");
- }
- bIsFirstMapEntry = false;
- const uint8* MapPairPtr = ScriptMapHelper.GetPairPtr(j);
- PropJsonString += PropertyValueToJsonString(MapProp->KeyProp, MapPairPtr + MapProp->MapLayout.KeyOffset);
- PropJsonString += TEXT(": ");
- PropJsonString += PropertyValueToJsonString(MapProp->ValueProp, MapPairPtr + MapProp->MapLayout.ValueOffset);
- }
- PropJsonString += TEXT(" }");
- }
- else if (const UStructProperty* const StructProp = Cast<const UStructProperty>(Prop))
- {
- PropJsonString += StructToJsonString(StructProp->Struct, ElementValueAddress);
- }
- else if (const UObjectPropertyBase* const ObjectProp = Cast<const UObjectPropertyBase>(Prop))
- {
- // note: need to detect loops
- const UObject* InnerObject = ObjectProp->GetObjectPropertyValue(ElementValueAddress);
- if (InnerObject)
- {
- PropJsonString += ObjectToJsonString(InnerObject);
- }
- }
- else
- {
- Prop->ExportText_Direct(PropJsonString, ElementValueAddress, ElementValueAddress, nullptr, PPF_Delimited);
- }
- }
- if (Prop->ArrayDim > 1)
- {
- PropJsonString += TEXT(" ]");
- }
- return PropJsonString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement