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
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

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

Avatar
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

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

05-02-2019

I tested this and it worked fine. As Scott mentioned use encode/decode as applicable.

output:

{":type": "weretail/components/content/text","message": "some junk characters test=€ , Š , Œ , ™ , š , œ , ž{}[]<>=-()@#$&*"}

acs-aem-samples/SampleComponentExporter.java at master · Adobe-Consulting-Services/acs-aem-samples ·...

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Exporter;

import org.apache.sling.models.annotations.ExporterOption;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.Required;

import org.apache.sling.models.annotations.injectorspecific.Self;

import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import com.adobe.cq.export.json.ComponentExporter;

import com.adobe.cq.export.json.ExporterConstants;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**

* This file focuses on making a Sling Model compatible with AEM Content

* Services (and thus SPA Editor), as this requires: a) Implementing the

* ComponentExporter interface.

*

* Note that these is a sibling interface to ComponentExporter,

* com.adobe.cq.export.json.ContainerExporter, that is used for components that

* included other components (like the Layout Container).

*

* For more detailed examples of Sling Models and Sling Model Exporters, see

* this file's sibling sample files (SampleSlingModel.class and

* SampleSlingModelExporter.class).

*/

@Model(

// This must adapt from a SlingHttpServletRequest, since this is invoked

// directly via a request, and not via a resource.

// If can specify Resource.class as a second adaptable as needed

adaptables = { SlingHttpServletRequest.class },

// This Model should have the specific Model class

// (SampleComponentExporter.class)

// AS WELL AS the ComponentExporter.class. Its required that

// ComponentExporter.class to be set at an adapter

// since this how the AEM Content Services JSON Exporter figures out which

// resources to serialize.

adapters = { SampleComponentExporter.class, ComponentExporter.class },

// The resourceType is required if you want Sling to "naturally" expose this

// model as the exporter for a Resource.

resourceType = SampleComponentExporter.RESOURCE_TYPE,

defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

// name = the registered name of the exporter (  ExporterConstants.SLING_MODEL_EXPORTER_NAME => jackson )

// extensions = the extensions this exporter is registered to (ExporterConstants.SLING_MODEL_EXTENSION => json)

// selector = defaults to "model", can override as needed; This is helpful if a single resource needs 2 different JSON renditions.

//            (ExporterConstants.SLING_MODEL_SELECTOR => model)

@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, selector = ExporterConstants.SLING_MODEL_SELECTOR,

extensions = ExporterConstants.SLING_MODEL_EXTENSION, options = { // options are optional... this just shows

// that it is possible...

/**

* Jackson options: - Mapper Features:

* http://static.javadoc.io/com.fasterxml.jackson.core/jackson-databind/2.8.5/com/fasterxml/jackson/dat...

* - Serialization Features:

* http://static.javadoc.io/com.fasterxml.jackson.core/jackson-databind/2.8.5/com/fasterxml/jackson/dat...

*/

@ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true"),

@ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "false") })

// Mark as JSON Serializable as the Model's class (SampleComponentExporter.class) or interface if those are used.

@JsonSerialize(as = SampleComponentExporter.class)

// Make sure the Model implementation implements (or the Model interface extends) com.adobe.cq.export.json.ComponentExporter.

public class SampleComponentExporter implements ComponentExporter {

static final String RESOURCE_TYPE = "weretail/components/content/text";

@Self

private SlingHttpServletRequest request;

@Self

private Resource resource;

// Injection will occur over all Injectors based on Ranking;

// Force an Injector using @Source(..)

// If an Injector is not working; ensure you are using the latest version of

// Sling Models

@SlingObject

@Required

private ResourceResolver resourceResolver;

@Override

public String getExportedType() {

// This method is required by ComponentExporter and its value populates the

// `:type` key in the JSON object.

// The resource value is ~always the ResourceType for this model (See @Model(..)

// above).

return RESOURCE_TYPE;

}

/**

* Jackson Annotations on the method level are supported;

* ie. @JsonIgnore, @JsonProperty(..), etc.

*

* For Jackson Annotations:

* https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

*/

public String getMessage() {

String rteString = "some junk characters test=€ , Š , Œ , ™ , š , œ , ž{}[]<>=-()@#$&*";

// encode/decode as applicable

return String.format(rteString, request.getResource().getPath());

}

}

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

06-02-2019

Thank you guys, I have used replace keyword instead of decoding, and is working as expected.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 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,409 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

06-02-2019

I am happy to hear its working.