Skip to content

Commit d045d35

Browse files
refactor: conversion of URawFormatValue to and from JSON
Each derived class of `URawFormatValue` now is responsible for the logic to convert itself to and from JSON. Use of the `FJsonRawFormatConverter` class is now deprecated.
1 parent 1a7d2ca commit d045d35

9 files changed

Lines changed: 132 additions & 43 deletions

File tree

Mods/SML/Source/SML/Private/Configuration/ConfigManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Util/SemVersion.h"
55
#include "TimerManager.h"
66
#include "Configuration/RootConfigValueHolder.h"
7+
#include "Configuration/RawFileFormat/RawFormatValueObject.h"
78
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
89
#include "Dom/JsonValue.h"
910
#include "Engine/GameInstance.h"
@@ -35,7 +36,7 @@ void UConfigManager::SaveConfigurationInternal(const FConfigId& ConfigId) {
3536
checkf(RawFormatValue, TEXT("Root RawFormatValue returned NULL for config %s"), *ConfigId.ModReference);
3637

3738
//Root value should always be JsonObject, since root property is section property
38-
const TSharedPtr<FJsonValue> JsonValue = FJsonRawFormatConverter::ConvertToJson(RawFormatValue);
39+
const TSharedPtr<FJsonValue> JsonValue = RawFormatValue->ToJson();
3940
check(JsonValue->Type == EJson::Object);
4041
TSharedRef<FJsonObject> UnderlyingObject = JsonValue->AsObject().ToSharedRef();
4142

@@ -95,7 +96,7 @@ void UConfigManager::LoadConfigurationInternal(const FConfigId& ConfigId, URootC
9596

9697
//Convert JSON tree into the raw value tree and feed it to root section value
9798
const TSharedRef<FJsonValue> RootValue = MakeShareable(new FJsonValueObject(JsonObject));
98-
URawFormatValue* RawFormatValue = FJsonRawFormatConverter::ConvertToRawFormat(this, RootValue);
99+
URawFormatValue* RawFormatValue = URawFormatValueObject::FromJson(this, RootValue);
99100
RootConfigValueHolder->GetWrappedValue()->Deserialize(RawFormatValue);
100101

101102
UE_LOG(LogConfigManager, Display, TEXT("Successfully loaded configuration from %s"), *ConfigurationFilePath);
Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
2-
#include "Configuration/RawFileFormat/RawFormatValueObject.h"
32
#include "Configuration/RawFileFormat/RawFormatValueArray.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueBool.h"
44
#include "Configuration/RawFileFormat/RawFormatValueNumber.h"
5+
#include "Configuration/RawFileFormat/RawFormatValueObject.h"
56
#include "Configuration/RawFileFormat/RawFormatValueString.h"
6-
#include "Configuration/RawFileFormat/RawFormatValueBool.h"
77
#include "Dom/JsonObject.h"
88

99
TSharedPtr<FJsonValue> FJsonRawFormatConverter::ConvertToJson(const URawFormatValue* RawFormatValue) {
1010
if (const URawFormatValueNumber* Number = Cast<URawFormatValueNumber>(RawFormatValue)) {
11-
return MakeShareable(new FJsonValueNumber(Number->Value));
11+
return Number->ToJson();
1212
}
1313
if (const URawFormatValueString* String = Cast<URawFormatValueString>(RawFormatValue)) {
14-
return MakeShareable(new FJsonValueString(String->Value));
14+
return String->ToJson();
1515
}
1616
if (const URawFormatValueBool* Boolean = Cast<URawFormatValueBool>(RawFormatValue)) {
17-
return MakeShareable(new FJsonValueBoolean(Boolean->Value));
17+
return Boolean->ToJson();
1818
}
1919
if (const URawFormatValueArray* Array = Cast<URawFormatValueArray>(RawFormatValue)) {
20-
TArray<TSharedPtr<FJsonValue>> OutJsonArray;
21-
for (const URawFormatValue* ChildValue : Array->GetUnderlyingArrayRef()) {
22-
OutJsonArray.Add(ConvertToJson(ChildValue));
23-
}
24-
return MakeShareable(new FJsonValueArray(OutJsonArray));
20+
return Array->ToJson();
2521
}
2622
if (const URawFormatValueObject* Object = Cast<URawFormatValueObject>(RawFormatValue)) {
27-
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
28-
for (TPair<FString, URawFormatValue*> Pair : Object->Values) {
29-
JsonObject->SetField(Pair.Key, ConvertToJson(Pair.Value));
30-
}
31-
return MakeShareable(new FJsonValueObject(JsonObject));
23+
return Object->ToJson();
3224
}
3325
checkf(false, TEXT("Unreachable code"));
3426
return NULL;
@@ -37,39 +29,27 @@ TSharedPtr<FJsonValue> FJsonRawFormatConverter::ConvertToJson(const URawFormatVa
3729
URawFormatValue* FJsonRawFormatConverter::ConvertToRawFormat(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
3830
switch (JsonValue->Type) {
3931
case EJson::Number: {
40-
URawFormatValueNumber* Number = NewObject<URawFormatValueNumber>(Outer);
41-
Number->Value = JsonValue->AsNumber();
42-
return Number;
43-
}
32+
return URawFormatValueNumber::FromJson(Outer, JsonValue);
33+
}
4434
case EJson::String: {
45-
URawFormatValueString* String = NewObject<URawFormatValueString>(Outer);
46-
String->Value = JsonValue->AsString();
47-
return String;
48-
}
35+
return URawFormatValueString::FromJson(Outer, JsonValue);
36+
}
4937
case EJson::Boolean: {
50-
URawFormatValueBool* Boolean = NewObject<URawFormatValueBool>(Outer);
51-
Boolean->Value = JsonValue->AsBool();
52-
return Boolean;
53-
}
38+
return URawFormatValueBool::FromJson(Outer, JsonValue);
39+
}
5440
case EJson::Array: {
55-
URawFormatValueArray* Array = NewObject<URawFormatValueArray>(Outer);
56-
for (const TSharedPtr<FJsonValue>& ChildValue : JsonValue->AsArray()) {
57-
Array->AddValue(ConvertToRawFormat(Array, ChildValue));
58-
}
59-
return Array;
60-
}
41+
return URawFormatValueArray::FromJson(Outer, JsonValue);
42+
}
6143
case EJson::Object: {
62-
URawFormatValueObject* Object = NewObject<URawFormatValueObject>(Outer);
63-
for (const TPair<FString, TSharedPtr<FJsonValue>>& Pair : JsonValue->AsObject()->Values) {
64-
Object->AddValue(Pair.Key, ConvertToRawFormat(Object, Pair.Value));
65-
}
66-
return Object;
67-
}
68-
case EJson::Null:
44+
return URawFormatValueObject::FromJson(Outer, JsonValue);
45+
}
46+
case EJson::Null: {
6947
checkf(false, TEXT("JSON Null value is not supported"));
7048
return NULL;
71-
default:
49+
}
50+
default: {
7251
checkf(false, TEXT("Unreachable code"));
7352
return NULL;
53+
}
7454
}
7555
}

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/Json/JsonRawFormatConverter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
class SML_API FJsonRawFormatConverter {
1313
public:
1414
/** Converts raw format value into the corresponding JSON element */
15+
[[deprecated("Use ToJson on the raw format value directly")]]
1516
static TSharedPtr<FJsonValue> ConvertToJson(const URawFormatValue* RawFormatValue);
1617

1718
/** Converts JSON element into the raw format value, using specified object as Outer for raw value */
19+
[[deprecated("Use FromJson on the raw format class directly")]]
1820
static URawFormatValue* ConvertToRawFormat(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
1921
};

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@
66
UCLASS(Abstract, BlueprintType)
77
class SML_API URawFormatValue : public UObject {
88
GENERATED_BODY()
9+
10+
public:
11+
/** Converts this object into a JSON value */
12+
virtual TSharedPtr<FJsonValue> ToJson() const {
13+
checkf(false, TEXT("ToJson not implemented"));
14+
return NULL;
15+
}
916
};

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueArray.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#pragma once
2+
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
24
#include "Configuration/RawFileFormat/RawFormatValue.h"
35
#include "Configuration/RawFileFormat/RawFormatValueNumber.h"
46
#include "Configuration/RawFileFormat/RawFormatValueString.h"
57
#include "Templates/SubclassOf.h"
8+
#include "SatisfactoryModLoader.h"
69
#include "RawFormatValueArray.generated.h"
710

811
/** Describes raw array value */
@@ -85,6 +88,12 @@ class SML_API URawFormatValueArray : public URawFormatValue {
8588

8689
/** Returns underlying array object reference. Reference is valid as long as this object is valid. */
8790
FORCEINLINE const TArray<URawFormatValue*>& GetUnderlyingArrayRef() const { return Values; }
91+
92+
virtual TSharedPtr<FJsonValue> ToJson() const override;
93+
94+
/** Creates a new URawFormatValueArray from the given JSON value */
95+
static URawFormatValueArray* FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
96+
8897
private:
8998
/** Private to ensure that added objects have valid outer */
9099
UPROPERTY()
@@ -108,3 +117,22 @@ FORCEINLINE void URawFormatValueArray::AddValue(URawFormatValue* ValueClass) {
108117
checkf(ValueClass && ValueClass->GetOuter() == this, TEXT("Cannot add URawFormatValue residuing inside another outer object (should be URawFormatValueArray)"));
109118
Values.Add(ValueClass);
110119
}
120+
121+
FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueArray::ToJson() const {
122+
TArray<TSharedPtr<FJsonValue>> OutJsonArray;
123+
for (const URawFormatValue* ChildValue : Values) {
124+
OutJsonArray.Add(ChildValue->ToJson());
125+
}
126+
return MakeShareable(new FJsonValueArray(OutJsonArray));
127+
}
128+
129+
FORCEINLINE URawFormatValueArray* URawFormatValueArray::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
130+
URawFormatValueArray* Array = NewObject<URawFormatValueArray>(Outer);
131+
for (const TSharedPtr<FJsonValue>& ChildValue : JsonValue->AsArray()) {
132+
// TODO: Base child property deserialization on the child config property.
133+
UE_LOG(LogSatisfactoryModLoader, Warning, TEXT("Deserializing JSON array value for property with fallback implementation."));
134+
#pragma warning(suppress : 4996)
135+
Array->AddValue(FJsonRawFormatConverter::ConvertToRawFormat(Array, ChildValue));
136+
}
137+
return Array;
138+
}

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueBool.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,19 @@ class SML_API URawFormatValueBool : public URawFormatValue {
1010
/** Actual boolean value stored */
1111
UPROPERTY(BlueprintReadWrite)
1212
bool Value;
13+
14+
virtual TSharedPtr<FJsonValue> ToJson() const override;
15+
16+
/** Creates a new URawFormatValueBool from the given JSON value */
17+
static URawFormatValueBool* FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
1318
};
19+
20+
FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueBool::ToJson() const {
21+
return MakeShareable(new FJsonValueBoolean(Value));
22+
}
23+
24+
FORCEINLINE URawFormatValueBool* URawFormatValueBool::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
25+
URawFormatValueBool* Boolean = NewObject<URawFormatValueBool>(Outer);
26+
Boolean->Value = JsonValue->AsBool();
27+
return Boolean;
28+
}

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueNumber.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class SML_API URawFormatValueNumber : public URawFormatValue {
2626
/** Updates raw value to specified 32-bit signed integer */
2727
UFUNCTION(BlueprintCallable)
2828
void SetValueInt(int32 NewValue);
29+
30+
virtual TSharedPtr<FJsonValue> ToJson() const override;
31+
32+
/** Creates a new URawFormatValueNumber from the given JSON value */
33+
static URawFormatValueNumber* FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
2934
};
3035

3136
FORCEINLINE void URawFormatValueNumber::SetValueFloat(float NewValue) {
@@ -35,3 +40,13 @@ FORCEINLINE void URawFormatValueNumber::SetValueFloat(float NewValue) {
3540
FORCEINLINE void URawFormatValueNumber::SetValueInt(int32 NewValue) {
3641
this->Value = NewValue;
3742
}
43+
44+
FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueNumber::ToJson() const {
45+
return MakeShareable(new FJsonValueNumber(Value));
46+
}
47+
48+
FORCEINLINE URawFormatValueNumber* URawFormatValueNumber::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
49+
URawFormatValueNumber* Number = NewObject<URawFormatValueNumber>(Outer);
50+
Number->Value = JsonValue->AsNumber();
51+
return Number;
52+
}

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueObject.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22
#include "Templates/SubclassOf.h"
3+
#include "Configuration/ConfigProperty.h"
4+
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
35
#include "Configuration/RawFileFormat/RawFormatValueArray.h"
6+
#include "SatisfactoryModLoader.h"
47
#include "RawFormatValueObject.generated.h"
58

69
/** Describes raw map (also known as object) value */
@@ -87,6 +90,11 @@ class SML_API URawFormatValueObject : public URawFormatValue {
8790
FORCEINLINE T* AddNewValue(const FString& Key) {
8891
return Cast<T>(AddNewValue(Key, T::StaticClass()));
8992
}
93+
94+
virtual TSharedPtr<FJsonValue> ToJson() const override;
95+
96+
/** Creates a new URawFormatValueObject from the given JSON value */
97+
static URawFormatValueObject* FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
9098
};
9199

92100
FORCEINLINE void URawFormatValueObject::SetString(const FString& Key, const FString& Value) {
@@ -106,3 +114,22 @@ FORCEINLINE void URawFormatValueObject::AddValue(const FString& Key, URawFormatV
106114
checkf(ValueClass && ValueClass->GetOuter() == this, TEXT("Cannot add URawFormatValue residuing inside another outer object (should be URawFormatValueArray)"));
107115
Values.Add(Key, ValueClass);
108116
};
117+
118+
FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueObject::ToJson() const {
119+
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
120+
for (TPair<FString, URawFormatValue*> Pair : Values) {
121+
JsonObject->SetField(Pair.Key, Pair.Value->ToJson());
122+
}
123+
return MakeShareable(new FJsonValueObject(JsonObject));
124+
}
125+
126+
FORCEINLINE URawFormatValueObject* URawFormatValueObject::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
127+
URawFormatValueObject* Object = NewObject<URawFormatValueObject>(Outer);
128+
for (const TPair<FString, TSharedPtr<FJsonValue>>& Pair : JsonValue->AsObject()->Values) {
129+
// TODO: Base child property deserialization on the child config property.
130+
UE_LOG(LogSatisfactoryModLoader, Warning, TEXT("Deserializing JSON value for property %s with fallback implementation."), *Pair.Key);
131+
#pragma warning(suppress : 4996)
132+
Object->AddValue(Pair.Key, FJsonRawFormatConverter::ConvertToRawFormat(Object, Pair.Value));
133+
}
134+
return Object;
135+
}

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueString.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,18 @@ class SML_API URawFormatValueString : public URawFormatValue {
1010
/** Value of this raw value as string */
1111
UPROPERTY(BlueprintReadWrite)
1212
FString Value;
13+
14+
virtual TSharedPtr<FJsonValue> ToJson() const override;
15+
16+
static URawFormatValueString* FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue);
1317
};
18+
19+
FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueString::ToJson() const {
20+
return MakeShareable(new FJsonValueString(Value));
21+
}
22+
23+
FORCEINLINE URawFormatValueString* URawFormatValueString::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
24+
URawFormatValueString* String = NewObject<URawFormatValueString>(Outer);
25+
String->Value = JsonValue->AsString();
26+
return String;
27+
}

0 commit comments

Comments
 (0)