class ViewModel::SerializeContext
def serialize_references(json)
def serialize_references(json) reference_context = self.for_references # References should be serialized in a stable order to improve caching via # naive response hash. serialized_refs = {} while references.present? extract_referenced_views!.each do |ref, value| next if serialized_refs.has_key?(ref) serialized_refs[ref] = Jbuilder.new do |j| ViewModel.serialize(value, j, serialize_context: reference_context) end end end serialized_refs.sort.each do |ref, value| json.set!(ref, value) end end