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

error parsing character å #1626

Closed
mikelaltadi opened this issue Jun 4, 2019 · 5 comments
Closed

error parsing character å #1626

mikelaltadi opened this issue Jun 4, 2019 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mikelaltadi
Copy link

mikelaltadi commented Jun 4, 2019

Hello!
Im having a trouble parsing a std::string that contains this character. å
I see it with decimal value of -27 in a array of chars.
And the nlohmann::json::accept
and
nlohmann::json::parse function
gives me an error
error_message = "invalid string: ill-formed UTF-8 byte";

But, if the character is in a std::string , should be valid isn´t it?

Environment: Windows 10 and Microsoft Visual Studio 2015

In this page, that validates UTF-8 , says that å is a correct UTF-8 character.
https://onlineutf8tools.com/validate-utf8

@nickaein
Copy link
Contributor

nickaein commented Jun 4, 2019

This library does supports UTF-8, even with std::string. Are you sure your string encodes characters correctly? For starters, you might take a look at notes at characters encoding.

If that string is a string literal hardcoded in the source code file, you might want to make sure your source code file encoding is in UTF-8. If you are reading it from a file, check the encoding of that file and make sure it is UTF-8.

@mikelaltadi
Copy link
Author

char mess[3000]="{"machine.serialnumber":"G4åd"}";

std::string s = mess;

bool validated = nlohmann::json::accept(s);

nlohmann::json::parse(s);

This is the code that produces exception.

@nickaein
Copy link
Contributor

nickaein commented Jun 4, 2019

Shouldn't you skip the double-quotes (e.g. by putting \ before every " in string)? Can you print it out to console to see if it is correct?

Also, you should probably make sure your source file is being saved in UTF-8 encoding, not some others (e.g. ASCII, UTF-16).

To be able to easier to track down the issue, you can test the following code. Put the JSON in a separate file named input.json and make sure it is in UTF-8 encoding (Windows is not that UTF-8 friendly and usually defaults to ASCII or UTF-16).

#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include "json.hpp"

using namespace std;

int main() 
{
    std::ifstream fin("input.json");
    if(!fin)
    {
        cout << "Failed to open file." << endl;
        return 1;
    }

    std::stringstream stream;
    stream << fin.rdbuf();   
    std::string json_string = stream.str();

    cout << "File contents:"  << endl;
    cout << json_string << endl << endl;

    bool validated = nlohmann::json::accept(json_string);
    cout << "Json validation result: " << validated << endl;

    auto json = nlohmann::json::parse(json_string);

    cout << "machine.serialnumber: " << json["machine.serialnumber"] << endl;
    
    return 0;
}

I have tested it and it outputs the following as expected:

File contents:
{"machine.serialnumber":"G4åd"}

Json validation result: 1
machine.serialnumber:"G4åd"

If this solved your issue, you should compare the string you had in your code to json_string in this code example to see where it differs. My guess is either you didn't skipped your string literal correctly or the encoding of the source code file is not UTF-8.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 5, 2019
@nlohmann
Copy link
Owner

nlohmann commented Jun 5, 2019

@mikelaltadi Do you need further assistance?

@mikelaltadi
Copy link
Author

No. I replace this letter by ‘a’ when I find it before parsing. Its enough for me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants