============================================================================ Serialization Method API ============================================================================ Serialize serializes polymorphic values where concrete type is unknown. Uses runtime type dispatch to find the appropriat
(value any)
| 529 | // |
| 530 | // For thread-safe usage, use threadsafe.Fory which copies the data internally. |
| 531 | func (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. |