Reading from file causes crash

I'm trying to read a .json file into my program.

I'm using the rapidjson library and following this tutorial:
http://rapidjson.org/md_doc_tutorial.html

The document object(levelsDocument) is defined as a global variable and the following function sets the value to the result of the ParseStream function. The issue is that the document object is always empty.

I have also tried doing .HasMember("level1") on the document, but then it crashes with the following error:

Assertion failed: IsObject(), file source_files/../lib/rapidjson/document.h, line 885
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.



This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool get_levels()
{
    FILE* fp = fopen("D:/Programming/Orbit/source_files/Levels/levels.json", "rb"); // non-Windows use "r"
    if (!fp) {
        printf( "File doesn't exist\n");
        return false;
    }
    char readBuffer[65536];
    FileReadStream is(fp, readBuffer, sizeof(readBuffer));
    levelsDocument.ParseStream(is);
    fclose(fp);
    if (!levelsDocument.IsObject())
    {
        printf( "Could not load levels file");
        return false;
    }
    return true;
}


And this is the json:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    "level1": {
        "background": {
            "image": "resources/space1.bmp"
        },
        "playerstart": {
            "x": 0,
            "y": 0,
        },
        "objects": {
            "earth": {
                "type": "planet",
                "x": 0,
                "y": 0,
                "scale": 1,
                "gravity": 9.8,
                "image": "resources/earth.bmp"
            },
            "moon": {
                "type": "planet",
                "x": 500,
                "y": 0,
                "scale": 1,
                "gravity": 3,
                "image": "resources/moon.bmp"
            }
        }
    }
}




Any ideas? Let me know if you need more information.
Note: I'm on Windows 8.1 and use Mingw. I use a makefile which I build using the mingw32-make command.
Ok, I feel dumb. It looks like my json is more than 65536 (bytes? no idea). If I reduce the size of the file it works fine. Increasing the size of the buffer doesn't seem to have any effect though strangely enough
The error is here:
6
7
8
9
        "playerstart": {
            "x": 0,
            "y": 0,
        },
There's a stray comma at the end of line 8. Because the next character is the closing brace at line 9, no comma is required.

It should look like this:
6
7
8
9
        "playerstart": {
            "x": 0,
            "y": 0
        },


Regarding the file size, your file "levels.json" contains under 700 characters, certainly far less than the buffer size. In any case a smaller buffer will also work, even if it is much smaller than the file size.
Last edited on
Topic archived. No new replies allowed.