Module: JSONC

Defined in:
lib/jsonc.rb,
lib/jsonc/parser.rb,
lib/jsonc/version.rb,
lib/jsonc/parser/comment_remover.rb,
lib/jsonc/parser/trailing_comma_remover.rb

Defined Under Namespace

Classes: Error, Parser

Constant Summary collapse

DEFAULT_MAX_BYTES =

10MB

10_485_760
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.load_file(path, **opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jsonc.rb', line 23

def self.load_file(path, **opts)
  max_bytes = opts.delete(:max_bytes) || DEFAULT_MAX_BYTES
  file_size = File.size(path)

  if file_size > max_bytes
    raise JSON::ParserError,
          "file too large (#{file_size} bytes, max #{max_bytes} bytes)"
  end

  parse(File.read(path), **opts)
end

.parse(string, **opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/jsonc.rb', line 12

def self.parse(string, **opts)
  max_bytes = opts.delete(:max_bytes) || DEFAULT_MAX_BYTES
  if string.bytesize > max_bytes
    raise JSON::ParserError,
          "input string too large (#{string.bytesize} bytes, max #{max_bytes} bytes)"
  end

  sanitized_string = Parser.parse(string)
  JSON.parse(sanitized_string, **opts)
end