Closed dragonis41 closed 4 months ago
Hey there @dragonis41!
It seems like what's needed to fulfill your request is incorporating JSON management into the library. While json.Marshal can handle standard field marshaling, the challenge arises when dealing with custom types containing private content, making JSON marshaling not straightforward. However, we can address this by implementing custom marshal behavior for all the custom types in go-odoo by utilizing https://pkg.go.dev/encoding/json#Marshaler, and optionally https://pkg.go.dev/encoding/json#Unmarshaler if bidirectional conversion is necessary.
For instance, we can take the String type as an example and implement its MarshalJSON
method like so:
// Get *String value.
func (s *String) MarshalJSON() ([]byte, error) {
if s == nil {
return nil, nil
}
return json.Marshal(s.v), nil
}
If there's a need to change field names, say from StateId to state_id, to respect standard json naming, we can achieve this by adding json:"state_id" tags in the model code. It would be even better to integrate this tag into the default template, ensuring its inclusion for all generated models.
I hope this explanation clarifies things. Feel free to ask any questions. Your contribution is welcome and I would be happy to help you with that ! :-)
Hello ! Thanks for the explanation, I don't know why I did not think about this ^^' I was focus on the .Get() function.
Should I close this issue or leave it open for a future implementation of the un/marshaling function ?
Closing it and feel free to contribute through a pull request !
Hi,
When I want to get a partner I use a function like this :
But I can't figure out how to convert the returned partner object to JSON. Normally, if I want to get the partner name, I need to use
partner.name.get()
, but how can I do this on all fields of the respartner struct ? If I do a dumbjsondata, _ := json.Marshal(&partner)
, the content of jsondata will be only the keys without the value except for Many2One field. Is it possible to get all fields content before converting it to JSON ?In the following json, the name is empty, but partner.Name.Get() give me the right name.