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.

7
  • 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. Commented Apr 11, 2016 at 7:42
  • freeformatter.com/javascript-escape.html Commented Apr 11, 2016 at 7: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. Commented Apr 11, 2016 at 7: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. Commented Apr 11, 2016 at 7:53
  • 1
    Just add something like CDATA and save the file as .jsonxyz. Then run a custom java program which would convert .jsonxyz to .json Commented Apr 11, 2016 at 8:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.