Issue #22 invalid

Empty arrays or objects

Lectem
created an issue

Hi, the parser won't add the tokens of arrays with 2 elements or less, nor will it add the tokens of empty objects. In case of an empty object that is a value, the next pair will even be omitted! an example json file :

{ "height":10, "layers":[ { "data":[6,6], "height":10, "name":"Calque de Tile 1", "opacity":1, "type":"tilelayer", "visible":true, "width":10, "x":0, "y":0 }], "orientation":"orthogonal", "properties": {

},

"tileheight":32, "tilesets":[ { "firstgid":1, "image":"..\/images\/tiles.png", "imageheight":64, "imagewidth":160, "margin":0, "name":"Tiles", "properties":{}, "spacing":0, "tileheight":32, "tilewidth":32 }], "tilewidth":32, "version":1, "width":10 }

the "data" array won't be parsed correctly (while if we add a space after the coma it will work fine, or if we put 3 members without spaces such as [5,5,5]) and the "spacing":0 pair won't have any tokens. The parsing will fail if a space is added between the brackets ( "properties":{ }, intead of "properties":{}, )

Is there a way to fix it? Thanks

Comments (3)

  1. Serge Zaitsev repo owner

    I've tried adding a test to confirm your issue. Here's the code:

    int test_issue_22() {
        int i;
        int r;
        jsmn_parser p;
        jsmntok_t tokens[128];
        const char *js;
    
        js = "{ \"height\":10, \"layers\":[ { \"data\":[6,6], \"height\":10, "
            "\"name\":\"Calque de Tile 1\", \"opacity\":1, \"type\":\"tilelayer\", "
            "\"visible\":true, \"width\":10, \"x\":0, \"y\":0 }], "
            "\"orientation\":\"orthogonal\", \"properties\": { }, \"tileheight\":32, "
            "\"tilesets\":[ { \"firstgid\":1, \"image\":\"..\\/images\\/tiles.png\", "
            "\"imageheight\":64, \"imagewidth\":160, \"margin\":0, \"name\":\"Tiles\", "
            "\"properties\":{}, \"spacing\":0, \"tileheight\":32, \"tilewidth\":32 }], "
            "\"tilewidth\":32, \"version\":1, \"width\":10 }";
        jsmn_init(&p);
        r = jsmn_parse(&p, js, tokens, 128);
        check(r == JSMN_SUCCESS);
        for (i = 1; tokens[i].end < tokens[0].end; i++) {
            if (tokens[i].type == JSMN_STRING || tokens[i].type == JSMN_PRIMITIVE) {
                printf("%.*s\n", tokens[i].end - tokens[i].start, js + tokens[i].start);
            } else if (tokens[i].type == JSMN_ARRAY) {
                printf("[%d elems]\n", tokens[i].size);
            } else if (tokens[i].type == JSMN_OBJECT) {
                printf("{%d elems}\n", tokens[i].size);
            } else {
                TOKEN_PRINT(tokens[i]);
            }
        }
        return 0;
    }
    
  2. Serge Zaitsev repo owner

    ...And here's the output:

    height
    10
    layers
    [1 elems]
    {18 elems}
    data
    [2 elems]
    6
    6
    height
    10
    name
    Calque de Tile 1
    opacity
    1
    type
    tilelayer
    visible
    true
    width
    10
    x
    0
    y
    0
    orientation
    orthogonal
    properties
    {0 elems}
    tileheight
    32
    tilesets
    [1 elems]
    {20 elems}
    firstgid
    1
    image
    ..\/images\/tiles.png
    imageheight
    64
    imagewidth
    160
    margin
    0
    name
    Tiles
    properties
    {0 elems}
    spacing
    0
    tileheight
    32
    tilewidth
    32
    tilewidth
    32
    version
    1
    width
    10
    

    As you can see, "data" attribute is parsed as array of two elements, 6 and 6; properties is parsed as empty object. To my mind, the code above works as expected, and I can't reproduce the issue. Could you please give me some code that proves that parsing of empty arrays or objects is not working? Thanks.

  3. Log in to comment