jdogo 3.0.0 → 3.0.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -1
- data/README.md +1 -1
- data/lib/jdogo/decoder.rb +37 -4
- data/lib/jdogo/encoder.rb +8 -1
- data/lib/jdogo/legacy.rb +4 -3
- data/lib/jdogo/value.rb +22 -6
- data/lib/jdogo/version.rb +1 -1
- data/lib/jdogo.rb +13 -9
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7889598e8e4f3a151a00a6a1e9ef246045c543edcdfe7b59c630fba232875dbb
|
|
4
|
+
data.tar.gz: 30b844dc5608ca441d903e12f944805a5489027e58e9d2ac20be05a2557778bb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4a71b6ae7c37ed87216794e628cc5f620770b039cd39aab2b4020119c2f9079127db2e284cb33c2a201bfe9aa67d7430fb6f99ae3d242dd993f422ed210c3b9c
|
|
7
|
+
data.tar.gz: 142aa37485ae0b587668dd9f1bd6aae1e1efac8b5f146d53e75f4b498f2430e0cfab26e0afdd4de4787bb5efd410fd02ff6dd67207db0a471f3ba24ba19f1323
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
## v3.0.1 (2026-12-06)
|
|
5
|
+
|
|
6
|
+
Spec 2.3.2. No output changes for any value that encoded in 3.0.0.
|
|
7
|
+
|
|
8
|
+
- **Security: expansion limit (spec 2.3.2 section 8).** Column constants, dictionary entries, prefixes, suffixes and `^` cells let a short document stand for far more data: 0.3 MB could read as 10 GB, and each repeated list or object was copied. `JDogo.decode` now counts what these copy (in bytes) and raises `JDogo::LimitError` past 50,000,000 (`max_expansion:`, `JDogo::MAX_EXPANSION`).
|
|
9
|
+
- **`Time`, `DateTime` and `Date` encode as ISO 8601** (`2026-01-02T03:04:05.000Z`, `2026-01-02`), as Rails writes them; they raised `EncodeError` before.
|
|
10
|
+
- **`BigDecimal` is never rounded silently:** one a float holds exactly is written as before; any other (`BigDecimal("12345678901234567.891")`) raises `EncodeError` instead of being written as `12345678901234568`.
|
|
11
|
+
- A limit error is no longer retried as 1.x text.
|
|
12
|
+
## v3.0.0 (2026-12-05)
|
|
4
13
|
|
|
5
14
|
The first release, with the 3.0 names that every JDogo SDK shares. It implements spec 2.3.1 and writes the same bytes as the Java, Python, TypeScript, Go, Dart, Swift, Rust, C# and PHP SDKs.
|
|
6
15
|
|
data/README.md
CHANGED
|
@@ -11,7 +11,7 @@ JDogo is a compact, lossless text format for JSON-like data, made for LLM prompt
|
|
|
11
11
|
|
|
12
12
|
`id+` stores each id as the difference from the one above.
|
|
13
13
|
|
|
14
|
-
This gem implements **JDogo spec 2.3.
|
|
14
|
+
This gem implements **JDogo spec 2.3.2**. It writes the same bytes as the [Java](https://github.com/jdogo-dev/jdogo-java), [Python](https://github.com/jdogo-dev/jdogo-python), [TypeScript](https://github.com/jdogo-dev/jdogo-js), [Go](https://github.com/jdogo-dev/jdogo-go), [Rust](https://github.com/jdogo-dev/jdogo-rust), [C#](https://github.com/jdogo-dev/jdogo-csharp), [Dart](https://github.com/jdogo-dev/jdogo-dart), [Swift](https://github.com/jdogo-dev/jdogo-swift) and [PHP](https://github.com/jdogo-dev/jdogo-php) SDKs on every shared vector in [jdogo-spec](https://github.com/jdogo-dev/jdogo-spec), and reads everything they write.
|
|
15
15
|
|
|
16
16
|
## Install
|
|
17
17
|
|
data/lib/jdogo/decoder.rb
CHANGED
|
@@ -48,16 +48,16 @@ module JDogo
|
|
|
48
48
|
114 => "\r", 116 => "\t" }.freeze
|
|
49
49
|
|
|
50
50
|
# decode reads a document (any JSON text is one). Empty or whitespace-only text reads as nil.
|
|
51
|
-
def self.decode(text, max_depth, max_collection)
|
|
51
|
+
def self.decode(text, max_depth, max_collection, max_expansion = MAX_EXPANSION)
|
|
52
52
|
text = Text.valid_text(text)
|
|
53
53
|
text = text.byteslice(3, text.bytesize - 3) if text.start_with?("\uFEFF") # not part of the document
|
|
54
54
|
s = text.end_with?("\n", "\r") ? text.sub(/[\r\n]+\z/, "") : text
|
|
55
55
|
return nil if s.match?(/\A[ \t\r\n]*\z/)
|
|
56
56
|
|
|
57
|
-
new(s, max_depth, max_collection).document
|
|
57
|
+
new(s, max_depth, max_collection, max_expansion).document
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
def initialize(s, max_depth, max_collection)
|
|
60
|
+
def initialize(s, max_depth, max_collection, max_expansion = MAX_EXPANSION)
|
|
61
61
|
@s = s
|
|
62
62
|
@n = s.bytesize
|
|
63
63
|
@p = 0
|
|
@@ -65,6 +65,24 @@ module JDogo
|
|
|
65
65
|
@depth = 0
|
|
66
66
|
@max_depth = max_depth
|
|
67
67
|
@max_collection = max_collection
|
|
68
|
+
@max_expansion = max_expansion
|
|
69
|
+
@expansion = 0
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# copy counts a copied value (spec 2.3.2 rule 8.1) and fails past the expansion limit (rule 8.2).
|
|
73
|
+
def copy(size)
|
|
74
|
+
@expansion += size
|
|
75
|
+
return if @expansion <= @max_expansion
|
|
76
|
+
|
|
77
|
+
raise LimitError, "Maximum expansion exceeded: more than #{@max_expansion} characters copied by " \
|
|
78
|
+
"constants, dictionaries, prefixes, suffixes and '^' cells"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# written_size is the byte length of @s[start, finish) without the spaces and tabs at its end
|
|
82
|
+
# (rule 8.1: the text as written).
|
|
83
|
+
def written_size(start, finish)
|
|
84
|
+
finish -= 1 while finish > start && ((c = @s.getbyte(finish - 1)) == SP || c == TAB)
|
|
85
|
+
finish - start
|
|
68
86
|
end
|
|
69
87
|
|
|
70
88
|
# ---------------------------------------------------------------- cursor
|
|
@@ -335,17 +353,24 @@ module JDogo
|
|
|
335
353
|
expect(RBRACE)
|
|
336
354
|
when EQ
|
|
337
355
|
@p += 1
|
|
338
|
-
|
|
356
|
+
skip_ws(false)
|
|
357
|
+
start = @p
|
|
358
|
+
v = parse_value_raw(false)
|
|
339
359
|
fail!("empty constant") if ABSENT.equal?(v)
|
|
360
|
+
f.const_size = written_size(start, @p)
|
|
361
|
+
skip_ws(false)
|
|
340
362
|
f.kind = :const
|
|
341
363
|
f.const_val = v
|
|
342
364
|
when AT
|
|
343
365
|
@p += 1
|
|
344
366
|
f.kind = :dict
|
|
345
367
|
f.dict = []
|
|
368
|
+
f.dict_sizes = []
|
|
346
369
|
loop do
|
|
347
370
|
# Read like Java's readRaw: up to , ] } ; or the line end; ':' and brackets are text.
|
|
371
|
+
start = @p
|
|
348
372
|
f.dict << (at?(QUOTE) ? parse_quoted : parse_value_token(false))
|
|
373
|
+
f.dict_sizes << written_size(start, @p)
|
|
349
374
|
break unless at?(SEMI)
|
|
350
375
|
|
|
351
376
|
@p += 1
|
|
@@ -383,7 +408,9 @@ module JDogo
|
|
|
383
408
|
end
|
|
384
409
|
f.kind = :affix
|
|
385
410
|
stop = mod == CARET ? PREFIX_TEXT : SUFFIX_TEXT
|
|
411
|
+
start = @p
|
|
386
412
|
val = at?(QUOTE) ? parse_quoted : trimmed(scan_span(stop))
|
|
413
|
+
f.affix_size += written_size(start, @p)
|
|
387
414
|
if mod == CARET
|
|
388
415
|
f.prefix = val
|
|
389
416
|
else
|
|
@@ -404,6 +431,7 @@ module JDogo
|
|
|
404
431
|
def parse_fields(fields, target, started, in_wrap)
|
|
405
432
|
fields.each do |f|
|
|
406
433
|
if f.kind == :const
|
|
434
|
+
copy(f.const_size)
|
|
407
435
|
target[f.name] = copy_value(f.const_val) # each row gets its own copy of a list or object
|
|
408
436
|
next
|
|
409
437
|
end
|
|
@@ -498,12 +526,14 @@ module JDogo
|
|
|
498
526
|
tok = parse_token(in_wrap)
|
|
499
527
|
idx = DIGITS.match?(tok) ? tok.to_i : nil
|
|
500
528
|
fail!("dictionary index out of range: #{tok.inspect}") if idx.nil? || idx >= f.dict.size # an error, as in Java
|
|
529
|
+
copy(f.dict_sizes[idx])
|
|
501
530
|
return f.dict[idx]
|
|
502
531
|
when :affix
|
|
503
532
|
if at?(TILDE)
|
|
504
533
|
@p += 1
|
|
505
534
|
return nil
|
|
506
535
|
end
|
|
536
|
+
copy(f.affix_size)
|
|
507
537
|
return "#{f.prefix}#{running_value(f, in_wrap)}#{f.suffix}" if f.delta
|
|
508
538
|
|
|
509
539
|
mid = at?(QUOTE) ? parse_quoted : parse_value_token(in_wrap)
|
|
@@ -522,12 +552,15 @@ module JDogo
|
|
|
522
552
|
q += 1 while q < @n && ((c = @s.getbyte(q)) == SP || c == TAB)
|
|
523
553
|
if q >= @n || DITTO_END.include?(@s.getbyte(q))
|
|
524
554
|
@p += 1
|
|
555
|
+
copy(f.prev_size)
|
|
525
556
|
return copy_value(f.prev_val)
|
|
526
557
|
end
|
|
527
558
|
end
|
|
559
|
+
start = @p
|
|
528
560
|
v = parse_value_raw(in_wrap) # parse_fields has skipped the spaces before the cell
|
|
529
561
|
unless ABSENT.equal?(v)
|
|
530
562
|
f.prev_val = v
|
|
563
|
+
f.prev_size = written_size(start, @p)
|
|
531
564
|
skip_ws(false)
|
|
532
565
|
end
|
|
533
566
|
v
|
data/lib/jdogo/encoder.rb
CHANGED
|
@@ -10,7 +10,10 @@ module JDogo
|
|
|
10
10
|
# @api private
|
|
11
11
|
class Field
|
|
12
12
|
attr_accessor :name, :kind, :sub, :wrapped, :const_val, :dict, :dict_index, :prefix, :suffix, :delta,
|
|
13
|
-
:time_unit, :trim_integral, :prev, :has_prev, :prev_val
|
|
13
|
+
:time_unit, :trim_integral, :prev, :has_prev, :prev_val,
|
|
14
|
+
# Copy sizes (spec 2.3.2 rule 8.1), in bytes: the text as written of the constant, each
|
|
15
|
+
# dictionary entry, the prefix plus suffix, and the cell that a '^' repeats.
|
|
16
|
+
:const_size, :dict_sizes, :affix_size, :prev_size
|
|
14
17
|
|
|
15
18
|
def initialize(name)
|
|
16
19
|
@name = name
|
|
@@ -25,6 +28,9 @@ module JDogo
|
|
|
25
28
|
@delta = false
|
|
26
29
|
@time_unit = TimeColumn::SECONDS
|
|
27
30
|
@trim_integral = false # readable encoder: whole floats in this column are written as integers
|
|
31
|
+
@const_size = 0
|
|
32
|
+
@dict_sizes = nil
|
|
33
|
+
@affix_size = 0
|
|
28
34
|
reset
|
|
29
35
|
end
|
|
30
36
|
|
|
@@ -32,6 +38,7 @@ module JDogo
|
|
|
32
38
|
@prev = 0
|
|
33
39
|
@has_prev = false
|
|
34
40
|
@prev_val = nil
|
|
41
|
+
@prev_size = 0
|
|
35
42
|
end
|
|
36
43
|
|
|
37
44
|
# next_diff returns the difference from the previous running value (the value itself on the first
|
data/lib/jdogo/legacy.rb
CHANGED
|
@@ -34,9 +34,10 @@ module JDogo
|
|
|
34
34
|
rest.include?("|") || V1_OBJECT.match?(rest)
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
def initialize(max_depth, max_collection)
|
|
37
|
+
def initialize(max_depth, max_collection, max_expansion = MAX_EXPANSION)
|
|
38
38
|
@max_depth = max_depth
|
|
39
39
|
@max_collection = max_collection
|
|
40
|
+
@max_expansion = max_expansion
|
|
40
41
|
end
|
|
41
42
|
|
|
42
43
|
def check_size(n)
|
|
@@ -59,7 +60,7 @@ module JDogo
|
|
|
59
60
|
lines = Legacy.non_empty_lines(s)
|
|
60
61
|
unless lines.drop(1).any? { |l| l.include?("|") }
|
|
61
62
|
begin
|
|
62
|
-
return Decoder.decode(s, @max_depth, @max_collection)
|
|
63
|
+
return Decoder.decode(s, @max_depth, @max_collection, @max_expansion)
|
|
63
64
|
rescue Error
|
|
64
65
|
nil # read as 1.x below
|
|
65
66
|
end
|
|
@@ -186,7 +187,7 @@ module JDogo
|
|
|
186
187
|
# older '!JDOGO/2 @mode TYPED' layout.
|
|
187
188
|
def decode_typed(text)
|
|
188
189
|
marker = ["#{TYPED_DOC_MARKER}\n", "#{TYPED_DOC_MARKER}\r\n"].find { |m| text.start_with?(m) }
|
|
189
|
-
return Decoder.decode(text.byteslice(marker.bytesize..), @max_depth, @max_collection) if marker
|
|
190
|
+
return Decoder.decode(text.byteslice(marker.bytesize..), @max_depth, @max_collection, @max_expansion) if marker
|
|
190
191
|
|
|
191
192
|
s = Legacy.trim_space(text)
|
|
192
193
|
s = Legacy.trim_space(s.delete_prefix(TYPED_V1_HEADER)) if s.start_with?(TYPED_V1_HEADER)
|
data/lib/jdogo/value.rb
CHANGED
|
@@ -51,13 +51,14 @@ module JDogo
|
|
|
51
51
|
when Array then normalize_array(v, depth, limit)
|
|
52
52
|
when Hash then normalize_hash(v, depth, limit)
|
|
53
53
|
when Symbol then Text.valid_text(v.to_s)
|
|
54
|
-
when Numeric
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
raise EncodeError, "cannot encode #{v.class}: #{e.message}"
|
|
59
|
-
end
|
|
54
|
+
when Numeric then normalize_number(v)
|
|
55
|
+
when Time
|
|
56
|
+
require "time"
|
|
57
|
+
v.iso8601(3) # as Rails writes it: 2026-01-02T03:04:05.000Z
|
|
60
58
|
else
|
|
59
|
+
return v.iso8601(3) if defined?(DateTime) && v.is_a?(DateTime)
|
|
60
|
+
return v.iso8601 if defined?(Date) && v.is_a?(Date)
|
|
61
|
+
|
|
61
62
|
if v.respond_to?(:as_json)
|
|
62
63
|
normalize(v.as_json, depth, limit)
|
|
63
64
|
elsif v.respond_to?(:to_h)
|
|
@@ -68,6 +69,21 @@ module JDogo
|
|
|
68
69
|
end
|
|
69
70
|
end
|
|
70
71
|
|
|
72
|
+
# Other Numerics are written as floats. A BigDecimal that a float cannot hold exactly is refused
|
|
73
|
+
# rather than rounded (BigDecimal("12345678901234567.891") would become 12345678901234568).
|
|
74
|
+
def normalize_number(v)
|
|
75
|
+
f = Float(v)
|
|
76
|
+
if defined?(BigDecimal) && v.is_a?(BigDecimal) && v.finite? && BigDecimal(f.to_s) != v
|
|
77
|
+
raise EncodeError, "cannot encode the BigDecimal #{v.to_s('F')} exactly: a JDogo number is a float, " \
|
|
78
|
+
"which would round it; convert it to a String, or to a Float if rounding is acceptable"
|
|
79
|
+
end
|
|
80
|
+
f
|
|
81
|
+
rescue EncodeError
|
|
82
|
+
raise
|
|
83
|
+
rescue StandardError => e
|
|
84
|
+
raise EncodeError, "cannot encode #{v.class}: #{e.message}"
|
|
85
|
+
end
|
|
86
|
+
|
|
71
87
|
def check_depth(depth, limit)
|
|
72
88
|
# the encoders never write text that a reader would reject
|
|
73
89
|
raise LimitError, "Maximum recursion depth exceeded: #{depth + 1} > #{limit}" if depth >= limit
|
data/lib/jdogo/version.rb
CHANGED
data/lib/jdogo.rb
CHANGED
|
@@ -11,7 +11,7 @@ require_relative "jdogo/decoder"
|
|
|
11
11
|
require_relative "jdogo/legacy"
|
|
12
12
|
|
|
13
13
|
# JDogo reads and writes JDogo, a compact, lossless text format for JSON-like data, aimed at LLM
|
|
14
|
-
# prompts and APIs. It implements JDogo spec 2.3.
|
|
14
|
+
# prompts and APIs. It implements JDogo spec 2.3.2 and writes the same bytes as the Java, Python,
|
|
15
15
|
# TypeScript, Go, Dart and Swift SDKs on every shared conformance vector.
|
|
16
16
|
#
|
|
17
17
|
# A list of objects becomes a table whose structure appears once, in a header:
|
|
@@ -27,13 +27,16 @@ require_relative "jdogo/legacy"
|
|
|
27
27
|
# keys, other Numerics and objects that respond to as_json or to_h.
|
|
28
28
|
module JDogo
|
|
29
29
|
# The JDogo specification this library implements.
|
|
30
|
-
SPEC_VERSION = "2.3.
|
|
30
|
+
SPEC_VERSION = "2.3.2"
|
|
31
31
|
|
|
32
32
|
# Default limits of the readers, the same in every SDK: documents nested deeper than 64 levels or
|
|
33
33
|
# holding more than 100,000 items in one list are refused. The encoders refuse values nested deeper
|
|
34
34
|
# than MAX_DEPTH, so they never write text a reader would reject.
|
|
35
35
|
MAX_DEPTH = 64
|
|
36
36
|
MAX_COLLECTION = 100_000
|
|
37
|
+
# Characters a document may copy through column constants, dictionaries, prefixes, suffixes and '^'
|
|
38
|
+
# cells (spec 2.3.2 section 8), counted in bytes.
|
|
39
|
+
MAX_EXPANSION = 50_000_000
|
|
37
40
|
|
|
38
41
|
# The forms JDogo.encode writes, the same three in every JDogo SDK.
|
|
39
42
|
MODES = %i[compact readable typed].freeze
|
|
@@ -107,16 +110,17 @@ module JDogo
|
|
|
107
110
|
#
|
|
108
111
|
# document_only: reads compact and readable documents and JSON only, as decode did in 2.x: typed and
|
|
109
112
|
# 1.x text raise JDogo::ParseError. For input that must be a document.
|
|
110
|
-
def decode(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION,
|
|
111
|
-
|
|
113
|
+
def decode(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION, max_expansion: MAX_EXPANSION,
|
|
114
|
+
document_only: false)
|
|
115
|
+
return Decoder.decode(text_arg(text), max_depth, max_collection, max_expansion) if document_only
|
|
112
116
|
|
|
113
117
|
text = Text.valid_text(text_arg(text)).delete_prefix("\uFEFF")
|
|
114
|
-
legacy = Legacy.new(max_depth, max_collection)
|
|
118
|
+
legacy = Legacy.new(max_depth, max_collection, max_expansion)
|
|
115
119
|
return legacy.decode_typed(text) if Legacy.trim_space(text).start_with?("!JDOGO/2")
|
|
116
120
|
|
|
117
121
|
begin
|
|
118
|
-
Decoder.decode(text, max_depth, max_collection)
|
|
119
|
-
rescue
|
|
122
|
+
Decoder.decode(text, max_depth, max_collection, max_expansion)
|
|
123
|
+
rescue ParseError # a limit is the answer, not a sign of 1.x text
|
|
120
124
|
raise unless Legacy.looks_v1?(text)
|
|
121
125
|
|
|
122
126
|
legacy.decode_v1(text)
|
|
@@ -134,8 +138,8 @@ module JDogo
|
|
|
134
138
|
# Ruby-style names: JDogo.dump writes compact JDogo, JDogo.load reads any JDogo text (JDogo.decode).
|
|
135
139
|
def dump(value, time_deltas: false) = encode(value, time_deltas: time_deltas)
|
|
136
140
|
|
|
137
|
-
def load(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION)
|
|
138
|
-
decode(text, max_depth: max_depth, max_collection: max_collection)
|
|
141
|
+
def load(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION, max_expansion: MAX_EXPANSION)
|
|
142
|
+
decode(text, max_depth: max_depth, max_collection: max_collection, max_expansion: max_expansion)
|
|
139
143
|
end
|
|
140
144
|
|
|
141
145
|
# ---------------------------------------------------------------- JSON and equality helpers
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jdogo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0.
|
|
4
|
+
version: 3.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- JDogo contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-12-
|
|
11
|
+
date: 2026-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'Reads and writes JDogo (spec 2.3.1): compact, readable and typed documents,
|
|
14
14
|
byte for byte the same as the Java, Python, TypeScript, Go, Dart, Swift, Rust, C#
|