Unreal Engine 4 provides some great JSON utility structures and functions for parsing and creating JSON. It’s a format that’s used a lot for data storage and transmission, especially with web requests.
This post is the first part in a short series introducing how to use Json in UE4. I’m not aiming to introduce what Json is – if you’re unfamiliar, there’s plenty of resources online.
All the functionality for JSON in UE4 is included in two modules: Json and JsonUtilities, which are both in the Source/Runtime directory. To be able to use these in your project, simply add Json and JsonUtilities to your project’s PrivateDependencyModuleNames (located in your project’s build rules file – normally called Project.Build.cs)
There are two ways to read and write Json in UE4. Either you can read the data and build the Json yourself, or you can use the built-in utility functions in JsonUtilities that use UE4’s reflection system to read from the UPROPERTYs in classes or structs.
This post will focus on the former – just using the simple Json functions to build some simple Json strings.
Json supports a number of types – bool, string, number. These are generally stored as a name with an associated value. Each of these name-value pairs are stored inside a Json object, which is essentially what is between the curly braces in a Json string.
These Json object is represented with the FJsonObject class in UE4, which is declared as such:
TSharedPtr JsonObject = MakeShareable(new FJsonObject);
Once you have created the root Json object, it’s as simple as adding the name-value pairs that you want. We’ll build a really simple set of properties that might represent a weapon, along with how much damage it does
JsonObject->SetStringField("Name", "Super Sword");
Note that you can call SetObjectField to add a new object type as well.
Once you build your FJsonObject, you’ll need to get a string representation of that to do something with – save to a file, for instance. This can be achieved using a TJsonWriter to write the Json, and the FJsonSerializer class to serialize the JsonObject itself.
The simplest is the TJsonWriterFactory, shown below:
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
This will serialize the Json to OutputString which you are then free to do whatever you want with – pass to a Http request, save to a file, write to a database etc.
The TJsonWriterFactory is templated to accept a “policy” which determines the way the Json is printed – for instance, “pretty” so its correctly formatted, or “condensed” so all the whitespace is removed. The names of these policies are TCondensedJsonPrintPolicy and TPrettyJsonPrintPolicy.
To use these, you need to pass the appropriate policy when creating the TJsonWriter, as such:
TSharedRef< TJsonWriter<TCHAR, TPrettyJsonPrintPolicy > > JsonWriter = TJsonWriterFactory<tchar, tprettyjsonprintpolicy="">::Create(&OutputString);
That looks a bit long-winded, but you can easily typedef these yourself to make it more readable. This line of code tyedefs a TJsonWriter with the pretty print policy as FPrettyJsonWriter in place of the Writer we declared earlier:
typedef TJsonWriter<tchar, tprettyjsonprintpolicy=""> FPrettyJsonWriter;
And can then be used as such:
TSharedRef< FPrettyJsonStringWriter > Writer = FPrettyJsonStringWriterFactory::Create( &OutputString );
When put together, this code would produce the following Json:
"Name" : "Super Sword",
"Damage: " 15,
"Weight" : 3
That’s all for how to create and serialize Json in UE4. In the next part I’ll look at how to deserialize Json.