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

json::parse(begin, end) parse error on first character using uchar* #1887

Closed
aeon0 opened this issue Jan 2, 2020 · 2 comments
Closed

json::parse(begin, end) parse error on first character using uchar* #1887

aeon0 opened this issue Jan 2, 2020 · 2 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@aeon0
Copy link

aeon0 commented Jan 2, 2020

What is the issue you have?

Using the json::parse() function with uchar* yields in a parsing error for the very first character.

Please describe the steps to reproduce the issue.

// outImg is of type cv::Mat (with outImg.rows > 0 and outImg.cols > 0)
uchar* imgBuff = outImg.isContinuous() ? outImg.data : outImg.clone().data;
size_t imgBuffSize = outImg.rows * outImg.cols * outImg.channels();
auto rawImgStr = nlohmann::json::parse(imgBuff, imgBuff + imgBuffSize);

What is the expected behavior?

A parsed img string can be added to the json object such as:

nlohmann::json output = {
  {"image", rawImgStr},
  {"imgWidth", outImg.size().width},
  {"imgHeight", outImg.size().height},
};

std::vector<std::uint8_t> v_bson = json::to_bson(output);

And what is the actual behavior instead?

Getting this error:

[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'D'

Which compiler and operating system are you using?

GCC v7.4, Linux Mint 19

Did you use a released version of the library or the version from the develop branch?

Release version: 3.7.3

@aeon0 aeon0 added the kind: bug label Jan 2, 2020
@nlohmann
Copy link
Owner

nlohmann commented Jan 3, 2020

What type is uchar and what encoding to you use? The library only supports UTF-8.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 3, 2020
@aeon0
Copy link
Author

aeon0 commented Jan 3, 2020

Oh yeah, forgot to mention that, the uchar is from opencv typedef unsigned char uchar.

And I guess the encoding is the whole problem here since the unsigned char actually represent intensity values for each pixel and channel in the range of 0-255. I guess if I want to encode that information into a json string, I'd have to map these values to valid utf-8 byte codes.

I ended up encoding the image to .jpg and create a base64 from that which I can add to the json without any issues.

@aeon0 aeon0 closed this as completed Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

2 participants