RandyGaul / cute_framework

The *cutest* framework out there for creating 2D games in C++!
https://randygaul.github.io/cute_framework/#/
Other
534 stars 31 forks source link

Parsing an int to a float in json returns 0 #148

Closed waldnercharles closed 3 months ago

waldnercharles commented 10 months ago

When attempting to deserialize an int to a float, yyjson appears to return 0. eg "health": 100 /* vs */ "health": 100.0. The former will return 0 when calling cf_json_get_float

Here's a quick test case I threw together with behavior I've implemented in my personal project. If you think the behavior is reasonable, I can implement it in the cf wrapper as well.

TEST_CASE(test_json_numeric)
{
    const char* s =
            "{\n"
            "\t\"health\": 100,\n"
            "\t\"speed\": 50.0,\n"
            "\t\"is_cute\": 1\n"
            "}"
    ;
    CF_JDoc doc = cf_make_json(s, CF_STRLEN(s));
    CF_JVal root = cf_json_get_root(doc);
    float health = cf_json_get_float(cf_json_get(root, "health"));
    int speed = cf_json_get_int(cf_json_get(root, "speed"));
    bool is_cute = cf_json_get_bool(cf_json_get(root, "is_cute"));

    REQUIRE(health == 100.f);
    REQUIRE(speed == 50);
    REQUIRE(is_cute == true);

    cf_destroy_json(doc);

    return true;
}
RandyGaul commented 3 months ago

Did you have a fix for implemented already?

RandyGaul commented 3 months ago

I implemented C-style conversions in the simplest way that first came to me. Let me know if you have a better method!

https://github.com/RandyGaul/cute_framework/commit/6120ff13978da675140d4226f26fa6f149e18630

RandyGaul commented 3 months ago

And thanks for the test case, I've copied it into CF.