|
23 | 23 | import java.util.List; |
24 | 24 | import java.util.logging.Level; |
25 | 25 | import java.util.logging.Logger; |
| 26 | +import org.json.simple.JSONArray; |
| 27 | +import org.json.simple.JSONAware; |
| 28 | +import org.json.simple.JSONObject; |
| 29 | +import org.json.simple.parser.JSONParser; |
| 30 | +import org.json.simple.parser.ParseException; |
26 | 31 | import org.netbeans.modules.csl.api.OffsetRange; |
27 | 32 | import org.netbeans.modules.php.api.util.StringUtils; |
28 | 33 | import org.netbeans.modules.php.editor.api.elements.ParameterElement; |
29 | 34 | import org.netbeans.modules.php.editor.api.elements.PropertyHookElement; |
30 | 35 | import org.netbeans.modules.php.editor.elements.ParameterElementImpl; |
31 | 36 | import org.netbeans.modules.php.editor.model.PropertyHookScope; |
| 37 | +import org.openide.util.Exceptions; |
32 | 38 |
|
33 | 39 | /** |
34 | 40 | * Represents a JSON format object for a property hook. |
|
47 | 53 | * } |
48 | 54 | * </pre> |
49 | 55 | */ |
50 | | -public class PropertyHookSignatureItem { |
| 56 | +public class PropertyHookSignatureItem implements JSONAware { |
51 | 57 |
|
52 | 58 | private static final Logger LOGGER = Logger.getLogger(PropertyHookSignatureItem.class.getName()); |
53 | 59 | private static final String EMPTY_ARRAY = "[]"; // NOI18N |
@@ -137,6 +143,9 @@ public static String getSignatureFromScopes(Collection<? extends PropertyHookSco |
137 | 143 | List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromScopes(propertyHookScopes); |
138 | 144 | String signature = EMPTY_ARRAY; |
139 | 145 | if (!signatureItems.isEmpty()) { |
| 146 | + JSONArray items = new JSONArray(); |
| 147 | + items.addAll(signatureItems); |
| 148 | + signature = items.toJSONString(); |
140 | 149 | // try { |
141 | 150 | // ObjectMapper mapper = new ObjectMapper(); |
142 | 151 | // signature = mapper.writeValueAsString(signatureItems); |
@@ -175,6 +184,9 @@ public static String getSignatureFromElements(Collection<? extends PropertyHookE |
175 | 184 | List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromElements(propertyHookElements); |
176 | 185 | String signature = EMPTY_ARRAY; |
177 | 186 | if (!signatureItems.isEmpty()) { |
| 187 | + JSONArray items = new JSONArray(); |
| 188 | + items.addAll(signatureItems); |
| 189 | + signature = items.toJSONString(); |
178 | 190 | // try { |
179 | 191 | // ObjectMapper mapper = new ObjectMapper(); |
180 | 192 | // signature = mapper.writeValueAsString(signatureItems); |
@@ -214,7 +226,27 @@ public static List<PropertyHookSignatureItem> fromSignature(final String signatu |
214 | 226 | } |
215 | 227 |
|
216 | 228 | final long start = (LOGGER.isLoggable(Level.FINE)) ? System.currentTimeMillis() : 0; |
217 | | - List<PropertyHookSignatureItem> signatureItems = List.of(); |
| 229 | + List<PropertyHookSignatureItem> signatureItems = new ArrayList<>(2); |
| 230 | + JSONParser parser = new JSONParser(); |
| 231 | + try { |
| 232 | + JSONArray jsonArray = (JSONArray) parser.parse(signature); |
| 233 | + for (Object object : jsonArray) { |
| 234 | + JSONObject jsonObject = (JSONObject) object; |
| 235 | + PropertyHookSignatureItem item = new PropertyHookSignatureItem( |
| 236 | + (String) jsonObject.get("name"), |
| 237 | + ((Long) jsonObject.get("start")).intValue(), |
| 238 | + ((Long) jsonObject.get("end")).intValue(), |
| 239 | + ((Long) jsonObject.get("mod")).intValue(), |
| 240 | + (Boolean) jsonObject.get("isAttr"), |
| 241 | + (Boolean) jsonObject.get("isRef"), |
| 242 | + (Boolean) jsonObject.get("hasBody"), |
| 243 | + (String) jsonObject.get("paramSig") |
| 244 | + ); |
| 245 | + signatureItems.add(item); |
| 246 | + } |
| 247 | + } catch (ParseException ex) { |
| 248 | + Exceptions.printStackTrace(ex); |
| 249 | + } |
218 | 250 | // try { |
219 | 251 | // ObjectMapper mapper = new ObjectMapper(); |
220 | 252 | // signatureItems = mapper.readValue(signature, new TypeReference<List<PropertyHookSignatureItem>>() {}); |
@@ -287,4 +319,18 @@ public String toString() { |
287 | 319 | + ", paramSig=" + paramSig // NOI18N |
288 | 320 | + '}'; |
289 | 321 | } |
| 322 | + |
| 323 | + @Override |
| 324 | + public String toJSONString() { |
| 325 | + return '{' |
| 326 | + + "\"name\":" + "\"" + name + "\""// NOI18N |
| 327 | + + ",\"start\":" + start // NOI18N |
| 328 | + + ",\"end\":" + end // NOI18N |
| 329 | + + ",\"mod\":" + mod // NOI18N |
| 330 | + + ",\"isRef\":" + (isRef ? "true" : "false") // NOI18N |
| 331 | + + ",\"isAttr\":" + (isAttr ? "true" : "false") // NOI18N |
| 332 | + + ",\"hasBody\":" + (hasBody ? "true" : "false") // NOI18N |
| 333 | + + ",\"paramSig\":" + "\"" + paramSig + "\""// NOI18N |
| 334 | + + '}'; |
| 335 | + } |
290 | 336 | } |
0 commit comments