Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this with a custom uberspector. This lets you customize how Velocity interprets gets/sets/iterators.</p> <p>I did the exact same thing recently for jsonlib. Here's my uberspector.</p> <pre><code>package util; import java.util.Iterator; import net.sf.json.JSONArray; import org.apache.velocity.util.introspection.Info; import org.apache.velocity.util.introspection.SecureUberspector; /** * Customized Velocity introspector. Used so that FML can iterate through JSON arrays. */ public class CustomUberspector extends SecureUberspector { @Override @SuppressWarnings("rawtypes") public Iterator getIterator(Object obj, Info i) throws Exception { if (obj instanceof JSONArray) { return new JsonArrayIterator((JSONArray) obj); } else { return super.getIterator(obj, i); } } } </code></pre> <p>JsonArrayIterator is just a simple iterator through the array. if you are using a different JSON library, just customize this class.</p> <pre><code>package util; import java.util.Iterator; import net.sf.json.JSONArray; import net.sf.json.JSONException; public class JsonArrayIterator implements Iterator&lt;Object&gt; { private final JSONArray array; private int nextIndex; private final int length; public JsonArrayIterator(JSONArray array) { this.array = array; nextIndex = 0; length = array.size(); } @Override public boolean hasNext() { return nextIndex &lt; length; } @Override public Object next() { nextIndex++; try { return array.get(nextIndex - 1); } catch (JSONException e) { throw new IllegalStateException(e); } } @Override public void remove() { throw new UnsupportedOperationException(); } } </code></pre> <p>The final step is to specify the uberspector in your velocity properties.</p> <pre><code>runtime.introspector.uberspect=util.CustomUberspector </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload