+ return false;
+ }
+
+ if (can_serialize_fast(ser)) {
+ ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_BACKREF));
+ ION_CHECK_RETURN(true, ion_writer_write_int64(php_ion_obj(writer, ser->wri)->writer, Z_LVAL_P(ref)));
+ } else {
+ zval z_ann_bref;
+ ZVAL_CHAR(&z_ann_bref, 'r');
+ zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_bref);
+ ION_CATCH_RETURN(true);
+ zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeInt", NULL, ref);
+ }
+ return true;
+}
+
+static void php_ion_serialize_string(php_ion_serializer *ser, zend_string *str)
+{
+ if (can_serialize_fast(ser)) {
+ ION_STRING is;
+ ION_CHECK(ion_writer_write_string(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, str)));
+ } else {
+ zval zs;
+ ZVAL_STR(&zs, str);
+ zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeString", NULL, &zs);
+ }
+}
+
+static void php_ion_serialize_reference(php_ion_serializer *ser, zval *noref)
+{
+ if (can_serialize_fast(ser)) {
+ ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_REFERENCE));
+ } else {
+ zval z_ann_ref;
+ ZVAL_CHAR(&z_ann_ref, 'R');
+ zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_ref);
+ ION_CATCH();
+ }
+ php_ion_serialize_zval(ser, noref);
+
+}
+static void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv)
+{
+ if (php_ion_serialize_system_value(ser, zv)) {
+ return;
+ }
+ if (php_ion_serialize_backref(ser, zv)) {
+ return;
+ }
+
+ switch (Z_TYPE_P(zv)) {
+ case IS_STRING:
+ php_ion_serialize_string(ser, Z_STR_P(zv));
+ break;
+
+ case IS_ARRAY:
+ if (zend_array_is_list(Z_ARRVAL_P(zv))) {
+ php_ion_serialize_list(ser, Z_ARRVAL_P(zv));
+ } else {
+ php_ion_serialize_struct(ser, Z_ARRVAL_P(zv), false, false);
+ }
+ break;
+
+ case IS_OBJECT:
+ php_ion_serialize_object(ser, Z_OBJ_P(zv));
+ break;
+
+ case IS_REFERENCE:
+ php_ion_serialize_reference(ser, Z_REFVAL_P(zv));
+ break;
+ }
+}
+
+static void php_ion_serialize_scalar(php_ion_serializer *ser, zval *zv)
+{
+ if (can_serialize_fast(ser)) {