visual-c++tinyxml2

Bool serialization with tinyxml2


I have set the following line, just before reading / writing a XML:

tinyxml2::XMLUtil::SetBoolSerialization("true", "false");
// Create a xml with bool attributes etc and save.
tinyxml2::XMLUtil::SetBoolSerialization("1", "0");

I know that the above values are the defaults, but elsewhere in my code I do set it to 1 / 0.

But my question is why is the above not being honoured? I specifically write this code, then create / create / save and then set back to 1 / 0.

My final xml file still have 1 / 0?

I know that I could stop trying to use this SetBoolSerialization approach and do something like:

eventDateTime->SetAttribute("AllDayEvent", event.bEventAllDay ? "true" : "false");

But I thought this function was meant for this.

The function is defined like this:

void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse)
{
    static const char* defTrue  = "true";
    static const char* defFalse = "false";

    writeBoolTrue = (writeTrue) ? writeTrue : defTrue;
    writeBoolFalse = (writeFalse) ? writeFalse : defFalse;
}

I think the problem is that because I am making the two calls in the same function, that the latter is becomg the global and overrides.


Solution

  • It was my fault! I forgot that the Boolean variables being written to XML were of type BOOL which is integers, and hence it wrote the numeric values.

    As soon as I caste those variable with static_cast>bool>(...) then they serialize as expected.