You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using the CustomMarshaler. My goal is to serialize the first-level objects normally, but for nested objects, return only type/id and queue them for delayed serialization. The challenge is to determine within the CustomMarshaler when an object is at the first level (i.e., the initial call of MarshalWithOptions to our CustomMarshaler) to serialize its simple fields (not sure how to accomplish this), while returning type/id for referenced fields.
Could anyone provide insights or approaches for handling this situation? Any help on how to effectively implement this type of serialization would be greatly appreciated.
Thank you!
The text was updated successfully, but these errors were encountered:
Currently, I have replaced the types of Dependency and Children in my structure with the IComponent interface, and it seems to be working well. This change allows CustomMarshaler[ []IComponent ] and CustomMarshaler[IComponent] to handle the lazy serialization of the Children and Dependency fields. Meanwhile, the real type A is being marshaled using reflection. However, if all types are the actual type A, the outcome is either entirely returning type/id for each or serializing them all.
I need a CustomMarshaler like this:
funcSerialize(aany) ([]byte, error) {
encodeOption:=yaml.CustomMarshaler(func(a*A) ([]byte, error) {
ifa==currentRootObject {
// FIXME: this line will cause infinite recursionreturnyaml.MarshalWithOptions(a, encodeOption)
} else {
returnyaml.Marshal(&RefObject{
Type: "A",
Id: a.ID,
})
}
})
returnyaml.MarshalWithOptions(a, encodeOption)
}
cpunion
changed the title
Need Help with Advanced Custom Serialization
Need Help with CustomMarshaler
Nov 28, 2023
Hello,
I'm working with a data structure, which is intended to be flexible and may represent various user-defined structures. Here's an example structure:
I need to serialize this tree structure in a flattened structure, all references to objects only store their type/id. For example:
I have been using the
CustomMarshaler
. My goal is to serialize the first-level objects normally, but for nested objects, return only type/id and queue them for delayed serialization. The challenge is to determine within the CustomMarshaler when an object is at the first level (i.e., the initial call ofMarshalWithOptions
to our CustomMarshaler) to serialize its simple fields (not sure how to accomplish this), while returning type/id for referenced fields.Could anyone provide insights or approaches for handling this situation? Any help on how to effectively implement this type of serialization would be greatly appreciated.
Thank you!
The text was updated successfully, but these errors were encountered: