Android-specific JSON parse failure

Feb 10, 2015 at 11:30 PM
Using Rest SDK 2.4.0, I tried the following which works on windows desktop but fails on Android 4.4:

std::error_code err;
web::json::value testJSON = web::json::value::parse(U("{\"key\":\"value\"}"), err);
<<print testJSON.serialize() and err.message() to debugger>>

On windows this yields the desired result:
err = "unknown error", testJSONserialized = {"key":"value"}
But on Android the same code produces:
err = "Malformed token", testJSONserialized = null

Given correct conversion wide<->narrow strings for each platform, why do the two platforms yield different results?
Feb 12, 2015 at 11:52 PM
Hi Sserpenthraxus,

This sounds funny, we have test cases covering JSON usage like you've shown that we run on Android. To double check I also tried the following in a test case and it passes fine running in the Visual Studio Emulator for Android.
    std::error_code err;
    web::json::value testJSON = web::json::value::parse(U("{\"key\":\"value\"}"), err);
    VERIFY_ARE_EQUAL(true, testJSON.is_object());
    VERIFY_ARE_EQUAL(U("\"value\""), testJSON[U("\"key\"")].as_string());
    const auto &serialized = testJSON.serialize();
    VERIFY_ARE_EQUAL(U("{\"key\":\"value\"}"), serialized);
I'm using KitKat 4.4 - 4.4.4, (android-19).

How are you building for Android and running?

Thanks,
Steve
Feb 13, 2015 at 8:25 PM
Thanks for the quick answer -- we’re building via Clang3.4 (android-17, C++11) and were able to get it working by using the -fsigned-char compiler switch. Unfortunately Clang defaults to using an unsigned char type for ARM. This affects the EOF checking code in Casablanca, e.g. json_parsing.cpp line 308:

const typename std::char_traits<CharType>::int_type m_eof;

template <typename CharType>
CharType JSON_Parser<CharType>::EatWhitespace()
{
CharType ch = NextCharacter();

while ( ch != this->m_eof && iswspace((int)ch) )


That is, m_eof contains -1 as it’s an integer type that can hold all values of char_type plus EOF. However, ch in the code above isn’t the same type and winds up with 255 != -1.
Feb 14, 2015 at 12:20 AM
Hi Sserpenthraxus,

Ah yes I see, I was running in the emulator so not actually ARM. This is a bug, we shouldn't be blindly assuming in our code that char is signed. I'm working on a fix to address this. Thanks for the further analysis. I'll update this thread once it is checked in.

Steve
Feb 16, 2015 at 7:52 PM
FYI this has been fixed in the 'development' branch. The code no longer assumes that the 'char' type is signed. Thanks again for reporting the issue. I verified all our JSON tests pass running with the /J switch on Windows and running for the ARM architecture on a physical Android device.

Steve
Feb 17, 2015 at 7:20 PM
Thanks for the quick response and fix!