class ViewModel::SerializeContext

def self.shared_context_class

def self.shared_context_class
  SharedContext
end

def extract_referenced_views!

Return viewmodels referenced during serialization and clear @references.
def extract_referenced_views!
  refs = references.each.to_h
  references.clear!
  refs
end

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

def serialize_references_to_hash

def serialize_references_to_hash
  Jbuilder.new { |json| serialize_references(json) }.attributes!
end