2

I am developing an API , where I receive some article related data as a POST request. The receiver I have as following:

@ApiOperation(value = "Add a new Article", produces = "application/json")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity createPost(@RequestBody String postContent) {
    try {
        // Here I need to conver the postContent to a POJO
        return new ResponseEntity("Post created successfully", HttpStatus.OK);
    } catch (Exception e) {
        logger.error(e);
        return responseHandler.generateErrorResponseJSON(e.getMessage(), 
        HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

Now it works for simple request like:

{
  "id": "1",
  "title": "This is the Post Title",
  "body": "This is the Post Body",
  "authorName": "Test",
  "tagList": [
    "tag-1",
    "tag-2",
    "tag-3"
  ]
}

But in real scenario I will get receive a HTML content as the value of the "body" key in request JSON, which can have "",<,> or many thing. Then the String to JSON conversion will fail. Is there any api, library or example, where I can have HTML content as the value of a JSON key.

Following is my input request where the code is failing to parse the JSON to Object:

{
  "menuId": "1",
  "title": "This is the Post Title",
  "body": "<p style="text-align:justify"><span style="font-size:16px"><strong>Mediator pattern</strong> is a Behavioral Design pattern. It is used to handle complex communications between related Objects, helping by decoupling those objects.</span></p>",
  "authorName": "Arpan Das",
  "tagList": [
    "Core Java",
    "Database",
    "Collection"
  ]
}

Now How I am parsing the json is like:

public Post parsePost(String content) {
        Post post = new Post();
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject jsonObject = (JSONObject) jsonParser.parse(content);
            post.setMenuId((Integer) jsonObject.get(Constant.MENU_ID));
            post.setTitle((String) jsonObject.get("title"));
            post.setBody((String) jsonObject.get("body"));
            post.setAuthorName((String) jsonObject.get("authorName"));
            post.setAuthorId((Integer) jsonObject.get("authorId"));
            post.setTagList((List) jsonObject.get("tag"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return post;
    }

It is giving a parse exception :

Unexpected character (t) at position 77.

The library I am using for parsing the JSON is:

 <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
Community
  • 1
  • 1
Arpan Das
  • 1,015
  • 3
  • 24
  • 57
  • *Then the String to JSON conversion will fail*: why would it? Do you really think JSON parsers are not able to properly escape/unescape values that need to be? – JB Nizet Jan 30 '18 at 17:20
  • JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(content); is failing to parse the json – Arpan Das Jan 30 '18 at 18:04
  • Post a complete minimal example (including a hard-coded JSON input) reproducing the issue. And tell us what library you're using to parse the JSON. And post the complete stack trace of the exception. Why don't you let Spring parse the JSON for you, BTW? – JB Nizet Jan 30 '18 at 18:11
  • So you want to convert "Hello World

    Some text

    " to this "{ "html": { "head": { "title": "Hello World" }, "body": { "p": "Some text" } } }"?
    – Rashin Jan 30 '18 at 18:13
  • 1
    @Rashin no, he wants to be able to parse JSON containing `"body": "

    some HTML \"with\" quotes

    "`
    – JB Nizet Jan 30 '18 at 18:15
  • Updated the question, with the minimal implementation – Arpan Das Jan 30 '18 at 18:21
  • The result you get is the expected one, because the JSON input is indeed invalid. Double quotes must be escaped (as shown in my above comment). You need to fix the code that generates this JSON. – JB Nizet Jan 30 '18 at 18:22

0 Answers0