Skip to content

Commit 286b49e

Browse files
committed
JSON simple
1 parent fa4965c commit 286b49e

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

php/php.editor/nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@
7878
<specification-version>1.0</specification-version>
7979
</run-dependency>
8080
</dependency>
81+
<dependency>
82+
<code-name-base>org.netbeans.libs.json_simple</code-name-base>
83+
<build-prerequisite/>
84+
<compile-dependency/>
85+
<run-dependency>
86+
<release-version>1</release-version>
87+
<specification-version>0.40</specification-version>
88+
</run-dependency>
89+
</dependency>
8190
<dependency>
8291
<code-name-base>org.netbeans.modules.csl.api</code-name-base>
8392
<build-prerequisite/>

php/php.editor/src/org/netbeans/modules/php/editor/model/impl/PropertyHookSignatureItem.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
import java.util.List;
2424
import java.util.logging.Level;
2525
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;
2631
import org.netbeans.modules.csl.api.OffsetRange;
2732
import org.netbeans.modules.php.api.util.StringUtils;
2833
import org.netbeans.modules.php.editor.api.elements.ParameterElement;
2934
import org.netbeans.modules.php.editor.api.elements.PropertyHookElement;
3035
import org.netbeans.modules.php.editor.elements.ParameterElementImpl;
3136
import org.netbeans.modules.php.editor.model.PropertyHookScope;
37+
import org.openide.util.Exceptions;
3238

3339
/**
3440
* Represents a JSON format object for a property hook.
@@ -47,7 +53,7 @@
4753
* }
4854
* </pre>
4955
*/
50-
public class PropertyHookSignatureItem {
56+
public class PropertyHookSignatureItem implements JSONAware {
5157

5258
private static final Logger LOGGER = Logger.getLogger(PropertyHookSignatureItem.class.getName());
5359
private static final String EMPTY_ARRAY = "[]"; // NOI18N
@@ -137,6 +143,9 @@ public static String getSignatureFromScopes(Collection<? extends PropertyHookSco
137143
List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromScopes(propertyHookScopes);
138144
String signature = EMPTY_ARRAY;
139145
if (!signatureItems.isEmpty()) {
146+
JSONArray items = new JSONArray();
147+
items.addAll(signatureItems);
148+
signature = items.toJSONString();
140149
// try {
141150
// ObjectMapper mapper = new ObjectMapper();
142151
// signature = mapper.writeValueAsString(signatureItems);
@@ -175,6 +184,9 @@ public static String getSignatureFromElements(Collection<? extends PropertyHookE
175184
List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromElements(propertyHookElements);
176185
String signature = EMPTY_ARRAY;
177186
if (!signatureItems.isEmpty()) {
187+
JSONArray items = new JSONArray();
188+
items.addAll(signatureItems);
189+
signature = items.toJSONString();
178190
// try {
179191
// ObjectMapper mapper = new ObjectMapper();
180192
// signature = mapper.writeValueAsString(signatureItems);
@@ -214,7 +226,27 @@ public static List<PropertyHookSignatureItem> fromSignature(final String signatu
214226
}
215227

216228
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+
}
218250
// try {
219251
// ObjectMapper mapper = new ObjectMapper();
220252
// signatureItems = mapper.readValue(signature, new TypeReference<List<PropertyHookSignatureItem>>() {});
@@ -287,4 +319,18 @@ public String toString() {
287319
+ ", paramSig=" + paramSig // NOI18N
288320
+ '}';
289321
}
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+
}
290336
}

0 commit comments

Comments
 (0)