Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLocator = reflect.TypeOf(GenericTypeLocator{})
DefaultLocator is the type of the default TypeLocator that is used in the simpler implementation of the unmarshaller.
Functions ¶
func Flatten ¶
Flatten takes an input object of any type and flattens the input object by extracting its fields and appending them to a slice. For fields of slice types, the function appends individual non-zero elements of the slice to the resulting flattened slice. The function also sorts the flattened slice based on the index of the elements if they implement the IndexGettable interface which can be used to control the ordering of the resultant JSON objects. If there are multiple objects that have the same index, they are sorted together with the internal order based on when they were first encountered. Any object that does not implement the IndexGettable interface will be sorted to the end using the same rules.
This does not marshal them into JSON, unlike Marshal, and can be used if there is a need to do any custom JSON serialization by your own code.
As in Marshal, the objects, when serialized to JSON by your code will need to have a field indicating the polymorphic type if you need that represented in the output JSON.
Parameters: - obj (any): The input object to be serialized. This can be a value or a pointer of any type.
Returns: - ([]any): A flattened representation of the input object with all the fields of the original object returned as a slice.
func Marshal ¶ added in v0.2.0
Marshal takes an input object of any type and serializes it into a JSON byte array. The function flattens the input object by extracting its fields and appending them to a slice. For fields of slice types, the function appends individual non-zero elements of the slice to the resulting flattened slice. The function also sorts the flattened slice based on the index of the elements if they implement the IndexGettable interface which can be used to control the ordering of the resultant JSON objects. If there are multiple objects that have the same index, they are sorted together with the internal order based on when they were first encountered. Any object that does not implement the IndexGettable interface will be sorted to the end using the same rules.
Note that this only serialized the objects to JSON using the default JSON marshalling. This means that if you want to have a JSON value that can be used to determine the type of object when the JSON is being deserialized, the appropriate field must be present. This does not magically add any type resolution to the output JSON objects.
Parameters: - obj (any): The input object to be serialized. This can be a value or a pointer of any type.
Returns: - ([]byte, error): A JSON byte array representing the serialized input object, and an error if any occurs during the marshalling process.
The function is designed to be flexible and support a wide range of input types. The resulting JSON byte array is a representation of the input object's fields and their values, with nested structures being flattened and sorted based on the index provided by the IndexGettable interface. This function is useful for situations where a more compact or custom JSON representation is desired for complex data structures.
func Unmarshal ¶ added in v0.2.0
Unmarshal is a convenience function that takes a raw JSON byte slice and a target any type variable, and unmarshalls the JSON into the target variable based on the default polymorphism rules. The target variable should be a struct with fields tagged with their respective polymorphic type names. The default polymorphism rules are defined by the DefaultLocator, which implements some common polymorphic type resolutions using "type", "@type", "Type", and "@Type" as the keys to determine the type of object based on the JSON. If your needs are different, you can define a custom TypeLocator which handles the type resolution in whatever way is needed for your application.
This function is a wrapper around UnmarshalCustom, using the DefaultLocator for type resolution. If an error occurs during unmarshalling, it returns an error.
Example usage:
type Dog struct { ... } type Cat struct { ... } type Owner struct { ... } type Result struct { Dogs []Dog `poly:"dog"` Cats []Cat `poly:"cat"` Owner Owner `poly:"owner"` } var result Result err := Unmarshal(jsonData, &result)
In this example, the Unmarshal function would unmarshall the JSON into the Result struct, populating the Dogs and Cats slices based on the polymorphic type names defined in the DefaultLocator struct.
func UnmarshalCustom ¶ added in v0.2.0
UnmarshalCustom takes a raw JSON byte slice, a target any type variable, and a typeLocator of type reflect.Type. It unmarshalls the JSON into the target variable based on the custom type polymorphism rules defined by the typeLocator. The target variable should be a struct with fields tagged with their respective polymorphic type names. The typeLocator should be a struct implementing the TypeLocator interface which returns a type name for the current object. If an error occurs during unmarshalling, it returns an error.
Example usage:
type Dog struct { ... } type Cat struct { ... } type Owner struct { ... } type AnimalTypeLocator struct { ... } func (tl *AnimalTypeLocator) TypeName() string { ... } type Result struct { Dogs []Dog `poly:"dog"` Cats []Cat `poly:"cat"` Owner Owner `poly:"owner"` } var result Result err := UnmarshalCustom(jsonData, &result, reflect.Type(AnimalTypeLocator{})
In this example, the UnmarshalCustom function would unmarshal the JSON into the Result struct, populating the Dogs and Cats slices based on the polymorphic type names defined in the TypeLocator struct.
Types ¶
type GenericTypeLocator ¶
type GenericTypeLocator struct { Type string `json:"type,omitempty"` TypeAt string `json:"@type,omitempty"` TypeCaps string `json:"Type,omitempty"` TypeAtCaps string `json:"@Type,omitempty"` }
GenericTypeLocator provides a default implementation of the TypeLocator that handles common cases.
func (*GenericTypeLocator) TypeName ¶
func (t *GenericTypeLocator) TypeName() string
TypeName returns the name of the generic type represented by the receiver.
type IndexGettable ¶
type IndexGettable interface {
GetIndex() int
}
type IndexSettable ¶
type IndexSettable interface { // SetIndex is called with the zero-based index into the JSON sub-object. // In cases where there are sub-objects that cannot be unmarshalled, those // still count in the indexing. SetIndex(index int) }
IndexSettable is an interface that you should implement if you need to know the index into the array of JSON sub-objects. This should be implemented by the types that are referred to by the TypeLocator.
type TypeLocator ¶
type TypeLocator interface { // TypeName returns the name of the generic type needed to satisfy the // polymorphic unmarshalling. TypeName() string }
TypeLocator needs to be implemented by whatever pre-deserializing type that is used to determine what is the actual type that we should create for each sub-object of whatever JSON array we're unmarshalling. Whatever `struct` you make that implements this should have the correct JSON members that will be used to determine which object to create to properly unmarshal the object.