MCPcopy Index your code
hub / github.com/apache/fory / serialize

Method serialize

python/pyfory/_fory.py:425–467  ·  view source on GitHub ↗

Serialize a Python object to bytes. Converts the object into Fory's binary format. The serialization process automatically handles reference tracking (if enabled), type information, and nested objects. Args: obj: The object to serialize

(
        self,
        obj,
        buffer: Buffer = None,
        buffer_callback=None,
        unsupported_callback=None,
    )

Source from the content-addressed store, hash-verified

423 return self.deserialize(buffer, buffers, unsupported_objects)
424
425 def serialize(
426 self,
427 obj,
428 buffer: Buffer = None,
429 buffer_callback=None,
430 unsupported_callback=None,
431 ) -> Union[Buffer, bytes]:
432 """
433 Serialize a Python object to bytes.
434
435 Converts the object into Fory's binary format. The serialization process
436 automatically handles reference tracking (if enabled), type information,
437 and nested objects.
438
439 Args:
440 obj: The object to serialize
441 buffer: Optional pre-allocated buffer to write to. If None, uses internal buffer
442 buffer_callback: Optional callback for out-of-band buffer serialization
443 unsupported_callback: Optional callback for handling unsupported types
444
445 Returns:
446 Serialized bytes if buffer is None, otherwise returns the provided buffer
447
448 Example:
449 >>> fory = Fory(xlang=False)
450 >>> data = fory.serialize({"key": "value", "num": 42})
451 >>> print(type(data))
452 <class 'bytes'>
453 """
454 try:
455 write_buffer = self._serialize(
456 obj,
457 buffer,
458 buffer_callback=buffer_callback,
459 unsupported_callback=unsupported_callback,
460 )
461 if write_buffer is not self.buffer:
462 return write_buffer
463 if write_buffer.get_output_stream() is not None:
464 return write_buffer
465 return write_buffer.to_bytes(0, write_buffer.get_writer_index())
466 finally:
467 self.reset_write()
468
469 def _serialize(
470 self,

Calls 3

_serializeMethod · 0.95
reset_writeMethod · 0.95
to_bytesMethod · 0.45