c# - Sending JSON with escape characters such as group separators -
specific question: how should 1 format characters such group separator (0x1d) in json?
details: i've inherited c# codebase reading barcodes, putting them in json messages , sending them on way service (not c# or windows based!)
the code takes byte array such as:
byte[] rawdata = { 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 52, 29, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48};
and uses code translate byte array string put in .json:
string datanew = regex.unescape(new string(encoding.ascii.getstring(rawdata).tochararray()));
the relevant part of .json looks like:
"notes": [ { "id": 0, "details": "produc code: codetype: datamatrix, data: 000000000000000000000000000004\u001d1000000000000000", "active": true, "acknowledged": false, "reported": false } ],
as can see, encoding.ascii.getstring...
treated ascii character 29 (0x1d) little differently. put in "\u001". if don't put in regex.unescape puts in "\\u001". i'm shaky on encoding , .json. can tell me:
- what "\u001". microsoft specific?
- the folks receiving message stating servers "choke" on message. no problems when there no group separator. can group separator sent via .json messages? , if how? if it's doable, how decode c# technologies?
slightly more info: server folks \u001 rendered ^] (which indicates non-printable character) on end. strip \u001 before sending message , might not bad idea. occurs me there other non-printable characters (cr, lf example) might keep info , send via .json.
please excuse vagueness. i'm still getting familiar code base , these particular ideas. did try , find out \u001 no avail. ask better explanation "choke", nice have specific questions in mind. if can provide clarification or more details please let me know.
it not encording.ascii.getstring
inserting \u
escape sequence; json serialization process (which have not shown in question) doing it, , normal , expected.
in json, unprintable characters in string encoded \u
+ 4 hexadecimal digits, digits unicode (utf-16) representation of character. in output, can see has happened: have \u001d
, ascii character 29 original byte sequence.
generally, avoid problems creating , decoding json, should use json serialization library such json.net designed handle you. i'm guessing "server folks" mention in question attempting parse json manually, , why running trouble. if you're doing correctly, should not need use regexes @ manipulate json string, including using regex.unescape
.
here short fiddle demonstrate: https://dotnetfiddle.net/uy3pcm
Comments
Post a Comment