Page MenuHomeFeedback Tracker

JsonFileLoader return values
Assigned, UrgentPublic

Description

Both JsonFileLoader.JsonLoadFile and JsonFileLoader.JsonLoadData should return a boolean to state success. Without making our own imitation of this class, there's no way to tell if an error occurred via script. Changing this logic does not effect current usage in mods using these methods.

Details

Severity
Tweak
Resolution
Open
Reproducibility
N/A
Operating System
Windows 11 x64
Category
General

Event Timeline

make the json serializer work out of a self-contained function. as of right now this fails / crashes

class MyClass
{
    float Maybe;

    void Load(string json)
    {
        JsonSerializer cereal = new JsonSerializer();
        string error;
        cereal.ReadFromString(this, json, error};
        // etc...
     }
}
lava76 added a subscriber: lava76.Jun 27 2023, 8:19 PM
Geez changed the task status from New to Assigned.Jun 28 2023, 11:50 AM

Bumping this with the expected changes.

class JsonFileLoader<Class T>
{
	static bool JsonLoadFile(string filename, out T data)
	{
		if (!FileExist(filename))
		{
			return false;
		}

		string file_content, line_content, error;
		FileHandle handle = OpenFile(filename, FileMode.READ);
		if (handle == 0)
		{
			return false;
		}

		while (ReadFile(handle, line_content, 128) != 0)
		{
			file_content += line_content;
		}

		CloseFile(handle);

		if (!m_Serializer)
		{
			m_Serializer = new JsonSerializer();
		}

		if (!m_Serializer.ReadFromString(data, file_content, error))
		{
			Error(error);
			return false;
		}

		return true;
	}

	static bool JsonLoadData(string string_data, out T data)
	{
		string error;
		if (!m_Serializer)
		{
			m_Serializer = new JsonSerializer();
		}

		if (!m_Serializer.ReadFromString(data, string_data, error))
		{
			Error(error);
			return false;
		}

		return true;
	}
}