Tinus Tate
Tinus Tate

Reputation: 2365

Html in json (no need for json specification)

I'm quite the happy jackson objectmapper user.

But one thing that does bug me is the way i manually have to enter html in json, there is a lot of escaping which is very difficult for a inexperience user.

Is there a way the allow unescaped plain html in a json file that can be read by jackson? For example something like cdata in xml.

The json doesn't have to meet the json specifications/standard and any pre or post processing is possible. But it needs to be entered manually in the json using for example notepad.

There are many questions and answers on stackoverflow on this topic, but they mostly all need to meet the json specification which isn't a requirement for me.

Upvotes: 1

Views: 169

Answers (1)

Tinus Tate
Tinus Tate

Reputation: 2365

I did go with @Teddy solution:

Just add something like CDATA and save the file as .jsonxyz.Then run a custom java program which would convert .jsonxyz to .json

With a pattern

<cdata>(.*?)</cdata>

I matches the cdata containing html in my json. With appendReplacement i replaced the html with escaped html compatible with json. Next i just read the whole json with jackson.

To serialize it again with cdata and unescaped html i extended:

HtmlFieldSerializer extends JsonSerializer<String>{
...
gen.writeRaw(": \"<cdata>"+value+"</cdata>\"");

Next jackson can simply write json to file.

Upvotes: 1

Related Questions