JimmyCushnie / JECS

Jimmy's Epic Config System

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Saving dictionaries can unintentionally generate invalid data files

JimmyCushnie opened this issue · comments

void Test()
{
    var dicc = new Dictionary<int, Vector3>()
    {
        [22] = new Vector3(-12, 323, 432.43f),
        [55] = new Vector3(-1, 0, 100),
        [-100] = new Vector3(Mathf.PI, Mathf.PI, Mathf.PI),
    };

    var file = new DataFile("test", autoSave: true);
    file.Set("dicc boi", dicc);
}

That code generates this file:

dicc boi:
    22:
        x: -12
        y: 323
        z: 432.43
    55:
        x: -1
        y: 0
        z: 100
    -100:
        x: 3.141593
        y: 3.141593
        z: 3.141593

Notice the key called "-100". Keys are not allowed to begin with -, as that character is reserved for list nodes. Trying to read the file later will throw errors. This can also happen when using DataFile.SaveAsDictionary().

The solution is probably to allow - in key nodes, and determine what is a list node by the absence of :.

Alternatively you could allow the use of double quotes on keys, which would be ignored when deserializing. So "-100" would be deserialized as -100, and not confused with a list as it doesn't begin with -.

That's a really good idea. Perhaps dictionary keys should always have quotes around them, to prevent any unforeseen issues like this cropping up later.

Strings can already contain quotes. If you want to serialize "hello", it is stored in the file as ""hello"".

Fixed in 2487cce and will be in the next release. Dictionaries, when serialized with their keys as children, will always surround the keys in quotes.

In 4934481 I fixed this in a much nicer way. Dictionaries now check if their key is a valid SUCC key, and if not, they serialize as an array of KeyValuePair.