Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Splecial characters in RTE are displayed differently after conversion to JSON view

Avatar

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
rajareddych
Level 2

01-02-2019

1. Created a custom component dialog, which has field='Answer Text' with richtext field

1681730_pastedImage_0.png

2. Gave Special characters as input

1681736_pastedImage_3.png

1681731_pastedImage_1.png

3. Create Sling Model Exporter for JSON output

@PostConstruct

public void invokepost() {

if (answerText != null && !answerText.isEmpty()) {

answerText = formatAnswerRTEText(answerText);

}

}

// In this method, rteText is passed as a parameter which needs to be trimmed and covered with double quotes and remove all the new lines in the text

private String formatAnswerRTEText(String rteText) {

String modifiedRteText = null;

modifiedRteText = "\"" + rteText.trim() + "\"";

modifiedRteText = modifiedRteText .replaceAll("\\r|\\n", "");

return modifiedRteText ;

}

4. JSON output is not displaying special characters

1681737_pastedImage_8.png

Please guide me in resolving this issue. (Special characters should be displayed as it is in JSON output)

Replies

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

01-02-2019

Make sure to export the content as "UTF-8", check the sling exporter version per below link-

[SLING-7344] Jackson Sling Model Exporter needs correct character encoding - ASF JIRA

test with latest version and revert

Avatar

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
rajareddych
Level 2

04-02-2019

I have followed to crate similar servlet, I am getting error at json = object.writeValueAsString(answerModel); for writeValueAsString

@SlingServlet(

resourceTypes = {AHMJsonServiceConstants.ANSWER_RT},

selectors = "servlet",

extensions = "json",

methods = "GET"

)

public class ContentServlet extends SlingSafeMethodsServlet {

private static final long serialVersionUID = -1765279544984834180L;

private static Logger LOG = LoggerFactory.getLogger(AnswerModel.class);

@Override

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        Resource answerResource = request.getResource();

        AnswerModel answerModel = answerResource.adaptTo(AnswerModel.class);

       

        Object object = new Object();

        String json = "{}";

        try {

            json = object.writeValueAsString(answerModel);

        } catch (RepositoryException e) {

        LOG.error(AHMJsonServiceConstants.MSG_PROCESSING_ERROR + e.getMessage());

            // Silently die as this is a POC

        }

        // The magic to make localized content work.

            // BOTH ContentType and CharacterEncoding must be set.

            response.setContentType("application/json");

            response.setCharacterEncoding("UTF-8");

            response.getWriter().write(json);

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

04-02-2019

could you share the error trace?

this is incomplete code as I can't interpret a lot of lines-

-resourceTypes = {AHMJsonServiceConstants.ANSWER_RT},

AnswerModel answerModel = answerResource.adaptTo(AnswerModel.class);

Avatar

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
rajareddych
Level 2

04-02-2019

[ERROR] /C:/Users/raja/Desktop/aem-release/core/src/main/java/com/core/servlets/qr/ContentServlet.java:[40,26] cannot find symbol

  symbol:   method writeValueAsString(com.aetna.ahm.core.models.qr.AnswerModel)

  location: variable object of type java.lang.Object

Avatar

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
rajareddych
Level 2

Likes

0 likes

Total Posts

25 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
rajareddych
Level 2

04-02-2019

Answer Model is a JSON sling model exporter with 3 fields, 2 are RTE fields.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

04-02-2019

I am checking with the team - I have never seen this use case of wanting to export spec chars in JSON before.

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

04-02-2019

The error you have mentioned 'cannot find symbol' is not related to this JSON-special characters. 

I'll test the special characters issue later and revert acc.

Its most likely related to the default character encoding being set on Sling exporter. There must be a configuration to check/fix, if applicable.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

04-02-2019

The team believes this is not a RTE issue - but an output issue. Make sure you are setting the encoding properly.

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

04-02-2019