module Bootsnap::CompileCache::JSON

Attributes

cache_dir[R]
msgpack_factory[RW]
supported_options[RW]

Public Class Methods

cache_dir=(cache_dir) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 12
def cache_dir=(cache_dir)
  @cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}json" : "#{cache_dir}-json"
end
init!() click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 48
def init!
  require("json")
  require("msgpack")

  self.msgpack_factory = MessagePack::Factory.new
  self.supported_options = [:symbolize_names]
  if supports_freeze?
    self.supported_options = [:freeze]
  end
  supported_options.freeze
end
input_to_output(data, kwargs) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 28
def input_to_output(data, kwargs)
  ::JSON.parse(data, **(kwargs || {}))
end
input_to_storage(payload, _) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 16
def input_to_storage(payload, _)
  obj = ::JSON.parse(payload)
  msgpack_factory.dump(obj)
end
install!(cache_dir) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 40
def install!(cache_dir)
  self.cache_dir = cache_dir
  init!
  if ::JSON.respond_to?(:load_file)
    ::JSON.singleton_class.prepend(Patch)
  end
end
precompile(path) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 32
def precompile(path)
  Bootsnap::CompileCache::Native.precompile(
    cache_dir,
    path.to_s,
    self,
  )
end
storage_to_output(data, kwargs) click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 21
def storage_to_output(data, kwargs)
  if kwargs&.key?(:symbolize_names)
    kwargs[:symbolize_keys] = kwargs.delete(:symbolize_names)
  end
  msgpack_factory.load(data, kwargs)
end

Private Class Methods

supports_freeze?() click to toggle source
# File lib/bootsnap/compile_cache/json.rb, line 62
def supports_freeze?
  ::JSON.parse('["foo"]', freeze: true).first.frozen? &&
    MessagePack.load(MessagePack.dump("foo"), freeze: true).frozen?
end