JSON valud conversion portability

Jan 17, 2015 at 5:19 AM
Hello,
I am doing an experiment with a portable library (Android and Windows Phone) with VS 2015 CTP calling a third party API which return a a JSON containing a string property that is actually an integer (not a great idea but I can't change that third party API).

The problem seem related that Android as_string() version returns an std::string while Windows Phone version returns a std::wstring.

How can this be handled in a portable library?

Thanks,
Pedro
Jan 17, 2015 at 5:27 AM
I forgot to mention that my current mitigation is to use a preprocessor directive. I am curious if there is something better
Jan 17, 2015 at 11:01 PM
Hi MsdnDev,

Yes in the C++ Rest SDK we use a string type dependent on the underlying platform type. What you are seeing is on Windows we are using UTF-16 stored in a std::wstring. On Android we use UTF-8 stored in a std::string. In some parts of the library we have overloads/options on each platform for dealing with each type, but not in the JSON portion of the library.

You can use a preprocessor ifdef like you are doing. Other options include using our typedef ::utility::string_t directly depending what you are doing, you can convert directly to one of the string types, like std::string, right after calling json::value::as_string(). The library contains a set of conversion functions for strings, specifically the one you could use for this is ::utility::conversions::to_utf8string.

Steve
Marked as answer by MsdnDev on 1/17/2015 at 3:31 PM
Jan 17, 2015 at 11:32 PM
Hi Steve,
The conversion utility is a bit more elegant. Thank you for the suggestion.

Pedro