Please check implementation from jbuilder. https://github.com/rails/jbuilder/blob/main/lib/jbuilder.rb#L183-L225