cton 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +48 -0
- data/README.md +119 -0
- data/lib/cton/decoder.rb +49 -20
- data/lib/cton/encoder.rb +16 -2
- data/lib/cton/stats.rb +123 -0
- data/lib/cton/type_registry.rb +137 -0
- data/lib/cton/validator.rb +427 -0
- data/lib/cton/version.rb +1 -1
- data/lib/cton.rb +84 -2
- data/sig/cton.rbs +119 -6
- metadata +5 -2
data/sig/cton.rbs
CHANGED
|
@@ -2,12 +2,97 @@ module Cton
|
|
|
2
2
|
VERSION: String
|
|
3
3
|
class Error < ::StandardError; end
|
|
4
4
|
class EncodeError < Error; end
|
|
5
|
-
class ParseError < Error; end
|
|
6
5
|
|
|
6
|
+
class ParseError < Error
|
|
7
|
+
attr_reader line: Integer?
|
|
8
|
+
attr_reader column: Integer?
|
|
9
|
+
attr_reader source_excerpt: String?
|
|
10
|
+
attr_reader suggestions: Array[String]
|
|
11
|
+
|
|
12
|
+
def initialize: (String message, ?line: Integer?, ?column: Integer?, ?source_excerpt: String?, ?suggestions: Array[String]?) -> void
|
|
13
|
+
def to_h: -> Hash[Symbol, untyped]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Module-level methods
|
|
7
17
|
def self.dump: (untyped payload, ?Hash[Symbol, untyped] options) -> String
|
|
8
18
|
def self.generate: (untyped payload, ?Hash[Symbol, untyped] options) -> String
|
|
9
19
|
def self.load: (String cton_string, ?symbolize_names: bool) -> untyped
|
|
10
20
|
def self.parse: (String cton_string, ?symbolize_names: bool) -> untyped
|
|
21
|
+
def self.validate: (String cton_string) -> ValidationResult
|
|
22
|
+
def self.valid?: (String cton_string) -> bool
|
|
23
|
+
def self.stats: (untyped data) -> Stats
|
|
24
|
+
def self.stats_hash: (untyped data) -> Hash[Symbol, untyped]
|
|
25
|
+
def self.register_type: (Class klass, ?as: Symbol) { (untyped) -> untyped } -> void
|
|
26
|
+
def self.unregister_type: (Class klass) -> void
|
|
27
|
+
def self.clear_type_registry!: -> void
|
|
28
|
+
def self.type_registry: -> TypeRegistry
|
|
29
|
+
|
|
30
|
+
class ValidationResult
|
|
31
|
+
attr_reader errors: Array[ValidationError]
|
|
32
|
+
|
|
33
|
+
def initialize: (?Array[ValidationError] errors) -> void
|
|
34
|
+
def valid?: -> bool
|
|
35
|
+
def to_s: -> String
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class ValidationError
|
|
39
|
+
attr_reader message: String
|
|
40
|
+
attr_reader line: Integer
|
|
41
|
+
attr_reader column: Integer
|
|
42
|
+
attr_reader source_excerpt: String?
|
|
43
|
+
|
|
44
|
+
def initialize: (message: String, line: Integer, column: Integer, ?source_excerpt: String?) -> void
|
|
45
|
+
def to_s: -> String
|
|
46
|
+
def to_h: -> Hash[Symbol, untyped]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class Validator
|
|
50
|
+
def initialize: -> void
|
|
51
|
+
def validate: (String cton_string) -> ValidationResult
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class Stats
|
|
55
|
+
CHARS_PER_TOKEN: Integer
|
|
56
|
+
|
|
57
|
+
attr_reader data: untyped
|
|
58
|
+
attr_reader json_string: String
|
|
59
|
+
attr_reader cton_string: String
|
|
60
|
+
|
|
61
|
+
def initialize: (untyped data, ?cton_string: String?, ?json_string: String?) -> void
|
|
62
|
+
def json_chars: -> Integer
|
|
63
|
+
def cton_chars: -> Integer
|
|
64
|
+
def json_bytes: -> Integer
|
|
65
|
+
def cton_bytes: -> Integer
|
|
66
|
+
def savings_chars: -> Integer
|
|
67
|
+
def savings_bytes: -> Integer
|
|
68
|
+
def savings_percent: -> Float
|
|
69
|
+
def estimated_json_tokens: -> Integer
|
|
70
|
+
def estimated_cton_tokens: -> Integer
|
|
71
|
+
def estimated_token_savings: -> Integer
|
|
72
|
+
def to_h: -> Hash[Symbol, untyped]
|
|
73
|
+
def to_s: -> String
|
|
74
|
+
|
|
75
|
+
def self.compare: (untyped data, ?options: Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
class TypeRegistry
|
|
79
|
+
class Handler
|
|
80
|
+
attr_accessor klass: Class
|
|
81
|
+
attr_accessor mode: Symbol
|
|
82
|
+
attr_accessor block: Proc
|
|
83
|
+
|
|
84
|
+
def initialize: (?klass: Class?, ?mode: Symbol?, ?block: Proc?) -> void
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def initialize: -> void
|
|
88
|
+
def register: (Class klass, ?as: Symbol) { (untyped) -> untyped } -> void
|
|
89
|
+
def unregister: (Class klass) -> void
|
|
90
|
+
def registered?: (Class klass) -> bool
|
|
91
|
+
def transform: (untyped value) -> untyped
|
|
92
|
+
def handler_for: (Class klass) -> Handler?
|
|
93
|
+
def registered_types: -> Array[Class]
|
|
94
|
+
def clear!: -> void
|
|
95
|
+
end
|
|
11
96
|
|
|
12
97
|
class Encoder
|
|
13
98
|
SAFE_TOKEN: Regexp
|
|
@@ -15,38 +100,57 @@ module Cton
|
|
|
15
100
|
RESERVED_LITERALS: Array[String]
|
|
16
101
|
FLOAT_DECIMAL_PRECISION: Integer
|
|
17
102
|
|
|
18
|
-
def initialize: (?separator: String) -> void
|
|
19
|
-
def encode: (untyped payload) -> String
|
|
103
|
+
def initialize: (?separator: String?, ?pretty: bool, ?decimal_mode: Symbol, ?comments: Hash[String, String]?) -> void
|
|
104
|
+
def encode: (untyped payload, ?io: IO?) -> String?
|
|
20
105
|
|
|
21
106
|
private
|
|
22
107
|
attr_reader separator: String
|
|
23
108
|
attr_reader io: StringIO
|
|
109
|
+
attr_reader pretty: bool
|
|
110
|
+
attr_reader indent_level: Integer
|
|
111
|
+
attr_reader decimal_mode: Symbol
|
|
112
|
+
attr_reader comments: Hash[String, String]
|
|
24
113
|
|
|
25
114
|
def encode_root: (untyped value) -> void
|
|
26
115
|
def encode_top_level_pair: (String | Symbol key, untyped value) -> void
|
|
27
116
|
def encode_value: (untyped value, context: Symbol) -> void
|
|
28
117
|
def encode_object: (Hash[untyped, untyped] hash) -> void
|
|
29
118
|
def encode_array: (Array[untyped] list) -> void
|
|
30
|
-
def encode_table: (Array[Hash[untyped, untyped]] rows) -> void
|
|
119
|
+
def encode_table: (Array[Hash[untyped, untyped]] rows, Array[String | Symbol] header) -> void
|
|
31
120
|
def encode_scalar_list: (Array[untyped] list) -> void
|
|
32
121
|
def encode_mixed_list: (Array[untyped] list) -> void
|
|
33
122
|
def encode_scalar: (untyped value) -> void
|
|
34
|
-
def
|
|
123
|
+
def scalar_to_string: (untyped value) -> String
|
|
124
|
+
def format_string: (String value) -> String
|
|
35
125
|
def format_number: (Numeric value) -> String
|
|
36
126
|
def normalize_decimal_string: (String string) -> String
|
|
37
127
|
def zero_string?: (String string) -> bool
|
|
38
128
|
def float_decimal_string: (Numeric value) -> String
|
|
129
|
+
def precise_float_decimal_string: (Numeric value) -> String
|
|
39
130
|
def format_key: (String | Symbol key) -> String
|
|
40
131
|
def string_needs_quotes?: (String value) -> bool
|
|
41
132
|
def numeric_like?: (String value) -> bool
|
|
42
133
|
def quote_string: (String value) -> String
|
|
43
134
|
def escape_string: (String value) -> String
|
|
44
135
|
def scalar?: (untyped value) -> bool
|
|
45
|
-
def
|
|
136
|
+
def table_schema_for: (Array[untyped] rows) -> Array[String | Symbol]?
|
|
137
|
+
def compute_table_schema: (Array[untyped] rows) -> Array[String | Symbol]?
|
|
138
|
+
def fast_scalar_stream?: (Array[untyped] list) -> bool
|
|
139
|
+
def homogeneous_scalar_tokens?: (Array[untyped] list) -> bool
|
|
140
|
+
def token_does_not_require_quotes?: (untyped value) -> bool
|
|
141
|
+
def fast_scalar_stream: (Array[untyped] list) -> String
|
|
142
|
+
def indent: -> void
|
|
143
|
+
def outdent: -> void
|
|
144
|
+
def newline: -> void
|
|
145
|
+
def emit_comment_for: (String key) -> void
|
|
46
146
|
end
|
|
47
147
|
|
|
48
148
|
class Decoder
|
|
49
149
|
TERMINATORS: Array[String]
|
|
150
|
+
KEY_VALUE_BOUNDARY_TOKENS: Array[String]
|
|
151
|
+
SAFE_KEY_PATTERN: Regexp
|
|
152
|
+
INTEGER_PATTERN: Regexp
|
|
153
|
+
FLOAT_PATTERN: Regexp
|
|
50
154
|
|
|
51
155
|
def initialize: (?symbolize_names: bool) -> void
|
|
52
156
|
def decode: (String cton) -> untyped
|
|
@@ -54,7 +158,11 @@ module Cton
|
|
|
54
158
|
private
|
|
55
159
|
attr_reader symbolize_names: bool
|
|
56
160
|
attr_reader scanner: StringScanner
|
|
161
|
+
attr_reader raw_string: String
|
|
57
162
|
|
|
163
|
+
def raise_error: (String message, ?suggestions: Array[String]?) -> bot
|
|
164
|
+
def calculate_location: (Integer pos) -> [Integer, Integer]
|
|
165
|
+
def extract_source_excerpt: (Integer pos, ?length: Integer) -> String
|
|
58
166
|
def parse_document: -> Hash[untyped, untyped]
|
|
59
167
|
def parse_value_for_key: -> untyped
|
|
60
168
|
def parse_object: -> Hash[untyped, untyped]
|
|
@@ -66,7 +174,11 @@ module Cton
|
|
|
66
174
|
def parse_scalar: (?allow_key_boundary: bool) -> untyped
|
|
67
175
|
def scan_until_terminator: -> String?
|
|
68
176
|
def scan_until_boundary_or_terminator: -> String?
|
|
177
|
+
def consume_slice: (Integer start_pos, Integer end_pos) -> String?
|
|
178
|
+
def find_terminator_position: (Integer start_pos) -> Integer
|
|
69
179
|
def find_key_boundary: (Integer from_index) -> Integer?
|
|
180
|
+
def terminator?: (String char) -> bool
|
|
181
|
+
def boundary_start_allowed?: (String char) -> bool
|
|
70
182
|
def convert_scalar: (String token) -> untyped
|
|
71
183
|
def parse_string: -> String
|
|
72
184
|
def parse_key_name: -> (String | Symbol)
|
|
@@ -74,6 +186,7 @@ module Cton
|
|
|
74
186
|
def symbolize_keys: (Hash[untyped, untyped] row) -> Hash[untyped, untyped]
|
|
75
187
|
def expect!: (String char) -> void
|
|
76
188
|
def skip_ws: -> void
|
|
189
|
+
def skip_ws_and_comments: -> void
|
|
77
190
|
def whitespace?: (String char) -> bool
|
|
78
191
|
def key_ahead?: -> bool
|
|
79
192
|
def safe_key_char?: (String? char) -> bool
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cton
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Davide Santangelo
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-11-
|
|
11
|
+
date: 2025-11-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: CTON provides a JSON-compatible, token-efficient text representation
|
|
14
14
|
optimized for LLM prompts.
|
|
@@ -29,6 +29,9 @@ files:
|
|
|
29
29
|
- lib/cton.rb
|
|
30
30
|
- lib/cton/decoder.rb
|
|
31
31
|
- lib/cton/encoder.rb
|
|
32
|
+
- lib/cton/stats.rb
|
|
33
|
+
- lib/cton/type_registry.rb
|
|
34
|
+
- lib/cton/validator.rb
|
|
32
35
|
- lib/cton/version.rb
|
|
33
36
|
- sig/cton.rbs
|
|
34
37
|
homepage: https://github.com/davidesantangelo/cton
|