You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using in Unreal Engine - handling custom types conversion #495
Hi. I come from a JavaScript world where working with JSON is total breeze. We are currently working on a game using Unreal Engine 4 and we need to develop custom save system using JSON format so it can be read using Javascript somewhere else.
I kinda like approach of this library as its syntax is similar to what I am used to from JS. Sadly me as total rookie to C++ I am struggling with this a lot. I would like to make some arbitrary conversions from Unreal types to ones supported by this library.
For example here is a header file for FString type. Somewhere else I've found what I need to do for conversion to std::string so I've tried to add following
However this is failing compilation with this error. I am totally clueless what that means. Can you please help?
error C3211: 'nlohmann::adl_serializer<FString,void>': explicit specialization is using partial specialization syntax, use template <> instead
note: see declaration of 'nlohmann::adl_serializer<FString,void>'
I haven't tried it, but it should be sufficient to define a to_json function in the namespace of FString. In the header, I did not find a namespace, so it should be sufficient to define
error C2908: explicit specialization; 'nlohmann::adl_serializer<T,void>' has already been instantiated
with
[
T=FString
]
error C2766: explicit specialization; 'nlohmann::adl_serializer<T,void>' has already been defined
with
[
T=FString
]
For types that belong to you, the free function is the best approach usually, however for those which are not in your namespaces that' s not really intended.
I think that error means that you should put this piece of code at the very top of your file, because adl_serializer is instantiated when you do this:
json result;
FString test = TEXT("MyTest");
result["key"] = test;
std::cout << result << std::endl;
The first change makes the conversion to a std::string explicit. The second string asks for a serialization without relying on the behavior of the UE_LOG function.
Well first change did not help and sadly it's not possible to use std::cout as there is no way to read output from it. The whole thing is executed from within Unreal Editor and there is no access to regular output.
Anyway, I will try to figure that out on side of UE. Thanks for help!
Activity
nlohmann commentedon Mar 11, 2017
I haven't tried it, but it should be sufficient to define a
to_jsonfunction in the namespace ofFString. In the header, I did not find a namespace, so it should be sufficient to define(I just copied the
j = TCHAR_TO_UTF8(*value);from your example).Then you should be able to execute for an
FStringobjectf:json j = f;.theodelrieu commentedon Mar 11, 2017
What you need to do is to remove the
typename Tand it will work:danielkcz commentedon Mar 11, 2017
@nlohmann I don't understand how that could work. How it can know to use global
to_jsonfunction for FString?@theodelrieu Tried that and now I am getting...
theodelrieu commentedon Mar 11, 2017
For types that belong to you, the free function is the best approach usually, however for those which are not in your namespaces that' s not really intended.
I think that error means that you should put this piece of code at the very top of your file, because
adl_serializeris instantiated when you do this:my_type mt; // adl_serializer<my_type> gets instantiated here json j = mt;If you don't put your specialization before , you get that error message.
danielkcz commentedon Mar 11, 2017
Ok, looks like that this actually works...
However when I am trying to dump that JSON now, I am getting bunch of japanese characters :)
I am not entirely sure I got that conversion to
std::stringright. Here is a source of the information: https://wiki.unrealengine.com/String_Conversions:_FString_to_FName,_FString_to_Int32,_Float_to_FStringAlso I had to use
c_str()on the dump result as the UE_LOG macro does not recognizestring_t. That last argument of UE_LOG is VarArgstheodelrieu commentedon Mar 11, 2017
Since FString is defined in the global namespaces, this is much more easier indeed.
Edit: about the conversion , I never used unreal, I don't think I can be of any help here...
nlohmann commentedon Mar 11, 2017
@FredyC I am also not an expert. To make sure, you could change the function to
and then try:
The first change makes the conversion to a
std::stringexplicit. The second string asks for a serialization without relying on the behavior of theUE_LOGfunction.danielkcz commentedon Mar 11, 2017
Well first change did not help and sadly it's not possible to use
std::coutas there is no way to read output from it. The whole thing is executed from within Unreal Editor and there is no access to regular output.Anyway, I will try to figure that out on side of UE. Thanks for help!
nlohmann commentedon Mar 11, 2017
You're welcome. I shall close this issue. But it would be great if you could share your further experience with the library and UE.
rrafis commentedon Jan 17, 2019
@FredyC, have you found any solution with this?
danielkcz commentedon Jan 17, 2019
@rrafis Sorry, I don't remember really, I've left Unreal Engine world shortly after this issue.
kigbariom commentedon Sep 17, 2019
UE_LOG_UTF8 will fix that
Suppress flawfinder CWE-126 warning in test code
Suppress flawfinder CWE-126 warning in test code
Suppress flawfinder CWE-126 warning in test code