TargetRecommendationsJSONUtil.java
5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.cq.targetrecommendations.impl.util;
import com.adobe.cq.targetrecommendations.api.model.ProductFeed;
import com.adobe.cq.targetrecommendations.api.model.RecommendationAlgorithm;
import com.adobe.cq.targetrecommendations.api.model.RecommendationTemplate;
import com.adobe.cq.targetrecommendations.api.model.TargetRecommendation;
import com.adobe.cq.targetrecommendations.impl.model.ProductFeedImpl;
import com.adobe.cq.targetrecommendations.impl.model.RecommendationAlgorithmImpl;
import com.adobe.cq.targetrecommendations.impl.model.RecommendationTemplateImpl;
import com.adobe.cq.targetrecommendations.impl.model.TargetRecommendationImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public class TargetRecommendationsJSONUtil {
public static List<RecommendationTemplate> parseTemplates(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
ArrayList<RecommendationTemplate> templates = new ArrayList<RecommendationTemplate>();
for (int i = 0; i < jsonPayload.length(); ++i) {
JSONObject currentTemplate = jsonPayload.getJSONObject(i);
templates.add(RecommendationTemplateImpl.fromJson(currentTemplate));
}
return templates;
}
public static List<RecommendationAlgorithm> parseAlgorithms(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
ArrayList<RecommendationAlgorithm> algorithms = new ArrayList<RecommendationAlgorithm>();
for (int i = 0; i < jsonPayload.length(); ++i) {
JSONObject currentAlgorithm = jsonPayload.getJSONObject(i);
algorithms.add(RecommendationAlgorithmImpl.fromJson(currentAlgorithm));
}
return algorithms;
}
public static List<TargetRecommendation> parseRecommendations(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
ArrayList<TargetRecommendation> recommendations = new ArrayList<TargetRecommendation>();
for (int i = 0; i < jsonPayload.length(); ++i) {
JSONObject currentAlgorithm = jsonPayload.getJSONObject(i);
recommendations.add(TargetRecommendationImpl.fromJson(currentAlgorithm));
}
return recommendations;
}
public static List<ProductFeed> parseFeeds(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
ArrayList<ProductFeed> feeds = new ArrayList<ProductFeed>();
for (int i = 0; i < jsonPayload.length(); ++i) {
JSONObject currentFeed = jsonPayload.getJSONObject(i);
feeds.add(ProductFeedImpl.fromJson(currentFeed));
}
return feeds;
}
public static ProductFeed parseFeed(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
if (jsonPayload.length() > 0) {
return ProductFeedImpl.fromJson(jsonPayload.getJSONObject(0));
}
return null;
}
public static RecommendationTemplate parseTemplate(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
if (jsonPayload.length() > 0) {
return RecommendationTemplateImpl.fromJson(jsonPayload.getJSONObject(0));
}
return null;
}
public static RecommendationAlgorithm parseAlgorithm(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
if (jsonPayload.length() > 0) {
return RecommendationAlgorithmImpl.fromJson(jsonPayload.getJSONObject(0));
}
return null;
}
public static TargetRecommendation parseRecommendation(String rawJson) throws JSONException {
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
if (jsonPayload.length() > 0) {
return TargetRecommendationImpl.fromJson(jsonPayload.getJSONObject(0));
}
return null;
}
public static List<String> getJsonArrayPropValues(String rawJson, String propName) throws JSONException {
ArrayList<String> propValues = new ArrayList<String>();
JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
for (int i = 0; i < jsonPayload.length(); ++i) {
JSONObject currentObj = jsonPayload.getJSONObject(i);
propValues.add(currentObj.getString(propName));
}
return propValues;
}
public static <T> List<T> unmarshall(JSONArray jsonArray, Class<T> convertTo) throws JSONException {
ArrayList<Object> objArray = new ArrayList<Object>();
if (jsonArray != null && jsonArray.length() > 0) {
for (int idx = 0; idx < jsonArray.length(); ++idx) {
Object arrayItem = jsonArray.get(idx);
Object convertedItem = null;
if (arrayItem == null || !convertTo.isAssignableFrom(arrayItem.getClass())) continue;
convertedItem = convertTo.cast(arrayItem);
objArray.add(convertedItem);
}
}
return objArray;
}
public static JSONArray convertToJson(String rawJson) throws JSONException {
JSONArray jsonPayload = null;
if (rawJson.startsWith("{")) {
JSONObject object = new JSONObject(rawJson);
jsonPayload = new JSONArray();
jsonPayload.put((Object)object);
} else {
jsonPayload = new JSONArray(rawJson);
}
return jsonPayload;
}
}