I'm trying out JsonBeans 0.7 after using libgdx on a separate project, but now I just need the JSON support for a non-game tool I'm working on.
However, JSON that was loading just fine for me in libgdx is crashing in JsonBeans. It looks like the bug is related to arrays of custom types, but that's just a guess at this point. It may also be due to parsing differences, as I went from using libgdx FileHandles to standard Java File loading (but inspecting the String I'm passing to JsonBeans, it looks fine).
Here's a snippet that should reproduce the error:
shortcuts.json
=============================
{
"shortcuts": [
{
"shortcut": "ctrl+N",
"commandId": "new_file"
},
{
"shortcut": "ctrl+W",
"commandId": "close_file"
},
{
"shortcut": "ctrl+Q",
"commandId": "exit"
}
]
}
=============================
ShortcutsLoader.java
=============================
public final class ShortcutsLoader
private final static class ShortcutData {
public String shortcut;
public String commandId;
}
private final static class ShortcutGroupData {
public ShortcutData[] shortcuts;
}
public static void loadTest() {
String contents = /* read shortcuts.json contents into a String */
Json json = new Json();
ShortcutGroupData shortcutGroupData = json.fromJson(ShortcutGroupData.class, contents);
}
=============================
For me, this throws an out of bounds Exception here:
=============================
if (type.isArray()) {
Class componentType = type.getComponentType();
if (elementType == null) elementType = componentType;
Object newArray = Array.newInstance(componentType, jsonData.size);
int i = 0;
for (JsonValue child = jsonData.child; child != null; child = child.next)
Array.set(newArray, i++, readValue(elementType, null, child));
return (T)newArray;
}
=============================
because jsonData.size is "1", even though the number of children I have is 3.