Skip to content

Commit

Permalink
JSON simple
Browse files Browse the repository at this point in the history
  • Loading branch information
junichi11 committed Feb 10, 2025
1 parent fa4965c commit 286b49e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
9 changes: 9 additions & 0 deletions php/php.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
<specification-version>1.0</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.libs.json_simple</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>0.40</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.csl.api</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.netbeans.modules.csl.api.OffsetRange;
import org.netbeans.modules.php.api.util.StringUtils;
import org.netbeans.modules.php.editor.api.elements.ParameterElement;
import org.netbeans.modules.php.editor.api.elements.PropertyHookElement;
import org.netbeans.modules.php.editor.elements.ParameterElementImpl;
import org.netbeans.modules.php.editor.model.PropertyHookScope;
import org.openide.util.Exceptions;

/**
* Represents a JSON format object for a property hook.
Expand All @@ -47,7 +53,7 @@
* }
* </pre>
*/
public class PropertyHookSignatureItem {
public class PropertyHookSignatureItem implements JSONAware {

private static final Logger LOGGER = Logger.getLogger(PropertyHookSignatureItem.class.getName());
private static final String EMPTY_ARRAY = "[]"; // NOI18N
Expand Down Expand Up @@ -137,6 +143,9 @@ public static String getSignatureFromScopes(Collection<? extends PropertyHookSco
List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromScopes(propertyHookScopes);
String signature = EMPTY_ARRAY;
if (!signatureItems.isEmpty()) {
JSONArray items = new JSONArray();
items.addAll(signatureItems);
signature = items.toJSONString();
// try {
// ObjectMapper mapper = new ObjectMapper();
// signature = mapper.writeValueAsString(signatureItems);
Expand Down Expand Up @@ -175,6 +184,9 @@ public static String getSignatureFromElements(Collection<? extends PropertyHookE
List<PropertyHookSignatureItem> signatureItems = getSignatureItemsFromElements(propertyHookElements);
String signature = EMPTY_ARRAY;
if (!signatureItems.isEmpty()) {
JSONArray items = new JSONArray();
items.addAll(signatureItems);
signature = items.toJSONString();
// try {
// ObjectMapper mapper = new ObjectMapper();
// signature = mapper.writeValueAsString(signatureItems);
Expand Down Expand Up @@ -214,7 +226,27 @@ public static List<PropertyHookSignatureItem> fromSignature(final String signatu
}

final long start = (LOGGER.isLoggable(Level.FINE)) ? System.currentTimeMillis() : 0;
List<PropertyHookSignatureItem> signatureItems = List.of();
List<PropertyHookSignatureItem> signatureItems = new ArrayList<>(2);
JSONParser parser = new JSONParser();
try {
JSONArray jsonArray = (JSONArray) parser.parse(signature);
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
PropertyHookSignatureItem item = new PropertyHookSignatureItem(
(String) jsonObject.get("name"),
((Long) jsonObject.get("start")).intValue(),
((Long) jsonObject.get("end")).intValue(),
((Long) jsonObject.get("mod")).intValue(),
(Boolean) jsonObject.get("isAttr"),
(Boolean) jsonObject.get("isRef"),
(Boolean) jsonObject.get("hasBody"),
(String) jsonObject.get("paramSig")
);
signatureItems.add(item);
}
} catch (ParseException ex) {
Exceptions.printStackTrace(ex);
}
// try {
// ObjectMapper mapper = new ObjectMapper();
// signatureItems = mapper.readValue(signature, new TypeReference<List<PropertyHookSignatureItem>>() {});
Expand Down Expand Up @@ -287,4 +319,18 @@ public String toString() {
+ ", paramSig=" + paramSig // NOI18N
+ '}';
}

@Override
public String toJSONString() {
return '{'
+ "\"name\":" + "\"" + name + "\""// NOI18N
+ ",\"start\":" + start // NOI18N
+ ",\"end\":" + end // NOI18N
+ ",\"mod\":" + mod // NOI18N
+ ",\"isRef\":" + (isRef ? "true" : "false") // NOI18N
+ ",\"isAttr\":" + (isAttr ? "true" : "false") // NOI18N
+ ",\"hasBody\":" + (hasBody ? "true" : "false") // NOI18N
+ ",\"paramSig\":" + "\"" + paramSig + "\""// NOI18N
+ '}';
}
}

0 comments on commit 286b49e

Please sign in to comment.