0

I am trying to send a JSON from C++ code to a REST service running on Java. I am using Visaul Studio 2019, nlohmann/json library to serialize my objects and libcurl to make the HTTP request.

I have an the following object I want to serialize:

typedef struct{
    int ordinal;
    std::string detail;
    Level level;
} LogType

static const LogType ONLINE = {0, "Çevrimiçi", Level::INFO};

I use this LogType to fill in some members of AliveLog(Very much similar to Java enums). And I use the following to serialize it:

std::string AliveLog::getJSON(){
    json j = json{
        {"createdAt", createdAt},
        {"id", id},
        {"message", message},
        {"detail", detail},
        {"deviceId", deviceId},
        {"type", type.ordinal},
        {"level", level}
    };
return j.dump(-1, ' ', false, json::error_handler_t::replace);
}

When I call this function I get bunch of question marks and ½'s where "Çevrimdışı" should be.

Also my REST service throws an exception JSON parse error: Invalid UTF-8 middle byte 0xdd;

I tried setting /utf8 and /validate-charset for compiler command line options but no avail.

If I just use j.dump() with no argument it throws an exception saying "type_error.316 if a string stored inside the JSON value is not UTF-8 encoded". In my text editor I have "Auto-detect UTF-8 encoding without signature" selected.

This same piece of code work fine written in Java, I am trying to make a C++ version of it.

edit: Added more information regarding the LogType and AliveLog

bobku123
  • 81
  • 9
  • 1
    How is the `ONLINE` object connected to the `getJSON` function? What is the `LogType` type? How is the string in `ONLINE` stored? Please try to create a [mcve] to show us. – Some programmer dude Nov 08 '19 at 09:06
  • Usually when you get `?` means you have different encoding. You might debug and find why utf8 is encoded something else. Hopefully helps – Mostafa Nov 08 '19 at 09:09
  • Save your .cpp file in utf-8. – rustyx Nov 08 '19 at 09:20
  • I have added more information regarding LogType and AliveLog. – bobku123 Nov 08 '19 at 10:42
  • One thing you should really start with is replacing your hard-coded UTF-8 string like '\u00c7evrimi\u00e7i' (or, if you want to make that more readable, write some functions like `string ced() { return "\u00e7"; }`). In any case, your code should not contain any special characters. – Aziuth Nov 08 '19 at 11:17
  • This whole thing has been very frustrating. I have set compiler, both source and execution charset to utf-8 and saved all the files seperately using "Save as" as UTF-8 and I am still not able to use j.dump() without getting an exception. What makes it so complicated to deal with this in C++? I never had to deal with anything like this in Java. – bobku123 Nov 08 '19 at 12:52
  • `std::string` uses the `char` type for holding data. You very likely need to use `std::wstring` to store wide characters. In any case, it is up to both you and your compiler to properly encode and decode strings. – Bob Dalgleish Nov 08 '19 at 16:30

0 Answers0