jdogo 3.0.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/LICENSE +202 -0
- data/README.md +108 -0
- data/lib/jdogo/decoder.rb +710 -0
- data/lib/jdogo/encoder.rb +662 -0
- data/lib/jdogo/legacy.rb +210 -0
- data/lib/jdogo/number.rb +117 -0
- data/lib/jdogo/readable.rb +604 -0
- data/lib/jdogo/text.rb +281 -0
- data/lib/jdogo/time_column.rb +96 -0
- data/lib/jdogo/value.rb +321 -0
- data/lib/jdogo/version.rb +5 -0
- data/lib/jdogo.rb +159 -0
- metadata +61 -0
data/lib/jdogo.rb
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "jdogo/version"
|
|
4
|
+
require_relative "jdogo/text"
|
|
5
|
+
require_relative "jdogo/number"
|
|
6
|
+
require_relative "jdogo/time_column"
|
|
7
|
+
require_relative "jdogo/value"
|
|
8
|
+
require_relative "jdogo/encoder"
|
|
9
|
+
require_relative "jdogo/readable"
|
|
10
|
+
require_relative "jdogo/decoder"
|
|
11
|
+
require_relative "jdogo/legacy"
|
|
12
|
+
|
|
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.1 and writes the same bytes as the Java, Python,
|
|
15
|
+
# TypeScript, Go, Dart and Swift SDKs on every shared conformance vector.
|
|
16
|
+
#
|
|
17
|
+
# A list of objects becomes a table whose structure appears once, in a header:
|
|
18
|
+
#
|
|
19
|
+
# rows = [{ "id" => 1, "name" => "Ana", "city" => "Paris" },
|
|
20
|
+
# { "id" => 2, "name" => "Bo", "city" => "Oslo" }]
|
|
21
|
+
# text = JDogo.encode(rows) # => "@id,name,city\n1,Ana,Paris\n2,Bo,Oslo"
|
|
22
|
+
# JDogo.decode(text) # => the same rows
|
|
23
|
+
# JDogo.encode(rows, mode: :readable) # full keys and values, for logs
|
|
24
|
+
#
|
|
25
|
+
# Decoded values are plain Ruby: nil, true, false, String, Integer (any size), Float, Array and Hash
|
|
26
|
+
# (String keys, in the order they were written). The encoders also accept Symbols, Hashes with Symbol
|
|
27
|
+
# keys, other Numerics and objects that respond to as_json or to_h.
|
|
28
|
+
module JDogo
|
|
29
|
+
# The JDogo specification this library implements.
|
|
30
|
+
SPEC_VERSION = "2.3.1"
|
|
31
|
+
|
|
32
|
+
# Default limits of the readers, the same in every SDK: documents nested deeper than 64 levels or
|
|
33
|
+
# holding more than 100,000 items in one list are refused. The encoders refuse values nested deeper
|
|
34
|
+
# than MAX_DEPTH, so they never write text a reader would reject.
|
|
35
|
+
MAX_DEPTH = 64
|
|
36
|
+
MAX_COLLECTION = 100_000
|
|
37
|
+
|
|
38
|
+
# The forms JDogo.encode writes, the same three in every JDogo SDK.
|
|
39
|
+
MODES = %i[compact readable typed].freeze
|
|
40
|
+
|
|
41
|
+
module_function
|
|
42
|
+
|
|
43
|
+
# ---------------------------------------------------------------- writing
|
|
44
|
+
|
|
45
|
+
# Returns the JDogo text for value, by default the compact document: the lossless default format, the
|
|
46
|
+
# same bytes the other SDKs write. JDogo.decode gives the value back.
|
|
47
|
+
#
|
|
48
|
+
# mode: :compact (default), :readable (full keys and values, for logs and prompts) or :typed (the
|
|
49
|
+
# compact text after a '!JDOGO/2 @doc' line), as in every JDogo SDK; a String works too.
|
|
50
|
+
# time_deltas: writes columns of canonical UTC timestamps ('2026-11-01T08:05:51Z', or with exactly
|
|
51
|
+
# three fraction digits) as '+t' / '+t3' differences (spec 2.2), in :compact mode only. Readers that
|
|
52
|
+
# only know spec 2.1 reject such documents, so turn it on once every reader is on 2.2.
|
|
53
|
+
# Raises ArgumentError for an unknown mode, or time_deltas with a mode other than :compact.
|
|
54
|
+
def encode(value, mode: :compact, time_deltas: false)
|
|
55
|
+
mode = mode.to_s.to_sym if mode.is_a?(String)
|
|
56
|
+
raise ArgumentError, "unknown mode #{mode.inspect}: use :compact, :readable or :typed" unless MODES.include?(mode)
|
|
57
|
+
raise ArgumentError, "time_deltas works with mode :compact only" if time_deltas && mode != :compact
|
|
58
|
+
|
|
59
|
+
normalized = Value.normalize(value)
|
|
60
|
+
case mode
|
|
61
|
+
when :readable then ReadableEncoder.encode(normalized, "", false)
|
|
62
|
+
when :typed
|
|
63
|
+
return "#{Legacy::TYPED_V1_HEADER}\nnull" if value.nil?
|
|
64
|
+
|
|
65
|
+
"#{Legacy::TYPED_DOC_MARKER}\n#{Encoder.encode_compact(normalized, false)}"
|
|
66
|
+
else Encoder.encode_compact(normalized, time_deltas)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Returns typed JDogo: the line '!JDOGO/2 @doc' followed by the compact document. nil is written as
|
|
71
|
+
# '!JDOGO/2 @mode TYPED' and a line 'null', as Java writes it.
|
|
72
|
+
#
|
|
73
|
+
# Deprecated since 3.0: use JDogo.encode(value, mode: :typed). Kept until 3.1.
|
|
74
|
+
def encode_typed(value, time_deltas: false)
|
|
75
|
+
warn("JDogo.encode_typed is deprecated: use JDogo.encode(value, mode: :typed)", category: :deprecated, uplevel: 1)
|
|
76
|
+
text = encode(value, time_deltas: time_deltas)
|
|
77
|
+
return "#{Legacy::TYPED_V1_HEADER}\nnull" if value.nil?
|
|
78
|
+
|
|
79
|
+
"#{Legacy::TYPED_DOC_MARKER}\n#{text}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Writes one value on one line, readable on its own (spec 2.1 section 2): a context value or a tool
|
|
83
|
+
# payload. A string that would start a document, or read as another type, is quoted.
|
|
84
|
+
def encode_value(value)
|
|
85
|
+
Encoder.write_inline(Value.normalize(value), false, true)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Returns readable JDogo: full key names and values, with no differences, dictionaries or ditto
|
|
89
|
+
# marks. It is larger than encode's output but easy to read in logs and prompts, and every reader
|
|
90
|
+
# decodes it. The output is the same as Java's DocCodec.encodeLegibleKeys, Python's encode_readable
|
|
91
|
+
# and TypeScript's encodeReadable.
|
|
92
|
+
#
|
|
93
|
+
# question: keeps columns verbatim when the question quotes one of their values, so a model can match
|
|
94
|
+
# the text it was asked about. dense_rows: keeps wide sparse rows as one table instead of '@:' record
|
|
95
|
+
# lines.
|
|
96
|
+
def encode_readable(value, question: nil, dense_rows: false)
|
|
97
|
+
q = question.nil? ? "" : Text.valid_text(question.to_s)
|
|
98
|
+
ReadableEncoder.encode(Value.normalize(value), q, dense_rows)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# ---------------------------------------------------------------- reading
|
|
102
|
+
|
|
103
|
+
# Reads JDogo text, the same in every JDogo SDK: compact, readable and typed ('!JDOGO/2') text, any
|
|
104
|
+
# JSON, and text written by JDogo 1.x when it is not a valid document and carries a 1.x marker (spec
|
|
105
|
+
# 2.1 section 10). A byte-order mark is ignored and empty or whitespace-only text reads as nil.
|
|
106
|
+
# Invalid text raises JDogo::ParseError, a limit JDogo::LimitError; decode never makes up values.
|
|
107
|
+
#
|
|
108
|
+
# document_only: reads compact and readable documents and JSON only, as decode did in 2.x: typed and
|
|
109
|
+
# 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, document_only: false)
|
|
111
|
+
return Decoder.decode(text_arg(text), max_depth, max_collection) if document_only
|
|
112
|
+
|
|
113
|
+
text = Text.valid_text(text_arg(text)).delete_prefix("\uFEFF")
|
|
114
|
+
legacy = Legacy.new(max_depth, max_collection)
|
|
115
|
+
return legacy.decode_typed(text) if Legacy.trim_space(text).start_with?("!JDOGO/2")
|
|
116
|
+
|
|
117
|
+
begin
|
|
118
|
+
Decoder.decode(text, max_depth, max_collection)
|
|
119
|
+
rescue Error
|
|
120
|
+
raise unless Legacy.looks_v1?(text)
|
|
121
|
+
|
|
122
|
+
legacy.decode_v1(text)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# JDogo.decode, under its 2.x name.
|
|
127
|
+
#
|
|
128
|
+
# Deprecated since 3.0: use JDogo.decode, which reads the same text. Kept until 3.1.
|
|
129
|
+
def read(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION)
|
|
130
|
+
warn("JDogo.read is deprecated: use JDogo.decode", category: :deprecated, uplevel: 1)
|
|
131
|
+
decode(text, max_depth: max_depth, max_collection: max_collection)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Ruby-style names: JDogo.dump writes compact JDogo, JDogo.load reads any JDogo text (JDogo.decode).
|
|
135
|
+
def dump(value, time_deltas: false) = encode(value, time_deltas: time_deltas)
|
|
136
|
+
|
|
137
|
+
def load(text, max_depth: MAX_DEPTH, max_collection: MAX_COLLECTION)
|
|
138
|
+
decode(text, max_depth: max_depth, max_collection: max_collection)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# ---------------------------------------------------------------- JSON and equality helpers
|
|
142
|
+
|
|
143
|
+
# Reads JSON text into the value model: object keys in order, integers of any size kept exactly.
|
|
144
|
+
def parse_json(text) = Value.parse_json(text_arg(text))
|
|
145
|
+
|
|
146
|
+
# Writes a value as compact JSON, object keys in order; NaN and infinities as null.
|
|
147
|
+
def generate_json(value) = Value.generate_json(value)
|
|
148
|
+
|
|
149
|
+
# Whether a and b are the same JSON value: numbers compare by value (5 and 5.0 are equal, as spec 2.1
|
|
150
|
+
# rule 5.2 says), objects by their keys in order and their values.
|
|
151
|
+
def same_value?(a, b) = Value.same_json?(a, b)
|
|
152
|
+
|
|
153
|
+
def text_arg(text)
|
|
154
|
+
raise TypeError, "JDogo text must be a String, got #{text.class}" unless text.is_a?(String)
|
|
155
|
+
|
|
156
|
+
text
|
|
157
|
+
end
|
|
158
|
+
private_class_method :text_arg
|
|
159
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jdogo
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 3.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- JDogo contributors
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'Reads and writes JDogo (spec 2.3.1): compact, readable and typed documents,
|
|
14
|
+
byte for byte the same as the Java, Python, TypeScript, Go, Dart, Swift, Rust, C#
|
|
15
|
+
and PHP SDKs.'
|
|
16
|
+
email:
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/jdogo.rb
|
|
25
|
+
- lib/jdogo/decoder.rb
|
|
26
|
+
- lib/jdogo/encoder.rb
|
|
27
|
+
- lib/jdogo/legacy.rb
|
|
28
|
+
- lib/jdogo/number.rb
|
|
29
|
+
- lib/jdogo/readable.rb
|
|
30
|
+
- lib/jdogo/text.rb
|
|
31
|
+
- lib/jdogo/time_column.rb
|
|
32
|
+
- lib/jdogo/value.rb
|
|
33
|
+
- lib/jdogo/version.rb
|
|
34
|
+
homepage: https://github.com/jdogo-dev/jdogo-ruby
|
|
35
|
+
licenses:
|
|
36
|
+
- Apache-2.0
|
|
37
|
+
metadata:
|
|
38
|
+
homepage_uri: https://github.com/jdogo-dev/jdogo-ruby
|
|
39
|
+
source_code_uri: https://github.com/jdogo-dev/jdogo-ruby
|
|
40
|
+
changelog_uri: https://github.com/jdogo-dev/jdogo-ruby/blob/main/CHANGELOG.md
|
|
41
|
+
rubygems_mfa_required: 'true'
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '3.1'
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 3.5.22
|
|
58
|
+
signing_key:
|
|
59
|
+
specification_version: 4
|
|
60
|
+
summary: 'JDogo: a compact, lossless text format for JSON-like data in LLM prompts'
|
|
61
|
+
test_files: []
|