Skip to content

Commit

Permalink
Improve jstrencode/jstrdecode fun
Browse files Browse the repository at this point in the history
  • Loading branch information
xexyl committed Oct 11, 2024
1 parent b976f72 commit d2586d9
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions json_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,63 @@ jdecencchk(int entertainment)
* and now we entertain, at least for those with a dark sense of humour :-)
*/
if (entertainment > 1) {
decstr = "\\uD83D\\uDD25\\uD83E\\uDDD9";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, &mlen2);
if (mstr2 == NULL) {
err(156, __func__, "json_decode_str(<%s>, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {

dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
dbg(DBG_MED, "%s == %s", decstr, mstr2);

/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(157, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);


/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(158, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}

/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}


}

if (entertainment > 2) {
decstr = "\\uD83E\\uDEC3\\uD83D\\uDD25\\uD83D\\uDC09";
/*
* test decoding the JSON encoded string
Expand Down

0 comments on commit d2586d9

Please sign in to comment.