============================================================================ Generic Serialization API ============================================================================ Serialize - type T inferred, serializer auto-resolved. The serializer handles its own ref/type info writing internally.
(f *Fory, value T)
| 860 | // |
| 861 | // For thread-safe usage, use threadsafe.Serialize which copies the data internally. |
| 862 | func Serialize[T any](f *Fory, value T) ([]byte, error) { |
| 863 | defer f.resetWriteState() |
| 864 | // WriteData protocol header |
| 865 | writeHeader(f.writeCtx, f.config) |
| 866 | |
| 867 | // Fast path: type switch for common types (Go compiler can optimize this) |
| 868 | v := any(value) |
| 869 | var err error |
| 870 | switch val := v.(type) { |
| 871 | case bool: |
| 872 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 873 | f.writeCtx.WriteTypeId(BOOL) |
| 874 | f.writeCtx.buffer.WriteBool(val) |
| 875 | case int8: |
| 876 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 877 | f.writeCtx.WriteTypeId(INT8) |
| 878 | f.writeCtx.buffer.WriteInt8(val) |
| 879 | case int16: |
| 880 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 881 | f.writeCtx.WriteTypeId(INT16) |
| 882 | f.writeCtx.buffer.WriteInt16(val) |
| 883 | case int32: |
| 884 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 885 | f.writeCtx.WriteTypeId(VARINT32) |
| 886 | f.writeCtx.buffer.WriteVarint32(val) |
| 887 | case int64: |
| 888 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 889 | f.writeCtx.WriteTypeId(VARINT64) |
| 890 | f.writeCtx.buffer.WriteVarint64(val) |
| 891 | case int: |
| 892 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 893 | if strconv.IntSize == 64 { |
| 894 | f.writeCtx.WriteTypeId(VARINT64) |
| 895 | f.writeCtx.buffer.WriteVarint64(int64(val)) |
| 896 | } else { |
| 897 | f.writeCtx.WriteTypeId(VARINT32) |
| 898 | f.writeCtx.buffer.WriteVarint32(int32(val)) |
| 899 | } |
| 900 | case float32: |
| 901 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 902 | f.writeCtx.WriteTypeId(FLOAT32) |
| 903 | f.writeCtx.buffer.WriteFloat32(val) |
| 904 | case float64: |
| 905 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 906 | f.writeCtx.WriteTypeId(FLOAT64) |
| 907 | f.writeCtx.buffer.WriteFloat64(val) |
| 908 | case Decimal: |
| 909 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 910 | f.writeCtx.WriteTypeId(DECIMAL) |
| 911 | writeDecimalParts(f.writeCtx.buffer, val.Scale, &val.Unscaled) |
| 912 | case string: |
| 913 | f.writeCtx.buffer.WriteInt8(NotNullValueFlag) |
| 914 | f.writeCtx.WriteTypeId(STRING) |
| 915 | f.writeCtx.buffer.WriteVarUint32(uint32(len(val))) |
| 916 | if len(val) > 0 { |
| 917 | f.writeCtx.buffer.WriteBinary(unsafe.Slice(unsafe.StringData(val), len(val))) |
| 918 | } |
| 919 | case []byte: |