@SuppressWarnings("unchecked")
  public static void writeObject(IAObject obj, DataOutput dataOutput) throws HyracksDataException {
    switch (obj.getType().getTypeTag()) {
      case RECORD:
        {
          IARecordBuilder recordBuilder = new RecordBuilder();
          recordBuilder.reset((ARecordType) obj.getType());
          recordBuilder.init();
          writeRecord((AMutableRecord) obj, dataOutput, recordBuilder);
          break;
        }

      case ORDEREDLIST:
        {
          OrderedListBuilder listBuilder = new OrderedListBuilder();
          listBuilder.reset((AOrderedListType) ((AMutableOrderedList) obj).getType());
          IACursor cursor = ((AMutableOrderedList) obj).getCursor();
          ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage();
          while (cursor.next()) {
            listItemValue.reset();
            IAObject item = cursor.get();
            writeObject(item, listItemValue.getDataOutput());
            listBuilder.addItem(listItemValue);
          }
          listBuilder.write(dataOutput, true);
          break;
        }

      case UNORDEREDLIST:
        {
          UnorderedListBuilder listBuilder = new UnorderedListBuilder();
          listBuilder.reset((AUnorderedListType) ((AMutableUnorderedList) obj).getType());
          IACursor cursor = ((AMutableUnorderedList) obj).getCursor();
          ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage();
          while (cursor.next()) {
            listItemValue.reset();
            IAObject item = cursor.get();
            writeObject(item, listItemValue.getDataOutput());
            listBuilder.addItem(listItemValue);
          }
          listBuilder.write(dataOutput, true);
          break;
        }

      default:
        AqlSerializerDeserializerProvider.INSTANCE
            .getSerializerDeserializer(obj.getType())
            .serialize(obj, dataOutput);
        break;
    }
  }
  private void parseUnorderedList(AUnorderedListType uoltype, DataOutput out)
      throws IOException, AsterixException, AdmLexerException {
    ArrayBackedValueStorage itemBuffer = getTempBuffer();
    UnorderedListBuilder unorderedListBuilder = (UnorderedListBuilder) getUnorderedListBuilder();

    IAType itemType = null;

    if (uoltype != null) {
      itemType = uoltype.getItemType();
    }
    unorderedListBuilder.reset(uoltype);

    int token;
    boolean inList = true;
    boolean expectingListItem = false;
    boolean first = true;
    do {
      token = admLexer.next();
      if (token == AdmLexer.TOKEN_END_RECORD) {
        if (admLexer.next() == AdmLexer.TOKEN_END_RECORD) {
          if (expectingListItem) {
            throw new ParseException("Found END_COLLECTION while expecting a list item.");
          } else {
            inList = false;
          }
        } else {
          throw new ParseException("Found END_RECORD while expecting a list item.");
        }
      } else if (token == AdmLexer.TOKEN_COMMA) {
        if (first) {
          throw new ParseException("Found COMMA before any list item.");
        }
        if (expectingListItem) {
          throw new ParseException("Found COMMA while expecting a list item.");
        }
        expectingListItem = true;
      } else {
        expectingListItem = false;
        itemBuffer.reset();
        admFromLexerStream(token, itemType, itemBuffer.getDataOutput(), false);
        unorderedListBuilder.addItem(itemBuffer);
      }
      first = false;
    } while (inList);
    unorderedListBuilder.write(out, true);
  }