Here is a simple example about how to pass information by json from js to wasm (C++ generated).
We will pass enum value with json in the following project.
C++
enum class CType{
T_A,
T_B,
None
};
void UModule::SetSlotType(std::string slotTypeJson)
{
CType type = CType::None;
nlohmann::json jsonObj = nlohmann::json::parse( slotTypeJson );
if( jsonObj.contains("slotType") )
{
std::string str = jsonObj["slotType"];
if( str == "T_A" )
{
type = CType::T_A;
}
else
{
// ...
}
}
LOG( INFO, "slotTypeJson: ", slotTypeJson, "\n, type: ", int( type ) );
}
JS:
const data = { slotType: "T_A" }; // T_A, T_B, None
const jsonStr = JSON.stringify(data);
moduleObj.SetSlotType( jsonStr )
If you want to write a whole CPlusPlus + js web app, you can refer to our old project: https://www.weiy.city/2021/12/export-cplusplus-algorithm-to-javascript-code/
nlohmann::json
can be import from json.hpp, the project: https://github.com/nlohmann/json