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

Method Serialize

go/fory/fory.go:531–552  ·  view source on GitHub ↗

============================================================================ Serialization Method API ============================================================================ Serialize serializes polymorphic values where concrete type is unknown. Uses runtime type dispatch to find the appropriat

(value any)

Source from the content-addressed store, hash-verified

529//
530// For thread-safe usage, use threadsafe.Fory which copies the data internally.
531func (f *Fory) Serialize(value any) ([]byte, error) {
532 defer f.resetWriteState()
533 // WriteData protocol header
534 writeHeader(f.writeCtx, f.config)
535
536 // Serialize the value - TypeMeta is written inline using streaming protocol
537 reflValue := reflect.ValueOf(value)
538 // if it's struct kind, must be pointer to struct, otherwise error
539 if reflValue.Kind() == reflect.Struct {
540 reflType := reflValue.Type()
541 if reflType != dateReflectType && reflType != timeReflectType {
542 return nil, fmt.Errorf("Serialize struct %s directly is disallowed, use pointer to struct (*%s) instead",
543 reflValue.Type(), reflValue.Type())
544 }
545 }
546 f.writeCtx.WriteValue(reflValue, RefModeTracking, true)
547 if f.writeCtx.HasError() {
548 return nil, f.writeCtx.TakeError()
549 }
550
551 return f.writeCtx.buffer.GetByteSlice(0, f.writeCtx.buffer.writerIndex), nil
552}
553
554// Deserialize deserializes data directly into the provided target value.
555// The target must be a pointer to the value to deserialize into.

Callers 15

TestEvolvingRoundTripFunction · 0.95
TestFloat16SliceFunction · 0.95
TestBFloat16SliceFunction · 0.95
TestIntSliceFunction · 0.95
TestUintSliceFunction · 0.95
TestInt8SliceFunction · 0.95
TestInt16SliceFunction · 0.95
TestInt32SliceFunction · 0.95
TestInt64SliceFunction · 0.95
TestUint16SliceFunction · 0.95
TestUint32SliceFunction · 0.95
TestUint64SliceFunction · 0.95

Calls 8

resetWriteStateMethod · 0.95
writeHeaderFunction · 0.85
KindMethod · 0.80
WriteValueMethod · 0.80
GetByteSliceMethod · 0.80
TypeMethod · 0.45
HasErrorMethod · 0.45
TakeErrorMethod · 0.45

Tested by 15

TestEvolvingRoundTripFunction · 0.76
TestFloat16SliceFunction · 0.76
TestBFloat16SliceFunction · 0.76
TestIntSliceFunction · 0.76
TestUintSliceFunction · 0.76
TestInt8SliceFunction · 0.76
TestInt16SliceFunction · 0.76
TestInt32SliceFunction · 0.76
TestInt64SliceFunction · 0.76
TestUint16SliceFunction · 0.76
TestUint32SliceFunction · 0.76
TestUint64SliceFunction · 0.76