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 Support Binary Data #130

Open
faultaddr opened this issue Jun 13, 2023 · 2 comments
Open

Json Parse Support Binary Data #130

faultaddr opened this issue Jun 13, 2023 · 2 comments

Comments

@faultaddr
Copy link
Contributor

Problem

yyjson cannot parse correctly when a json contains binary data

@ibireme
Copy link
Owner

ibireme commented Jun 13, 2023

According to the RFC 8259 standard, valid JSON should be a UTF-8 string. I’m not sure what JSON containing binary datameans. Could you provide an example file?

@VaslD
Copy link

VaslD commented Jul 10, 2023

The only widespread and legit way to put arbitrary data (blobs) inside JSON is encoding as Base64 strings. Like this:

{
  "result": 1,
  "data": "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"
}

Because Base64'd data is just a string in JSON's point of view, you can use YYJSON to read strings from the original JSON, and then convert them from Base64 to data objects some other way.


If you're talking about JSON mixed with binary contents, like this hex dump:

7B 22 65 6D 70 6C 6F 79 65 65 22 3A 7B 22 6E 61    |    {"employee":{"na
6D 65 22 3A 22 73 6F 6E 6F 6F 22 2C 22 73 61 6C    |    me":"sonoo","sal
61 72 79 22 3A 35 36 30 30 30 2C 22 6D 61 72 72    |    ary":56000,"marr
69 65 64 22 3A 74 72 75 65 7D 7D 52 49 46 46 56    |    ied":true}}RIFFV
20 03 00 57 45 42 50 56 50 38 20 4A 20 03 00 B0    |     ��WEBPVP8 J ��°
B9 0A 9D 01 2A 40 06 FF 05 3E 6D 32 95 48 24 22    |    ¹��*@�ÿ�>m2�H$"

You can read it with YYJSON's YYJSON_READ_STOP_WHEN_DONE option if you know beforehand where JSON starts. YYJSON doesn't seem to support reading arbitrary data to look for parts of valid JSON (I guess that would be really hard to implement, and too error-prone to use), but it can be configured to ignore arbitrary data after a valid JSON ends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants