A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
DlgJsonWriter.h
Go to the documentation of this file.
1// Copyright Csaba Molnar, Daniel Butum. All Rights Reserved.
2#pragma once
3
4#include "Logging/LogMacros.h"
5#include "UObject/UnrealType.h"
6#include "Misc/FileHelper.h"
7#include "Dom/JsonValue.h"
8#include "Dom/JsonObject.h"
9
10#include "IDlgWriter.h"
11
13
14
15struct DLGSYSTEM_API DlgJsonWriterOptions
16{
17 int32 InitialIndent = 0;
18 bool bPrettyPrint = true;
19};
20
25class DLGSYSTEM_API FDlgJsonWriter : public IDlgWriter
26{
38public:
39
41
42 // IDlgWriter Interface
43 void Write(const UStruct* StructDefinition, const void* ContainerPtr) override;
44
50 bool ExportToFile(const FString& FileName) override
51 {
52 return FFileHelper::SaveStringToFile(JsonString, *FileName, FFileHelper::EEncodingOptions::ForceUTF8WithoutBOM);
53 }
54
55 const FString& GetAsString() const override
56 {
57 return JsonString;
58 }
59
60private: // UStruct -> JSON
65 TSharedPtr<FJsonValue> ConvertScalarPropertyToJsonValue(const FNYProperty* Property, const void* const ContainerPtr, const void* const ValuePtr);
66
75 TSharedPtr<FJsonValue> PropertyToJsonValue(const FNYProperty* Property, const void* const ContainerPtr, const void* const ValuePtr);
76
86 bool UStructToJsonAttributes(const UStruct* StructDefinition, const void* const ContainerPtr,
87 TMap<FString, TSharedPtr<FJsonValue>>& OutJsonAttributes);
88
98 bool UStructToJsonObject(const UStruct* StructDefinition, const void* const ContainerPtr, TSharedRef<FJsonObject>& OutJsonObject)
99 {
100 return UStructToJsonAttributes(StructDefinition, ContainerPtr, OutJsonObject->Values);
101 }
102
112 bool UStructToJsonString(const UStruct* StructDefinition, const void* const ContainerPtr, const DlgJsonWriterOptions& Options,
113 FString& OutJsonString);
114
116 {
117 IndexInArray = INDEX_NONE;
118 bIsPropertyMapKey = false;
119 }
120
121private:
122 // Final output string
123 FString JsonString;
124
126 static constexpr int64 CheckFlags = ~CPF_ParmFlags; // all properties except those who have these flags? TODO is this ok?
127
128 // Used when parsing
129
130 // If it is in an array this is != INDEX_NONE
131 int32 IndexInArray = INDEX_NONE;
132
133 // Is the current property used as a map key?
134 bool bIsPropertyMapKey = false;
135};
DECLARE_LOG_CATEGORY_EXTERN(LogDlgJsonWriter, All, All)
UProperty FNYProperty
The DlgJsonWriter class mostly adapted for Dialogues, copied from FJsonObjectConverter See IDlgWriter...
const FString & GetAsString() const override
bool UStructToJsonObject(const UStruct *StructDefinition, const void *const ContainerPtr, TSharedRef< FJsonObject > &OutJsonObject)
bool ExportToFile(const FString &FileName) override