Module: JSON

Defined in:
lib/nosj/json.rb

Overview

Reopened by require "nosj/json" to reroute the module functions through NOSJ; behavior is documented on the require and in the README, not here.

Instance Method Summary collapse

Instance Method Details

#dump(obj, an_io = nil, limit = nil, kwargs = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nosj/json.rb', line 128

def dump(obj, an_io = nil, limit = nil, kwargs = nil)
  # Fast path for the common shapes, dump(obj) and dump(obj, opts
  # hash), mirroring gem json: dump defaults merged under the
  # user's options, NestingError surfaced as ArgumentError. IO and
  # limit arguments take gem json's own dump.
  if limit.nil? && kwargs.nil? && (an_io.nil? || an_io.instance_of?(Hash))
    opts = _dump_default_options
    opts = opts.merge(an_io) if an_io
    if NOSJ::JSONDropIn.supported?(opts, NOSJ::JSONDropIn::GENERATE_OPTS)
      begin
        return NOSJ::JSONDropIn.generate(obj, opts, false)
      rescue ::JSON::NestingError
        raise ArgumentError, "exceed depth limit"
      end
    end
  end
  nosj_original_dump(obj, an_io, limit, kwargs)
end

#generate(obj, opts = nil) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/nosj/json.rb', line 112

def generate(obj, opts = nil)
  if NOSJ::JSONDropIn.supported?(opts, NOSJ::JSONDropIn::GENERATE_OPTS)
    NOSJ::JSONDropIn.generate(obj, opts, false)
  else
    nosj_original_generate(obj, opts)
  end
end

#nosj_original_dumpObject



102
# File 'lib/nosj/json.rb', line 102

alias_method :nosj_original_dump, :dump

#nosj_original_generateObject



100
# File 'lib/nosj/json.rb', line 100

alias_method :nosj_original_generate, :generate

#nosj_original_parseObject



99
# File 'lib/nosj/json.rb', line 99

alias_method :nosj_original_parse, :parse

#nosj_original_pretty_generateObject



101
# File 'lib/nosj/json.rb', line 101

alias_method :nosj_original_pretty_generate, :pretty_generate

#parse(source, opts = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/nosj/json.rb', line 104

def parse(source, opts = nil)
  if NOSJ::JSONDropIn.supported?(opts, NOSJ::JSONDropIn::PARSE_OPTS)
    NOSJ::JSONDropIn.parse(source, opts)
  else
    nosj_original_parse(source, opts)
  end
end

#pretty_generate(obj, opts = nil) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/nosj/json.rb', line 120

def pretty_generate(obj, opts = nil)
  if NOSJ::JSONDropIn.supported?(opts, NOSJ::JSONDropIn::GENERATE_OPTS)
    NOSJ::JSONDropIn.generate(obj, opts, true)
  else
    nosj_original_pretty_generate(obj, opts)
  end
end