JimmyCushnie / JECS

Jimmy's Epic Config System
Do What The F*ck You Want To Public License
154 stars 16 forks source link

Saving dictionaries can unintentionally generate invalid data files #5

Closed JimmyCushnie closed 5 years ago

JimmyCushnie commented 5 years ago
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 :.

pipe01 commented 5 years ago

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 -.

JimmyCushnie commented 5 years ago

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.

pipe01 commented 5 years ago

That works. It would also be great if you could escape the quotes with \", so that the string can contain quotes.

JimmyCushnie commented 5 years ago

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

JimmyCushnie commented 5 years ago

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.

JimmyCushnie commented 5 years ago

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.