Game Version number: Latest stable
Modded?: No
Issue Description: A vector value with integers instead of floats fails to load. [1,2,3] fails, while [1.0, 2.0, 3.0] works. IMO, the vector value should be loaded even if it would be just [1,2,3]. It's a proper json if checked with a validator.
Description
Details
- Severity
- Major
- Resolution
- Open
- Reproducibility
- Always
- Operating System
- Windows 11 x64
- Category
- General
- Create a 'JsonError' mod in WB
- Create a file <moddir>\JsonError\scripts\Game\GameCore\Modded\SDRC_BaseGameMode.c
- Copy the code in Additional Information
- In WB, Create a world with GameMode_Plain:
- Compile and start the game.
- A simple file JsonError.json is created under profile\JsonError\
- File content will be: "{"value":1,"name":"dummy","vecval":[1.0,2.0,3.0]}"
- Rerun the code and the file values are loaded properly
- Change the file content to: "{"value":1,"name":"dummy","vecval":[2.0,3.0,4.0]}"
- Rerun the code and the file values are loaded properly
- Change the file content to: "{"value":1,"name":"dummy","vecval":[2.0,3,4.0]}". NOTE: 3.0 is changed to 3
- The file is proper json in case it's tested with a validator.
- Rerun the code and the file fails to load. The default values are used.
IMO, the vector value should be loaded even if it would be just [1,2,3]. With other float variables this works properly.
modded class SCR_BaseGameMode
{
private ref SDRC_ConfJsonApi m_ConfJsonApi = new SDRC_ConfJsonApi();
//------------------------------------------------------------------------------------------------
override void OnGameStart() { super.OnGameStart(); m_ConfJsonApi.Load(); Print(m_ConfJsonApi.conf.vecval); }
};
class SDRC_Conf
{
int value = 1;
string name = "dummy";
vector vecval = "1 2 3";
}
//------------------------------------------------------------------------------------------------
class SDRC_ConfJsonApi : SDRC_JsonApi
{
const string DC_CONF_FILE = "JsonError.json";
ref SDRC_Conf conf = new SDRC_Conf();
//------------------------------------------------------------------------------------------------
void Load()
{
SCR_JsonLoadContext loadContext = LoadConfig(DC_CONF_FILE); if (!loadContext) { SetDefaults(); Save(""); return; } loadContext.ReadValue("", conf);
}
//------------------------------------------------------------------------------------------------
void Save(string data)
{
SCR_JsonSaveContext saveContext = SaveConfigOpen(DC_CONF_FILE); saveContext.WriteValue("", conf); SaveConfigClose(saveContext);
}
//------------------------------------------------------------------------------------------------
void SetDefaults()
{
}
}
//Helpers/SDRC_JsonApi.c
For readable jsons, use https://jsonformatter.org
NOTE: View .json in Notepad++ - press Ctrl+Alt+Shift+J
// NOTE: Format .json in Notepad++ - press Ctrl+Alt+Shift+M
//------------------------------------------------------------------------------------------------
class SDRC_JsonApi : JsonApiStruct
{
private string m_FileName = "";
//------------------------------------------------------------------------------------------------
SCR_JsonLoadContext LoadConfig(string fileName)
{
SetFileName(fileName); SCR_JsonLoadContext loadContext = new SCR_JsonLoadContext(); bool success = loadContext.LoadFromFile(m_FileName); if (!success) { return null; } return loadContext;
}
//------------------------------------------------------------------------------------------------
SCR_JsonSaveContext SaveConfigOpen(string fileName)
{
SetFileName(fileName); SCR_JsonSaveContext saveContext = new SCR_JsonSaveContext(); return saveContext;
}
//------------------------------------------------------------------------------------------------
void SaveConfigClose(SCR_JsonSaveContext saveContext)
{
string dataString = saveContext.ExportToString(); ExpandFromRAW(dataString); if (!saveContext.SaveToFile(m_FileName)) { Print("[SDRC_JsonApi] Config save failed to: " + m_FileName, LogLevel.ERROR); } else { Print("[SDRC_JsonApi] Config saved to: " + m_FileName, LogLevel.DEBUG); }
}
//------------------------------------------------------------------------------------------------
override void OnError(int errorCode)
{
// errorCode is EJsonApiError // Event called when pending store operation is finished - callback when error happened Print("[SDRC_JsonApi] Error loading config. " + SCR_Enum.GetEnumName(EJsonApiError, errorCode), LogLevel.ERROR);
}
//------------------------------------------------------------------------------------------------
void SetFileName(string fileName)
{
string path = ""; string directory = "JsonError"; path = "$profile:/" + directory + "/"; if (!FileIO.MakeDirectory(path)) { Print("[SDRC_JsonApi] Could not create path: " + path, LogLevel.ERROR); } m_FileName = path + fileName;
}
}