`serializable_hash` vs `as_json` - which is the user-facing way to control JSON serialization?

Looking into active support’s JSON extension, this code uses as_json to figure out how to serialize an object to JSON. A class could override as_json, calling super(...) to customize what is part of the JSON eventually generated by to_json.

However, it also seems that serializable_hash does this, but I’m not able to figure out exactly how the two relate. serializable_hash is documented—as_json is not—so is this the right way?

What I’m wondering is what is the official Rails Way to manage the way an object (usually an Active Record or Active Model) is turned into JSON.

(What I don’t need is third party ways to do this - I want to know which of these two methods is the “right” way in vanilla Rails to do this)

1 Like