nosj 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/nosj.rb CHANGED
@@ -86,10 +86,13 @@ module NOSJ
86
86
  # Parses a JSON document, JSON.parse-compatible: same values, same
87
87
  # option names, same behavior, byte-for-byte.
88
88
  #
89
- # The +json+ gem's legacy object-deserialization options
90
- # (+object_class+, +array_class+, +decimal_class+,
91
- # +create_additions+) are deliberately unsupported and raise
92
- # ArgumentError.
89
+ # Options follow json 3: an unknown key raises ArgumentError
90
+ # (<code>unknown keyword: foo</code>). The json options nosj does not
91
+ # implement (+object_class+, +array_class+, +decimal_class+,
92
+ # +on_load+, +create_additions+, +allow_comments+,
93
+ # +allow_control_characters+, +allow_invalid_escape+) raise
94
+ # ArgumentError unless falsy, since their falsy default is nosj's
95
+ # behavior.
93
96
  #
94
97
  # @example
95
98
  # NOSJ.parse('{"a":[1,true]}') #=> {"a" => [1, true]}
@@ -98,12 +101,14 @@ module NOSJ
98
101
  # @param source [String] the JSON document (UTF-8 or US-ASCII)
99
102
  # @param opts [Hash, nil] +symbolize_names+, +freeze+, +max_nesting+
100
103
  # (Integer or +false+ for unlimited), +allow_nan+,
101
- # +allow_trailing_comma+
104
+ # +allow_trailing_comma+, +allow_duplicate_key+ (json 3 semantics:
105
+ # a repeated key raises unless this is true, then the last one wins)
102
106
  # @return [Object] the parsed value tree
103
- # @raise [ParserError] when the document is malformed or not UTF-8;
104
- # carries the failure position ({ParserError#line} and friends)
107
+ # @raise [ParserError] when the document is malformed, repeats a key,
108
+ # holds a lone surrogate (+"\udc00"+), or is not UTF-8; carries the
109
+ # failure position ({ParserError#line} and friends)
105
110
  # @raise [NestingError] when nesting exceeds +max_nesting+
106
- # @raise [ArgumentError] for unsupported options
111
+ # @raise [ArgumentError] for unknown or unsupported options
107
112
  def self.parse(source, opts = nil)
108
113
  parse_native(source, opts)
109
114
  end
@@ -119,12 +124,18 @@ module NOSJ
119
124
  # @param obj [Object] the value tree to serialize
120
125
  # @param opts [Hash, nil] +indent+, +space+, +space_before+,
121
126
  # +object_nl+, +array_nl+, +max_nesting+ (Integer or +false+),
122
- # +allow_nan+, +ascii_only+, +script_safe+ (alias +escape_slash+),
123
- # +strict+, +depth+, +buffer_initial_length+
127
+ # +allow_nan+, +ascii_only+, +script_safe+, +strict+, +depth+,
128
+ # +buffer_initial_length+, +allow_duplicate_key+ (json 3
129
+ # semantics: keys that render alike, like <code>"a"</code> and
130
+ # <code>:a</code>, raise unless true). As in json 3, an unknown
131
+ # key raises (+escape_slash+ is gone: use +script_safe+), and the
132
+ # unimplemented +sort_keys+ and +as_json+ raise unless falsy.
124
133
  # @return [String] the JSON document
125
134
  # @raise [GeneratorError] for non-finite floats without +allow_nan+,
126
- # unsupported objects under +strict+, or broken string encodings
135
+ # unsupported objects under +strict+, keys that render alike, or
136
+ # broken string encodings
127
137
  # @raise [NestingError] when nesting exceeds +max_nesting+
138
+ # @raise [ArgumentError] for unknown or unsupported options
128
139
 
129
140
  # Generates human-readable JSON, JSON.pretty_generate-compatible
130
141
  # (two-space indent, newlines between elements). Options override the
@@ -156,7 +167,7 @@ module NOSJ
156
167
  # @param source [String] the JSON document
157
168
  # @param opts [Hash, nil] same options as {.parse}
158
169
  # @return [Boolean]
159
- # @raise [ArgumentError] for unsupported options
170
+ # @raise [ArgumentError] for unknown or unsupported options
160
171
  def self.valid?(source, opts = nil)
161
172
  valid_native(source, opts)
162
173
  end
@@ -323,18 +334,19 @@ module NOSJ
323
334
  # Minifies a document without building any Ruby values: the parser's
324
335
  # events pipe straight into the emission kernels, SIMD in and SIMD
325
336
  # out. Output is exactly what <code>generate(parse(json))</code>
326
- # would produce, except duplicate object keys pass through instead of
327
- # being collapsed (a reformatter must not silently drop data).
328
- # Numbers come out in the canonical spelling (+1.50+ becomes +1.5+)
329
- # and string escapes are normalized.
337
+ # would produce, and it accepts exactly what {.parse} accepts; under
338
+ # +allow_duplicate_key+, repeated keys pass through instead of being
339
+ # collapsed (a reformatter must not silently drop data). Numbers come
340
+ # out in the canonical spelling (+1.50+ becomes +1.5+) and string
341
+ # escapes are normalized.
330
342
  #
331
343
  # @example
332
344
  # NOSJ.minify(%({ "a": [1, 2],\n "b": "x" })) #=> '{"a":[1,2],"b":"x"}'
333
345
  #
334
346
  # @param json [String] the document (UTF-8 or US-ASCII)
335
347
  # @param opts [Hash, nil] acceptance options (+allow_nan+,
336
- # +allow_trailing_comma+, +max_nesting+); trailing commas are
337
- # normalized away when accepted
348
+ # +allow_trailing_comma+, +allow_duplicate_key+, +max_nesting+);
349
+ # trailing commas are normalized away when accepted
338
350
  # @return [String] the minified document
339
351
  # @raise [ParserError] when the document is malformed
340
352
  # @raise [NestingError] past +max_nesting+
@@ -358,8 +370,7 @@ module NOSJ
358
370
  # @return [String] the reformatted document
359
371
  # @raise [ParserError] when the document is malformed
360
372
  # @raise [NestingError] past +max_nesting+
361
- # @raise [GeneratorError] when +ascii_only+ meets a lone-surrogate
362
- # string it cannot represent
373
+ # @raise [GeneratorError] for a non-finite float without +allow_nan+
363
374
  def self.reformat(json, opts = nil)
364
375
  if opts&.key?(:pretty)
365
376
  pretty = opts[:pretty]
@@ -598,7 +609,9 @@ module NOSJ
598
609
  #
599
610
  # @param source [String] the JSON document (UTF-8 or US-ASCII)
600
611
  # @param opts [Hash, nil] +max_nesting+, +allow_nan+,
601
- # +allow_trailing_comma+ (acceptance options only)
612
+ # +allow_trailing_comma+ (acceptance options only). Being a
613
+ # diagnostic, stats also describes documents {.parse} would refuse
614
+ # for a repeated key or a lone surrogate.
602
615
  # @return [Hash] the statistics described above
603
616
  # @raise [ParserError] when the document is malformed or not UTF-8
604
617
  def self.stats(source, opts = nil)
data/sig/nosj.rbs CHANGED
@@ -14,9 +14,10 @@ module NOSJ
14
14
  # Options arrive as a positional Hash (the JSON gem's own calling
15
15
  # convention; an explicit **kwargs would allocate per call), nil when
16
16
  # omitted. Parse: symbolize_names, freeze, max_nesting, allow_nan,
17
- # allow_trailing_comma. Generate: indent, space, space_before,
18
- # object_nl, array_nl, max_nesting, allow_nan, ascii_only,
19
- # script_safe/escape_slash, strict, depth, buffer_initial_length.
17
+ # allow_trailing_comma, allow_duplicate_key. Generate: indent, space,
18
+ # space_before, object_nl, array_nl, max_nesting, allow_nan,
19
+ # ascii_only, script_safe, strict, depth, buffer_initial_length,
20
+ # allow_duplicate_key. Unknown keys raise ArgumentError (json 3).
20
21
  type opts = Hash[Symbol, untyped]?
21
22
 
22
23
  # One NOSJ.dig path element (negative Integer indices resolve to nil).
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nosj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Markin
@@ -64,6 +64,8 @@ files:
64
64
  - ext/nosj/src/lazy.rs
65
65
  - ext/nosj/src/lib.rs
66
66
  - ext/nosj/src/lines.rs
67
+ - ext/nosj/src/locate.rs
68
+ - ext/nosj/src/opt_reader.rs
67
69
  - ext/nosj/src/parse.rs
68
70
  - ext/nosj/src/patch.rs
69
71
  - ext/nosj/src/pointer.rs