Replacing ">" and "<" with Unicode code point for. json encoding

Hello everyone after few hours of looking at docs and googling I still can’t for the love of me figure out how to mirror the json_encode in PHP json_encode with JSON_HEX_TAG which according to documentation here convert

All < and > are converted to \u003C and \u003E.

I know that using Jason_encode with the escape: :html_safe tag will will convert the ‘<’ with the proper unicode but NOT ‘>’.

What’s the most convenient way to fix this?
Thanks! :smiley:

Welcome to the forum :rocket:

There is an open issue about this from 20d ago, and it definitely seems like a bug. html_safe encoding bug · Issue #151 · michalmuskala/jason · GitHub

This line in the code jason/encode.ex at master · michalmuskala/jason · GitHub is where the angle brackets should be encoded, and < is absent.

html_ranges​ ​=​ ​[​{​0x00​..​0x1F​,​ ​:unicode​}​,​ ​{​?<​,​ ​:unicode​}​,​ ​{​?/​,​ ​?/​}​ ​|​ ​slash_escapes​]
1 Like

Maybe for now do your own String.replace until the bug is fixed. Or fork the repo, update the code for your use, and submit a PR to fix upstream. :raised_hands:

I meant ?> is missing