Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Parse Out of Range Error #1574

Closed
jeffmorr opened this issue Apr 20, 2019 · 9 comments
Closed

JSON Parse Out of Range Error #1574

jeffmorr opened this issue Apr 20, 2019 · 9 comments
Labels
kind: question state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@jeffmorr
Copy link

Hi all,

I am getting an out of range error when I'm trying to parse my json file and I'm not sure why.

My json file

[
    {
        "TableType" : "Random",
        "TableName" : "CurrencyTable",
        "TableEntryCollection" : [
            {
                "EntryType" : "Item",
                "EntryName" : "platinum",
                "MinDrops" : 1,
                "MaxDrops" : 10,
                "SelectionWeight" : 0.25
            },
            {
                "EntryType" : "Item",
                "EntryName" : "gold",
                "MinDrops" : 1,
                "MaxDrops" : 100,
                "SelectionWeight" : 0.50
            },
            {
                "EntryType" : "Item",
                "EntryName" : "silver",
                "MinDrops" : 1,
                "MaxDrops" : 500,
                "SelectionWeight" : 0.75
            },
            {
                "EntryType" : "Item",
                "EntryName" : "copper",
                "MinDrops" : 1,
                "MaxDrops" : 1000,
                "SelectionWeight" : 1.0
            }
        ]
    },
    {
        "TableType" : "UniqueRandom",
        "TableName" : "EquipmentTable",
        "TableEntryCollection" : [
            {
                "EntryType" : "Item",
                "EntryName" : "IronSword",
                "MinDrops" : 1,
                "MaxDrops" : 2,
                "SelectionWeight" : 1
            },
            {
                "EntryType" : "Item",
                "EntryName" : "WoodenShield",
                "MinDrops" : 1,
                "MaxDrops" : 4,
                "SelectionWeight" : 2
            },
            {
                "EntryType" : "Item",
                "EntryName" : "GoldRing",
                "MinDrops" : 1,
                "MaxDrops" : 1,
                "SelectionWeight" : 3
            },
            {
                "EntryType" : "Item",
                "EntryName" : "SilverRing",
                "MinDrops" : 1,
                "MaxDrops" : 3,
                "SelectionWeight" : 4
            }
        ]
    },
    {
        "TableType" : "Random",
        "TableName" : "TreasureChestTable",
        "TableEntryCollection" : [
            {
                "EntryType" : "Table",
                "EntryName" : "CurrencyTable",
                "MinDrops" : 10,
                "MaxDrops" : 20,
                "SelectionWeight" : 1
            },
            {
                "EntryType" : "Table",
                "EntryName" : "EquipmentTable",
                "MinDrops" : 1,
                "MaxDrops" : 3,
                "SelectionWeight" : 1
            },
            {
                "EntryType" : "Item",
                "EntryName" : "diamonds",
                "MinDrops" : 1,
                "MaxDrops" : 5,
                "SelectionWeight" : 1
            }
        ]
    }
]
@nlohmann
Copy link
Owner

Can you please post the description of the exception (what())?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Apr 20, 2019
@jeffmorr
Copy link
Author

I get:
Unhandled exception at 0x00007FFCAF36A388 in Driver.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x0000007C0B33E310. occurred

from this function

bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
                     const detail::exception& ex)
    {
        errored = true;
        if (allow_exceptions)
        {
            // determine the proper exception type from the id
            switch ((ex.id / 100) % 100)
            {
                case 1:
                    JSON_THROW(*static_cast<const detail::parse_error*>(&ex));
                case 4:
                    JSON_THROW(*static_cast<const detail::out_of_range*>(&ex)); //Exception is thrown here!
                // LCOV_EXCL_START
                case 2:
                    JSON_THROW(*static_cast<const detail::invalid_iterator*>(&ex));
                case 3:
                    JSON_THROW(*static_cast<const detail::type_error*>(&ex));
                case 5:
                    JSON_THROW(*static_cast<const detail::other_error*>(&ex));
                default:
                    assert(false);
                    // LCOV_EXCL_STOP
            }
        }
        return false;
    }

@jeffmorr jeffmorr reopened this Apr 20, 2019
@nlohmann
Copy link
Owner

Can you catch the exception and print what(), please?

@Meqolo
Copy link

Meqolo commented Apr 27, 2019

How do i do that? I tried catching the exception but it still crashed rather than printing anything

@nlohmann
Copy link
Owner

Like this:

try
{
    // put here whatever code you call where the exception is triggered
}
catch (std::exception& e)
{
    std::cerr << e.what() << std::endl;
}

@Meqolo
Copy link

Meqolo commented Apr 27, 2019

image
Oh, just realised that it's cutting bits of it off

@nlohmann
Copy link
Owner

This is not the library's fault - please check if the input you parse is proper JSON.

@Meqolo
Copy link

Meqolo commented Apr 27, 2019

Oh, I was also on the wrong issue, sorry.

@stale
Copy link

stale bot commented May 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 27, 2019
@nlohmann nlohmann closed this as completed Jun 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants