OSDN Git Service

Add strong typed fixed length array to UBJsonReader
authorXoppa <contact@xoppa.nl>
Sat, 1 Jun 2013 23:08:39 +0000 (01:08 +0200)
committerXoppa <contact@xoppa.nl>
Sat, 1 Jun 2013 23:08:39 +0000 (01:08 +0200)
gdx/src/com/badlogic/gdx/utils/UBJsonReader.java

index aae63e9..ab76471 100644 (file)
@@ -77,7 +77,10 @@ public class UBJsonReader implements BaseJsonReader {
                        return new JsonValue(din.readDouble());
                else if (type == 's' || type == 'S')
                        return new JsonValue(parseString(din, type));
-               return null;
+               else if (type == 'a' || type == 'A')
+                       return parseData(din, type);
+               else
+                       throw new GdxRuntimeException("Unrecognized data type");
        }
        
        protected JsonValue parseArray(final DataInputStream din) throws IOException {
@@ -122,6 +125,27 @@ public class UBJsonReader implements BaseJsonReader {
                return result;
        }
        
+       protected JsonValue parseData(final DataInputStream din, final byte blockType) throws IOException {
+               // FIXME: h/H is currently not following the specs because it lacks strong typed, fixed sized containers, 
+               // see: https://github.com/thebuzzmedia/universal-binary-json/issues/27
+               final byte dataType = din.readByte();
+               final long size = blockType == 'A' ? readUInt(din) : (long)readUChar(din);
+               final JsonValue result = new JsonValue(JsonValue.ValueType.array);
+               JsonValue prev = null;
+               for (long i = 0; i < size; i++) {
+                       final JsonValue val = parse(din, dataType);
+                       if (prev != null) {
+                               prev.next = val;
+                               result.size++;
+                       } else {
+                               result.child = val;
+                               result.size = 1;
+                       }
+                       prev = val;
+               }
+               return result;
+       }
+       
        protected String parseString(final DataInputStream din, final byte type) throws IOException {
                return readString(din, (type == 's') ? (long)readUChar(din) : readUInt(din));
        }