Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. FString ObjectToJsonString(const UObject* Obj);
  2. FString StructToJsonString(const UStruct* Struct, const void* StructData);
  3. FString PropertyValueToJsonString(const UProperty* Prop, const void* ValuePtr);
  4.  
  5. FString ObjectToJsonString(const UObject* Obj)
  6. {
  7.     FString ObjJsonString;
  8.     ObjJsonString += TEXT("{ ");
  9.  
  10.     bool bIsFirst = true;
  11.     for (TFieldIterator<const UProperty> PropIt(Obj->GetClass(), EFieldIteratorFlags::IncludeSuper, EFieldIteratorFlags::ExcludeDeprecated, EFieldIteratorFlags::IncludeInterfaces); PropIt; ++PropIt)
  12.     {
  13.         if (!bIsFirst)
  14.         {
  15.             ObjJsonString += TEXT(", ");
  16.         }
  17.         bIsFirst = false;
  18.  
  19.         ObjJsonString += PropIt->GetName();
  20.         ObjJsonString += TEXT(": ");
  21.         ObjJsonString += PropertyValueToJsonString(*PropIt, PropIt->ContainerPtrToValuePtr<void>(Obj));
  22.     }
  23.  
  24.     ObjJsonString += TEXT(" }");
  25.     return ObjJsonString;
  26. }
  27.  
  28. FString StructToJsonString(const UStruct* Struct, const void* StructData)
  29. {
  30.     FString StructJsonString;
  31.     StructJsonString += TEXT("{ ");
  32.  
  33.     bool bIsFirst = true;
  34.     for (TFieldIterator<const UProperty> PropIt(Struct, EFieldIteratorFlags::IncludeSuper, EFieldIteratorFlags::ExcludeDeprecated, EFieldIteratorFlags::IncludeInterfaces); PropIt; ++PropIt)
  35.     {
  36.         if (!bIsFirst)
  37.         {
  38.             StructJsonString += TEXT(", ");
  39.         }
  40.         bIsFirst = false;
  41.  
  42.         StructJsonString += PropIt->GetName();
  43.         StructJsonString += TEXT(": ");
  44.         StructJsonString += PropertyValueToJsonString(*PropIt, PropIt->ContainerPtrToValuePtr<void>(StructData));
  45.     }
  46.  
  47.     StructJsonString += TEXT(" }");
  48.     return StructJsonString;
  49. }
  50.  
  51. FString PropertyValueToJsonString(const UProperty* Prop, const void* ValuePtr)
  52. {
  53.     FString PropJsonString;
  54.  
  55.     if (Prop->ArrayDim > 1)
  56.     {
  57.         PropJsonString += TEXT("[ ");
  58.     }
  59.  
  60.     for (int32 i = 0; i < Prop->ArrayDim; ++i)
  61.     {
  62.         const void* const ElementValueAddress = reinterpret_cast<const uint8*>(ValuePtr) + (Prop->ElementSize * i);
  63.  
  64.         if (i > 0)
  65.         {
  66.             PropJsonString += TEXT(", ");
  67.         }
  68.  
  69.         if (const UArrayProperty* const ArrayProp = Cast<const UArrayProperty>(Prop))
  70.         {
  71.             PropJsonString += TEXT("[ ");
  72.  
  73.             FScriptArrayHelper ScriptArrayHelper(ArrayProp, ElementValueAddress);
  74.             const int32 ElementCount = ScriptArrayHelper.Num();
  75.             for (int32 j = 0; j < ElementCount; ++j)
  76.             {
  77.                 if (j > 0)
  78.                 {
  79.                     PropJsonString += TEXT(", ");
  80.                 }
  81.  
  82.                 PropJsonString += PropertyValueToJsonString(ArrayProp->Inner, ScriptArrayHelper.GetRawPtr(j));
  83.             }
  84.  
  85.             PropJsonString += TEXT(" ]");
  86.         }
  87.         else if (const UMapProperty* const MapProp = Cast<const UMapProperty>(Prop))
  88.         {
  89.             PropJsonString += TEXT("{ ");
  90.  
  91.             bool bIsFirstMapEntry = true;
  92.             FScriptMapHelper ScriptMapHelper(MapProp, ElementValueAddress);
  93.             const int32 ElementCount = ScriptMapHelper.Num();
  94.             for (int32 j = 0; j < ElementCount; ++j)
  95.             {
  96.                 if (!ScriptMapHelper.IsValidIndex(j))
  97.                 {
  98.                     continue;
  99.                 }
  100.  
  101.                 if (!bIsFirstMapEntry)
  102.                 {
  103.                     PropJsonString += TEXT(", ");
  104.                 }
  105.                 bIsFirstMapEntry = false;
  106.  
  107.                 const uint8* MapPairPtr = ScriptMapHelper.GetPairPtr(j);
  108.                 PropJsonString += PropertyValueToJsonString(MapProp->KeyProp, MapPairPtr + MapProp->MapLayout.KeyOffset);
  109.                 PropJsonString += TEXT(": ");
  110.                 PropJsonString += PropertyValueToJsonString(MapProp->ValueProp, MapPairPtr + MapProp->MapLayout.ValueOffset);
  111.             }
  112.  
  113.             PropJsonString += TEXT(" }");
  114.         }
  115.         else if (const UStructProperty* const StructProp = Cast<const UStructProperty>(Prop))
  116.         {
  117.             PropJsonString += StructToJsonString(StructProp->Struct, ElementValueAddress);
  118.         }
  119.         else if (const UObjectPropertyBase* const ObjectProp = Cast<const UObjectPropertyBase>(Prop))
  120.         {
  121.             // note: need to detect loops
  122.             const UObject* InnerObject = ObjectProp->GetObjectPropertyValue(ElementValueAddress);
  123.             if (InnerObject)
  124.             {
  125.                 PropJsonString += ObjectToJsonString(InnerObject);
  126.             }
  127.         }
  128.         else
  129.         {
  130.             Prop->ExportText_Direct(PropJsonString, ElementValueAddress, ElementValueAddress, nullptr, PPF_Delimited);
  131.         }
  132.     }
  133.  
  134.     if (Prop->ArrayDim > 1)
  135.     {
  136.         PropJsonString += TEXT(" ]");
  137.     }
  138.  
  139.     return PropJsonString;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement