Module: NOSJ
- Defined in:
- lib/nosj.rb,
lib/nosj/json.rb,
lib/nosj/lazy.rb,
lib/nosj/version.rb,
lib/nosj/multi_json.rb,
sig/nosj.rbs
Overview
Public interfaces only. The native layer (NOSJ.parse_native and friends) and the JSON drop-in's patched JSON singleton methods are implementation detail and deliberately unsigned.
Defined Under Namespace
Modules: JSONDropIn Classes: Error, GeneratorError, Lazy, MultiJsonAdapter, NestingError
Constant Summary collapse
- VERSION =
The gem version.
"0.2.0"
Class Method Summary collapse
-
.at_pointer(source, pointer, opts = nil) ⇒ Object?
Partial parsing by JSON Pointer (with the standard +~0+/+~1+ escapes).
-
.at_pointer_file(path, pointer, opts = nil) ⇒ Object?
NOSJ.at_pointer against a file: memory-maps it, resolves the pointer, materializes only the matched subtree, and never reads the rest into Ruby.
-
.at_pointers(source, pointers, opts = nil) ⇒ Array<Object, nil>
Batch NOSJ.at_pointer: the pointer-string counterpart of NOSJ.dig_many, resolving the whole set in one pass.
-
.dig(source, *path) ⇒ Object?
Partial parsing, Hash#dig-shaped: extracts one value from a JSON string without materializing the rest of the document.
-
.dig_file(path, *path_elements) ⇒ Object?
NOSJ.dig against a file: the Hash#dig-shaped counterpart of NOSJ.at_pointer_file.
-
.dig_many(source, paths, opts = nil) ⇒ Array<Object, nil>
Batch NOSJ.dig: many paths resolved in ONE pass over the document.
-
.generate(obj, opts = nil) ⇒ String
Generates JSON, JSON.generate-compatible: identical output bytes, including the gem's exact float formatting.
-
.lazy(source, opts = nil) ⇒ NOSJ::Lazy, Object
Wraps a JSON document for lazy, on-demand access: returns a Lazy node for a container root, or the materialized value for a scalar root.
-
.load_file(path, opts = nil) ⇒ Object
Parses a JSON file, like +JSON.load_file+—except the file is read natively into a reused buffer, so no file-sized Ruby String is ever created (or garbage-collected).
-
.load_lazy_file(path, opts = nil) ⇒ NOSJ::Lazy, Object
Wraps a JSON file as a lazy document (NOSJ.lazy for files): the file is memory-mapped read-only, so beyond one sequential UTF-8 check, pages you never read are never loaded from disk.
-
.parse(source, opts = nil) ⇒ Object
Parses a JSON document, JSON.parse-compatible: same values, same option names, same behavior, byte-for-byte.
-
.pretty_generate(obj, opts = nil) ⇒ String
Generates human-readable JSON, JSON.pretty_generate-compatible (two-space indent, newlines between elements).
-
.valid?(source, opts = nil) ⇒ Boolean
Validates a document without building any Ruby values: the full parser (tokenizers, string decode, number validation) runs into a null sink, 1.8-2.5x faster than NOSJ.parse.
-
.write_file(path, obj, opts = nil) ⇒ Integer
Generates
objas JSON and writes it topath, streaming the generator's buffer straight to disk—no intermediate Ruby String.
Class Method Details
.at_pointer(source, pointer, opts = nil) ⇒ Object?
Partial parsing by JSON Pointer (with the standard +~0+/+~1+ escapes). The matched subtree materializes under the same options as parse.
176 177 178 |
# File 'lib/nosj.rb', line 176 def self.at_pointer(source, pointer, opts = nil) at_pointer_native(source, pointer, opts) end |
.at_pointer_file(path, pointer, opts = nil) ⇒ Object?
at_pointer against a file: memory-maps it, resolves the pointer, materializes only the matched subtree, and never reads the rest into Ruby.
260 261 262 |
# File 'lib/nosj.rb', line 260 def self.at_pointer_file(path, pointer, opts = nil) at_pointer_file_native(path, pointer, opts) end |
.at_pointers(source, pointers, opts = nil) ⇒ Array<Object, nil>
Batch at_pointer: the pointer-string counterpart of dig_many, resolving the whole set in one pass.
192 193 194 |
# File 'lib/nosj.rb', line 192 def self.at_pointers(source, pointers, opts = nil) at_pointers_native(source, pointers, opts) end |
.dig(source, *path) ⇒ Object?
Partial parsing, Hash#dig-shaped: extracts one value from a JSON string without materializing the rest of the document. Skipped content is stepped over at SIMD block speed, so a lookup costs what it skips, not what the document weighs.
135 136 137 |
# File 'lib/nosj.rb', line 135 def self.dig(source, *path) dig_native(source, path) end |
.dig_file(path, *path_elements) ⇒ Object?
dig against a file: the Hash#dig-shaped counterpart of at_pointer_file. Negative indices resolve to nil.
273 274 275 |
# File 'lib/nosj.rb', line 273 def self.dig_file(path, *path_elements) dig_file_native(path, path_elements) end |
.dig_many(source, paths, opts = nil) ⇒ Array<Object, nil>
Batch dig: many paths resolved in ONE pass over the document. The walk descends only into subtrees some path still needs, so a batch costs about as much as its single deepest lookup.
On malformed documents a batch may raise where a single dig would
return nil: one pass scans every byte some path needs.
157 158 159 |
# File 'lib/nosj.rb', line 157 def self.dig_many(source, paths, opts = nil) dig_many_native(source, paths, opts) end |
.generate(obj, opts = nil) ⇒ String
Generates JSON, JSON.generate-compatible: identical output bytes, including the gem's exact float formatting. Implemented natively (no Ruby forwarder frame; the definition lives in the extension).
|
|
# File 'lib/nosj.rb', line 66
|
.lazy(source, opts = nil) ⇒ NOSJ::Lazy, Object
Wraps a JSON document for lazy, on-demand access: returns a Lazy node for a container root, or the materialized value for a scalar root. The document bytes are copied once; no parsing happens beyond locating the root value.
162 163 164 |
# File 'lib/nosj/lazy.rb', line 162 def self.lazy(source, opts = nil) lazy_native(source, opts) end |
.load_file(path, opts = nil) ⇒ Object
Parses a JSON file, like +JSON.load_file+—except the file is read natively into a reused buffer, so no file-sized Ruby String is ever created (or garbage-collected).
208 209 210 |
# File 'lib/nosj.rb', line 208 def self.load_file(path, opts = nil) load_file_native(path, opts) end |
.load_lazy_file(path, opts = nil) ⇒ NOSJ::Lazy, Object
Wraps a JSON file as a lazy document (lazy for files): the file is memory-mapped read-only, so beyond one sequential UTF-8 check, pages you never read are never loaded from disk. The mapping lives as long as any node on it; the file must not be modified while it is in use.
244 245 246 |
# File 'lib/nosj.rb', line 244 def self.load_lazy_file(path, opts = nil) load_lazy_file_native(path, opts) end |
.parse(source, opts = nil) ⇒ Object
Parses a JSON document, JSON.parse-compatible: same values, same option names, same behavior, byte-for-byte.
The json gem's legacy object-deserialization options
(+object_class+, array_class, decimal_class,
create_additions) are deliberately unsupported and raise
ArgumentError.
62 63 64 |
# File 'lib/nosj.rb', line 62 def self.parse(source, opts = nil) parse_native(source, opts) end |
.pretty_generate(obj, opts = nil) ⇒ String
Generates human-readable JSON, JSON.pretty_generate-compatible (two-space indent, newlines between elements). Options override the pretty defaults and are otherwise the same as generate.
93 94 95 96 |
# File 'lib/nosj.rb', line 93 def self.pretty_generate(obj, opts = nil) opts = opts.nil? ? PRETTY_GENERATE_OPTS : PRETTY_GENERATE_OPTS.merge(opts) generate_native(obj, opts) end |
.valid?(source, opts = nil) ⇒ Boolean
Validates a document without building any Ruby values: the full parser (tokenizers, string decode, number validation) runs into a null sink, 1.8-2.5x faster than parse.
Returns true iff NOSJ.parse(source, opts) would
succeed: parse refusals (malformed JSON, wrong encoding, too-deep
nesting) are false, while option and argument-type errors still
raise exactly like parse.
115 116 117 |
# File 'lib/nosj.rb', line 115 def self.valid?(source, opts = nil) valid_native(source, opts) end |
.write_file(path, obj, opts = nil) ⇒ Integer
Generates obj as JSON and writes it to path, streaming the
generator's buffer straight to disk—no intermediate Ruby String.
225 226 227 |
# File 'lib/nosj.rb', line 225 def self.write_file(path, obj, opts = nil) write_file_native(path, obj, opts) end |