I am using FJsonObjectConverter for json deserialization. The result for the first deserialization is correct, but I get an incorrect result for subsequent operations. Maybe I have encountered a situation that belongs to the nature of C++ that I am not familiar with. I am sharing the code and debug results. How do you think I can fix the problem? Thanks.
Debug Result :
As you can see in the json data the “character” property is null but the debugger shows it as previous deserialized data.
The function works just fine with a simple struct for me.
I’m giving it json with a random value every second and it produces expected result every time.
What does your struct look like ? Especially that character part ?
Is it an object reference, or a nested struct ?
How about other struct members, do they update correctly on subsequent operations ?
Ok so I have experimented a bit and as I suspected, null is not compatible as a nested struct value. So as a result when the string is parsed, the character:null part is interpreted as an object type which does not match the character property type, and it is skipped. And since blueprint graphs seemingly retain persistent memory for pins, it retains its previous value.
I can think of two possible solutions :
Avoid having null as a possible json input for structs. Null is for pointers, and the only pointers in blueprints are for objects. Nested structs are not pointers and cannot be null. Replace null with whatever the default empty struct looks like. For example I see a companions field which seems to be an array. The corresponding empty json would be :
"character" : { "companions": [] }
If you are lucky maybe simply replacing "character":null with "character":{} could be enough for the underlying parser to reset the struct. That’s a pure guess though.
Alternatively, modify your function a bit to reset the struct memory before each parsing.
Add this bit just before calling Inner_JsonStringToStruct :
Firstly thanks a lot for your effort. I tried your recommendations but thats not solved the problem. Maybe i didnt apply the solution well idk. Finally i found a way. Honestly i dont know this is the perfect solution ot not, but its working…
I carried the JsonStringToStruct function to a ActorComponent and then i created and destroyed it on the deserialization operation. Inner blueprint view looks like that :