Module: ToonFormat
- Defined in:
- lib/toon_format.rb,
lib/toon_format/errors.rb,
lib/toon_format/parser.rb,
lib/toon_format/decoder.rb,
lib/toon_format/encoder.rb,
lib/toon_format/railtie.rb,
lib/toon_format/version.rb,
lib/toon_format/validator.rb,
lib/toon_format/rails/extensions.rb
Defined Under Namespace
Modules: Rails Classes: DecodeError, Decoder, EncodeError, Encoder, Error, ParseError, Parser, Railtie, ValidationError, Validator
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.decode(toon_string, strict: true) ⇒ Object
Decode TOON format string to Ruby object.
-
.encode(data, **options) ⇒ String
Encode Ruby object to TOON format string.
-
.estimate_savings(data) ⇒ Hash
Estimate token savings compared to JSON.
Class Method Details
.decode(toon_string, strict: true) ⇒ Object
Decode TOON format string to Ruby object
41 42 43 |
# File 'lib/toon_format.rb', line 41 def decode(toon_string, strict: true) Decoder.decode(toon_string, strict: strict) end |
.encode(data, **options) ⇒ String
Encode Ruby object to TOON format string
28 29 30 |
# File 'lib/toon_format.rb', line 28 def encode(data, **) Encoder.encode(data, ) end |
.estimate_savings(data) ⇒ Hash
Estimate token savings compared to JSON
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/toon_format.rb', line 50 def estimate_savings(data) json_str = JSON.generate(data) toon_str = encode(data) json_size = json_str.bytesize toon_size = toon_str.bytesize { json_tokens: estimate_tokens(json_str), toon_tokens: estimate_tokens(toon_str), savings_percent: ((json_size - toon_size) / json_size.to_f * 100).round(1), json_size: json_size, toon_size: toon_size } end |