1

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.

Tinus Tate
  • 2,237
  • 2
  • 12
  • 33
  • You could use a tool to escape the HTML before pasting it into the JSON file. Write HTML in notepad -> escape it using an online tool -> paste it into the JSON file. – Teddy Apr 11 '16 at 07:42
  • http://www.freeformatter.com/javascript-escape.html – Teddy Apr 11 '16 at 07:44
  • Or you could write a graphical tool to take your inputs and generate the JSON file, if you have to do this activity often. – Teddy Apr 11 '16 at 07:46
  • @Teddy cheers for the idea, but the editing workflow would become quite lengthy when a online tool or UI is introduced, the nice thing about json is that it can be edited quickly without UI, just with plain old notepad or on the command line using the default editor like vi or nano. – Tinus Tate Apr 11 '16 at 07:53
  • Yeah.. that's true. Here's a discussion on something like CDATA: http://stackoverflow.com/a/14936090/1364747 – Teddy Apr 11 '16 at 08:05
  • 1
    Just add something like CDATA and save the file as .jsonxyz. Then run a custom java program which would convert .jsonxyz to .json – Teddy Apr 11 '16 at 08:10
  • "The json doesn't have to meet the json specification" of course you can do that, but please don't call it JSON any more. – Henry Apr 11 '16 at 08:22

1 Answers1

1

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.

Tinus Tate
  • 2,237
  • 2
  • 12
  • 33