Serializing a Unreal Engine USTRUCT to JSON or deserializing JSON back to a USTRUCT is very painless thanks to the build-in FJsonObjectConverter class. The process is performed recursively without any extra effort as well as having the ability to include/exclude specific properties from the serialization. Below we’ll describe the requirements for using
FJsonObjectConverter as well as a practical example.
FJsonObjectConverter you will need to ensure that both
JsonUtilities are included within
PublicDependencyModuleNames in your projects
You’ll also need to include the
JsonObjectConverter.h header in source files where you make calls to
The examples below will use this following struct as a guide:
To serialize a struct to a JSON payload use FJsonObjectConverter::UStructToJsonObjectString. This method will operate recursively, so if you have anything liked nested structs, arrays inside of arrays inside of other structs it will operate as expected. Note that the
PropertyToIgnore property above is marked as
Transient, this will prevent the property from being serialized. For example:
Will result in the following JSON payload being written to the
To deserialize a JSON payload back to a struct use FJsonObjectConverter::JsonObjectStringToUStruct. After this above serialization example this method works exactly how you would expect it to. For example:
You will have noticed above that we are passing two 0’s as the final arguments to both methods above. These 0’s are the
SkipFlags respectively. These can be used to provide more fine-graned control over what properties are either included (whitelisted) or skipped (blacklisted).
Above we used
Transient to skip a property above without having to pass this value into
SkipFlags. This is because
Transient by default when no
SkipFlags are specified (a very sane default).