Parsing arrays: index out of bounds error

47 views
Skip to first unread message

David Herman

unread,
Jan 1, 2015, 6:40:33 PM1/1/15
to jsonbea...@googlegroups.com
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.

Nate

unread,
Jan 2, 2015, 8:08:30 PM1/2/15
to jsonbea...@googlegroups.com
I've updated JsonBeans to the latest from libgdx. Your example works now. Cheers!

BTW, if you run into another problem, please provide executable example code, eg:


   private final static class ShortcutData {
       public String shortcut;
       public String commandId;
   }

   private final static class ShortcutGroupData {
       public ShortcutData[] shortcuts;
   }

   public static void main (String[] args) throws Exception {
        ShortcutGroupData data = new Json().fromJson(ShortcutGroupData.class, "{\r\n" +
            "  \"shortcuts\": [\r\n" +
            "    {\r\n" +
            "      \"shortcut\": \"ctrl+N\",\r\n" +
            "      \"commandId\": \"new_file\"\r\n" +
            "    },\r\n" +
            "    {\r\n" +
            "      \"shortcut\": \"ctrl+W\",\r\n" +
            "      \"commandId\": \"close_file\"\r\n" +
            "    },\r\n" +
            "    {\r\n" +
            "      \"shortcut\": \"ctrl+Q\",\r\n" +
            "      \"commandId\": \"exit\"\r\n" +
            "    }\r\n" +
            "  ]\r\n" +
            "}");
        System.out.println(data.shortcuts.length);
    }

 

--
--
You received this message because you are subscribed to the "jsonbeans-users" group:
http://groups.google.com/group/jsonbeans-users

---
You received this message because you are subscribed to the Google Groups "jsonbeans-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonbeans-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages