[
https://issues.apache.org/jira/browse/SLING-7748?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Suren Konathala resolved SLING-7748.
------------------------------------
Resolution: Not A Problem
With help from [~justinedelson] we found that the issue was with Gson's
"toJson" method that is not
Before the fix, my code was
{code:java}
// code placeholder
Resource jcrdataResource = resource.getChild("jcr:content");
ValueMap jcrProperties = ResourceUtil.getValueMap(jcrdataResource);
...
JsonObject assetMetadataJson.addProperty(hit.getPath(), new
Gson().toJson(allProperties));
...
{code}
Modified this to be like this:
{code:java}
// code placeholder
Resource jcrdataResource = resource.getChild("jcr:content");
ValueMap jcrProperties = ResourceUtil.getValueMap(jcrdataResource);
...
JsonObject assetMetadataJson.addProperty(hit.getPath(),
customGson().toJson(allProperties));
...
...
private Gson customGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
JsonSerializer<Calendar> serializer = new JsonSerializer<Calendar>() {
@Override
public JsonElement serialize(Calendar src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(ISO8601.format(src));
}
};
gsonBuilder.registerTypeAdapter(Calendar.class, serializer);
gsonBuilder.registerTypeAdapter(GregorianCalendar.class, serializer);
return gsonBuilder.create();
}
{code}
With the above change, the output JSON of all DATES is always in ISO8601 format
(i've removed all other properties and left just dates for reference)
{code:java}
// code placeholder
\"dc:title\":\"SiriusXM Sample
Suren\",\"dam:extracted\":\"2018-01-31T15:31:15.119-05:00\",",\"jcr:created\":\"2018-01-31T15:31:14.978-05:00\",\"dc:modified\":\"2018-04-25T18:08:40.335-04:00\",\"jcr:lastModified\":\"2018-06-28T14:37:05.043-04:00\",\"cq:lastReplicated\":\"2018-05-18T17:35:36.793-04:00\
{code}
> Value of Month from jcr:lastModified is INCORRECT when
> org.apache.sling.api.resource.Resource.getValueMap() is used
> -------------------------------------------------------------------------------------------------------------------
>
> Key: SLING-7748
> URL: https://issues.apache.org/jira/browse/SLING-7748
> Project: Sling
> Issue Type: Bug
> Components: API
> Affects Versions: API 2.16.2
> Reporter: Suren Konathala
> Priority: Major
> Attachments: 12928514_how-to-create2.png, Screen Shot 2018-06-19 at
> 4.09.38 PM.png, Screen Shot 2018-06-19 at 4.09.56 PM.png, how-to-create.png,
> how-to-create.png, indy.png, indy2.png, sand.png, sand.png,
> service-result.png, service-result.png
>
>
> When using *org.apache.sling.api.resource.Resource* , the method *getValueMap
> (*
> [https://sling.apache.org/apidocs/sling9/org/apache/sling/api/resource/Resource.html#getValueMap--]
> ), returns a ValueMap of all the properties.
> We observed that the "*month*" in "*jcr:lastModified*" is off by 1 month.
> While Year, Day, Hour, Min, Seconds are all correct. Please see the
> attachments. And this is the same for all Nodes.
> The last date modified was
> "2018-*{color:#d04437}05{color}*-29T10:31:49.123-05:00" while the value from
> the above method returned as below:
> {color:#333333}jcr:lastModified\"{\"year\":2018,*\"month\":4,\{color}*"dayOfMonth\":29,\"hourOfDay\":10,\"minute\":31,\"second\":49}{color}
> We did not test this on all nodes though.
>
> Sample code:
> {code:java}
> Resource jcrdataResource = resource.getChild("jcr:content");
> ValueMap jcrProperties = ResourceUtil.getValueMap(jcrdataResource);
> ...
> JsonObject assetMetadataJson.addProperty(hit.getPath(), new
> Gson().toJson(allProperties));
> ...
> JsonElement assetMetadataJsonElement = new
> Gson().fromJson(assetMetadataJson.toString(), JsonElement.class);
> ...
> JsonObject mainjsonObject.add("assets", assetMetadataJsonElement);
> return mainjsonObject;
> {code}
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)