public ByteBuffer Serialize(out bool hasMetaData, object obj) { if (obj == null) { hasMetaData = false; _writer.WriteUInt8(0); // null return(_writer.GetDataAndRewind()); } try { _newTypeFound = false; StoreObject(obj); _visited.Clear(); if (!_newTypeFound) { // No unknown metadata found - to be optimistic pays off hasMetaData = false; return(_writer.GetDataAndRewind()); } StoreNewDescriptors(obj); if (_typeOrDescriptor2InfoNew.Count > 0) { _writer.Rewind(); if (MergeTypesByShapeAndStoreNew()) { _typeOrDescriptor2InfoNew.Clear(); _visited.Clear(); hasMetaData = true; return(_writer.GetDataAndRewind()); } _typeOrDescriptor2InfoNew.Clear(); } _visited.Clear(); _newTypeFound = false; StoreObject(obj); if (_newTypeFound) { throw new BTDBException("Forgot descriptor or type"); } _visited.Clear(); hasMetaData = false; return(_writer.GetDataAndRewind()); } catch { _visited.Clear(); _typeOrDescriptor2InfoNew.Clear(); _writer.Rewind(); throw; } }