question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support JSON ContentType

See original GitHub issue

I’d like to use this template language to fill in some large JSON. Do you think it makes sense to have a ContentType.Json, that escapes all JSON values like done here in JsonValue.escape()?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
spinscalecommented, Nov 25, 2020

Just for the internet archive and google. I solved this with a special output, that escapes user content like this

    private static final class JsonStringOutput extends StringOutput {
        @Override
        public void writeUserContent(String value) {
            super.writeUserContent(escape(value));
        }
    }

    String render(final String templateName, final Map<String, Object> params) {
        try (StringOutput output = new JsonStringOutput()) {
            templateEngine.render(templateName + ".jte", params, output);
            return output.toString();
        }
    }

    public String string escape(String input) {
       ...
    }

I used the escape logic from minimal-json at https://github.com/ralfstx/minimal-json/blob/master/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonWriter.java

0reactions
kelunikcommented, Nov 25, 2020

@spinscale Seems like it’s easy enough to solve without specialized support. Instead of only escaping strings, something like fastjson’s JSON.toJSONString() converting any object might be good.

It might be a good idea to decouple some things, so full support can be implemented by plug-ins.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Which JSON content type do I use? - Stack Overflow
The right content type for JSON is application/json UNLESS you're using JSONP, also known as ...
Read more >
What is the Correct Content-Type for JSON? Request Header ...
The Correct Content-Type for JSON. JSON has to be correctly interpreted by the browser to be used appropriately. text/plain was typically used ...
Read more >
What is the correct JSON content type ? - GeeksforGeeks
What is the correct JSON content type ? ... Content-Type is an HTTP header that is used to indicate the media type of...
Read more >
How to Set JSON Content Type In Spring MVC - Baeldung
Learn different options for setting the content type in Spring MVC. ... Let's start with a simple example of an API exposing a...
Read more >
What is the correct Content Type for JSON? - ReqBin
The correct content type for JSONP (padded JSON) is application/javascript. For JSON-LD (JSON-linked data), the correct content type is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found