@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 parseOrderedList(AOrderedListType oltype, DataOutput out)
      throws IOException, AsterixException, AdmLexerException {
    ArrayBackedValueStorage itemBuffer = getTempBuffer();
    OrderedListBuilder orderedListBuilder = (OrderedListBuilder) getOrderedListBuilder();

    IAType itemType = null;
    if (oltype != null) {
      itemType = oltype.getItemType();
    }
    orderedListBuilder.reset(oltype);

    int token;
    boolean inList = true;
    boolean expectingListItem = false;
    boolean first = true;
    do {
      token = admLexer.next();
      if (token == AdmLexer.TOKEN_END_ORDERED_LIST) {
        if (expectingListItem) {
          throw new ParseException("Found END_COLLECTION while expecting a list item.");
        }
        inList = false;
      } 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);
        orderedListBuilder.addItem(itemBuffer);
      }
      first = false;
    } while (inList);
    orderedListBuilder.write(out, true);
  }