SerializeWithCallback serializes a value to buffer (for streaming/cross-language use). The third parameter is an optional callback for buffer objects (can be nil). If callback is provided, it will be called for each BufferObject during serialization. Return true from callback to write in-band, false
(buffer *ByteBuffer, v any, callback func(BufferObject) bool)
| 693 | // If callback is provided, it will be called for each BufferObject during serialization. |
| 694 | // Return true from callback to write in-band, false for out-of-band. |
| 695 | func (f *Fory) SerializeWithCallback(buffer *ByteBuffer, v any, callback func(BufferObject) bool) error { |
| 696 | buf := f.writeCtx.buffer |
| 697 | defer func() { |
| 698 | // Reset internal state but NOT the buffer - caller manages buffer state |
| 699 | // This allows streaming multiple values to the same buffer |
| 700 | f.writeCtx.ResetState() |
| 701 | f.writeCtx.buffer = buf |
| 702 | if f.metaContext != nil { |
| 703 | f.metaContext.Reset() |
| 704 | } |
| 705 | // Set up buffer callback for out-of-band serialization |
| 706 | if callback != nil { |
| 707 | f.writeCtx.bufferCallback = nil |
| 708 | f.writeCtx.outOfBand = false |
| 709 | } |
| 710 | }() |
| 711 | f.writeCtx.buffer = buffer |
| 712 | if f.metaContext != nil { |
| 713 | f.metaContext.Reset() |
| 714 | } |
| 715 | // Set up buffer callback for out-of-band serialization |
| 716 | if callback != nil { |
| 717 | f.writeCtx.bufferCallback = callback |
| 718 | f.writeCtx.outOfBand = true |
| 719 | } |
| 720 | |
| 721 | // WriteData protocol header |
| 722 | writeHeader(f.writeCtx, f.config) |
| 723 | |
| 724 | // Serialize the value - TypeMeta is written inline using streaming protocol |
| 725 | f.writeCtx.WriteValue(reflect.ValueOf(v), RefModeTracking, true) |
| 726 | if f.writeCtx.HasError() { |
| 727 | return f.writeCtx.TakeError() |
| 728 | } |
| 729 | |
| 730 | return nil |
| 731 | } |
| 732 | |
| 733 | // DeserializeWithCallbackBuffers deserializes from buffer into the provided value (for streaming/cross-language use). |
| 734 | // The third parameter is optional external buffers for out-of-band data (can be nil). |