MCPcopy Create free account
hub / github.com/apache/fory / SerializeTo

Method SerializeTo

go/fory/fory.go:594–639  ·  view source on GitHub ↗

SerializeTo serializes a value and appends the bytes to the provided buffer. This is useful when you need to write multiple serialized values to the same buffer. Returns error if serialization fails.

(buf *ByteBuffer, value any)

Source from the content-addressed store, hash-verified

592// This is useful when you need to write multiple serialized values to the same buffer.
593// Returns error if serialization fails.
594func (f *Fory) SerializeTo(buf *ByteBuffer, value any) error {
595 defer f.resetWriteState()
596
597 // Temporarily swap buffer
598 origBuffer := f.writeCtx.buffer
599 f.writeCtx.buffer = buf
600
601 // Write protocol header
602 writeHeader(f.writeCtx, f.config)
603
604 // Fast path for pointer-to-struct types (bypasses ptrToValueSerializer wrapper)
605 rv := reflect.ValueOf(value)
606 if rv.Kind() == reflect.Ptr && !rv.IsNil() && rv.Elem().Kind() == reflect.Struct && !f.config.TrackRef {
607 // Get TypeInfo using fast pointer cache
608 elemValue := rv.Elem()
609 typeInfo, err := f.typeResolver.getTypeInfo(rv, true)
610 if err == nil && typeInfo != nil && typeInfo.Serializer != nil {
611 // Write not-null flag and type ID directly
612 buf.WriteInt8(NotNullValueFlag)
613 f.typeResolver.WriteTypeInfo(buf, typeInfo, f.writeCtx.Err())
614 // Call the underlying struct serializer's WriteData directly
615 if ptrSer, ok := typeInfo.Serializer.(*ptrToValueSerializer); ok {
616 ptrSer.valueSerializer.WriteData(f.writeCtx, elemValue)
617 } else {
618 typeInfo.Serializer.WriteData(f.writeCtx, elemValue)
619 }
620 if f.writeCtx.HasError() {
621 f.writeCtx.buffer = origBuffer
622 return f.writeCtx.TakeError()
623 }
624 f.writeCtx.buffer = origBuffer
625 return nil
626 }
627 }
628
629 // Standard path - TypeMeta is written inline using streaming protocol
630 f.writeCtx.WriteValue(rv, RefModeTracking, true)
631 if f.writeCtx.HasError() {
632 f.writeCtx.buffer = origBuffer
633 return f.writeCtx.TakeError()
634 }
635
636 // Restore original buffer
637 f.writeCtx.buffer = origBuffer
638 return nil
639}
640
641// DeserializeFrom deserializes data from an existing buffer directly into the provided target value.
642// The buffer's reader index is advanced as data is read.

Calls 11

resetWriteStateMethod · 0.95
writeHeaderFunction · 0.85
KindMethod · 0.80
WriteValueMethod · 0.80
WriteDataMethod · 0.65
getTypeInfoMethod · 0.45
WriteInt8Method · 0.45
WriteTypeInfoMethod · 0.45
ErrMethod · 0.45
HasErrorMethod · 0.45
TakeErrorMethod · 0.45