To use the Javascript tree library DynaTree on my Visualforce page I create JSON data in its controller

List<DynaTreeNode> rootNodes = nodesByParentId.get(null).values();   
dynaTreeJson = JSON.serialize(rootNodes);
dynaTreeJson = dynaTreeJson.replaceAll('select_X', 'select');

To construct the nodes I use this Apex class

public virtual class DynaTreeNode {
    public Boolean select_X = false;
    public Boolean hideCheckbox = false;

The problem is that the Javascript expect a property called select which I cannot use as a name in Apex as it is a reserved (SOQL) keyword. According to this answer I prefixed it with _X and do a replaceAll() on the output.

The problem is that I now run into expeception when used on large JSON strings.

System.LimitException: Regex too complicated Error is in expression


This is a case where instead of an Apex class with fields a Map<String, Object> can be used instead because the map keys can be any valid string. Convert the DynaTreeNode objects to Map<String, Object> objects before you serialize:

private List<Map<String, Object>> toSerializableForm(List<DynaTreeNode> rootNodes) {

Tedious I know.

(Or use Map<String, Object> everywhere instead of DynaTreeNode.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.