Documentation
¶
Overview ¶
Package poly provides utilities for handling polymorphic JSON serialization and deserialization. It allows registering interfaces and their implementations, then automatically sets discriminant fields during marshaling and creates appropriate concrete types during unmarshaling.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Poly ¶
type Poly struct {
// contains filtered or unexported fields
}
Poly manages the registration of interfaces and their implementations for polymorphic JSON handling
func (*Poly) BeforeMarshalJSON ¶
BeforeMarshalJSON prepares a value for JSON marshaling by setting discriminant fields Call this before json.Marshal to ensure interface implementations are correctly tagged
func (*Poly) BeforeUnmarshalJSON ¶
BeforeUnmarshalJSON prepares a value for JSON unmarshaling by creating appropriate concrete types Call this before json.Unmarshal to ensure interface fields get the correct concrete implementations ptr: pointer to the value to populate buf: the JSON bytes to parse
func (*Poly) RegisterInterface ¶
RegisterInterface registers an interface type for polymorphic handling iFacePtr: a pointer to the interface type (e.g., (*Shape)(nil)) discriminantFieldName: the JSON field name used to distinguish implementations (e.g., "type") discriminantFieldParser: a function to parse the discriminant field value from raw JSON
func (*Poly) RegisterStruct ¶
RegisterStruct registers a struct implementation for an interface iFacePtr: a pointer to the interface type (e.g., (*Shape)(nil)) structPtr: a pointer to the struct type (e.g., (*Circle)(nil)) value: the discriminant value for this struct (e.g., "circle")