zon-rb 0.1.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/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +109 -0
- data/Rakefile +12 -0
- data/lib/zon/lexer.rb +129 -0
- data/lib/zon/parser.rb +144 -0
- data/lib/zon/serializer.rb +104 -0
- data/lib/zon/version.rb +5 -0
- data/lib/zon/zon_grammar.treetop +48 -0
- data/lib/zon.rb +45 -0
- data/sig/zon.rbs +4 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 32686cf609ab53ad187e1ea5eab78453f396642b7a092c60d1104b843f7dd47c
|
4
|
+
data.tar.gz: e022ce6c1cb840580a105acc23ace93bf42ebca5001af78d31d2bda183e18319
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 13629fb58006c1ca81cecce5d3bf893f96835e3d927458de00745b0f0c3ccfea40a640d75bfb783c76433be2bcc5b83488c1576325237134f561d286f224e5c5
|
7
|
+
data.tar.gz: c0968e432df9bb625d9d6d49ad6104b6a0a9d9730180d7ef62a1efcb800e6405996184e39918a63ca45e6abae11b0834854ec365be8f90efc9949349bc7062d0
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 David P. Sugar
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# Zig Object Notation (ZON) for Ruby
|
2
|
+
|
3
|
+
The [Zig](https://ziglang.org/) Object Notation (ZON) is a file format primarily used within the Zig ecosystem. For example, ZON is used for the Zig package [manifest](https://github.com/ziglang/zig/blob/b7ab62540963d80f68d0e9ee7ce18520fb173487/doc/build.zig.zon.md).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
13
|
+
```
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To parse ZON data into a Ruby object, one can use the `Zon::parse` method. The method expects either a `String` or `IO` (`File`) object as its argument.
|
24
|
+
|
25
|
+
```irb
|
26
|
+
irb(main):001> Zon::parse(".{ \"foo\", \"bar\"}")
|
27
|
+
=> ["foo", "bar"]
|
28
|
+
irb(main):002> Zon.parse(File.open("../PassKeeZ/build.zig.zon"))
|
29
|
+
=>
|
30
|
+
{name: :passkeez,
|
31
|
+
version: "0.5.3",
|
32
|
+
minimum_zig_version: "0.15.1",
|
33
|
+
fingerprint: 15931082159778014966,
|
34
|
+
dependencies:
|
35
|
+
{keylib:
|
36
|
+
{url: "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz",
|
37
|
+
hash: "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA"},
|
38
|
+
uuid:
|
39
|
+
{url: "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz",
|
40
|
+
hash: "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i"},
|
41
|
+
kdbx: {path: "../kdbx"}},
|
42
|
+
paths: ["build.zig", "build.zig.zon", "linux", "README.md", "script", "src", "static"]}
|
43
|
+
```
|
44
|
+
|
45
|
+
To serialize a Ruby object into ZON, use the `Zon::serialize` method.
|
46
|
+
|
47
|
+
```irb
|
48
|
+
irb(main):002> o
|
49
|
+
=>
|
50
|
+
{name: :passkeez,
|
51
|
+
version: "0.5.3",
|
52
|
+
minimum_zig_version: "0.15.1",
|
53
|
+
fingerprint: 15931082159778014966,
|
54
|
+
dependencies:
|
55
|
+
{keylib: {url: "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz", hash: "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA"},
|
56
|
+
uuid: {url: "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz", hash: "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i"},
|
57
|
+
kdbx: {path: "../kdbx"}},
|
58
|
+
paths: ["build.zig", "build.zig.zon", "linux", "README.md", "script", "src", "static"]}
|
59
|
+
irb(main):013> puts(Zon::serialize(o, {:indent_level => 4}))
|
60
|
+
.{
|
61
|
+
.name = .passkeez,
|
62
|
+
.version = "0.5.3",
|
63
|
+
.minimum_zig_version = "0.15.1",
|
64
|
+
.fingerprint = 0xdd1692d15d21a6f6,
|
65
|
+
.dependencies = .{
|
66
|
+
.keylib = .{
|
67
|
+
.url = "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz",
|
68
|
+
.hash = "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA",
|
69
|
+
},
|
70
|
+
.uuid = .{
|
71
|
+
.url = "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz",
|
72
|
+
.hash = "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i",
|
73
|
+
},
|
74
|
+
.kdbx = .{
|
75
|
+
.path = "../kdbx",
|
76
|
+
},
|
77
|
+
},
|
78
|
+
.paths = .{
|
79
|
+
"build.zig",
|
80
|
+
"build.zig.zon",
|
81
|
+
"linux",
|
82
|
+
"README.md",
|
83
|
+
"script",
|
84
|
+
"src",
|
85
|
+
"static",
|
86
|
+
},
|
87
|
+
}
|
88
|
+
```
|
89
|
+
|
90
|
+
To customize the generated ZON data one can use the `:indent_level` and `:ibase` options. The `:indent_level` specifies how much each block is indented. The `:ibase` option can be used to define in which format (hex: 16, octal: 8, binary: 2, decimal: 10) an `Integer` should be serialized. The default for `:ibase` is `16` (hexadecimal).
|
91
|
+
|
92
|
+
```irb
|
93
|
+
irb(main):010> Zon::serialize(255, {:ibase => 2})
|
94
|
+
=> "0b11111111"
|
95
|
+
```
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
+
|
101
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/r4gus/zon-rb.
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/zon/lexer.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
module Zon
|
2
|
+
module Lexer
|
3
|
+
class TokenType
|
4
|
+
LITERAL = 0
|
5
|
+
STRING = 1
|
6
|
+
NUMBER = 2
|
7
|
+
BOOLEAN = 3
|
8
|
+
LBRACE = 4
|
9
|
+
RBRACE = 5
|
10
|
+
EQUALS = 6
|
11
|
+
COMMA = 7
|
12
|
+
end
|
13
|
+
|
14
|
+
class Token
|
15
|
+
attr_accessor :type
|
16
|
+
attr_accessor :value
|
17
|
+
|
18
|
+
def initialize(type, value)
|
19
|
+
@type = type
|
20
|
+
@value = value
|
21
|
+
end
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
self.type == other.type and self.value == other.value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.read(input)
|
29
|
+
curr = ""
|
30
|
+
in_comment = false
|
31
|
+
in_string = false
|
32
|
+
|
33
|
+
Enumerator.new do |y|
|
34
|
+
input.each_char.with_index do |char, idx|
|
35
|
+
# Handle comments
|
36
|
+
if not in_string and not in_comment and char == "/" and input[idx + 1] == "/"
|
37
|
+
if curr.length > 0
|
38
|
+
y << curr
|
39
|
+
curr = ""
|
40
|
+
end
|
41
|
+
in_comment = true
|
42
|
+
next
|
43
|
+
elsif in_comment and char.match?(/(\r\n|\n|\r)/)
|
44
|
+
# there are only line comments, i.e. every
|
45
|
+
# line needs to be commented separately.
|
46
|
+
in_comment = false
|
47
|
+
next
|
48
|
+
elsif in_comment
|
49
|
+
next
|
50
|
+
end
|
51
|
+
|
52
|
+
if not in_string and char.match?(/\s/) # is whitespace
|
53
|
+
if curr.length > 0
|
54
|
+
y << curr
|
55
|
+
curr = ""
|
56
|
+
end
|
57
|
+
next
|
58
|
+
end
|
59
|
+
|
60
|
+
if not in_string and char == "{"
|
61
|
+
if curr[-1] == "."
|
62
|
+
y << ".{"
|
63
|
+
curr = ""
|
64
|
+
else
|
65
|
+
raise "Invalid character #{curr[-1]} preceding '{' at index #{idx}"
|
66
|
+
end
|
67
|
+
elsif not in_string and ["}", "=", ","].include?(char)
|
68
|
+
y << curr if curr.length > 0
|
69
|
+
y << char
|
70
|
+
curr = ""
|
71
|
+
else
|
72
|
+
in_string = not in_string if char == "\"" and curr[-1] != "\\"
|
73
|
+
curr += char
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.parse(input)
|
80
|
+
Enumerator.new do |y|
|
81
|
+
self.read(input).with_index do |token_str, idx|
|
82
|
+
if token_str == ".{"
|
83
|
+
y << Token.new(TokenType::LBRACE, token_str)
|
84
|
+
elsif token_str == "}"
|
85
|
+
y << Token.new(TokenType::RBRACE, token_str)
|
86
|
+
elsif token_str == "="
|
87
|
+
y << Token.new(TokenType::EQUALS, token_str)
|
88
|
+
elsif token_str == ","
|
89
|
+
y << Token.new(TokenType::COMMA, token_str)
|
90
|
+
elsif token_str[0] == '"'
|
91
|
+
if token_str.length <= 1 or not token_str[-1] == '"'
|
92
|
+
raise "Token string at index #{idx} not terminated"
|
93
|
+
end
|
94
|
+
y << Token.new(TokenType::STRING, token_str)
|
95
|
+
elsif ["true", "false"].include?(token_str)
|
96
|
+
y << Token.new(TokenType::BOOLEAN, token_str)
|
97
|
+
elsif self.is_number? token_str
|
98
|
+
y << Token.new(TokenType::NUMBER, token_str)
|
99
|
+
elsif self.is_literal? token_str
|
100
|
+
y << Token.new(TokenType::LITERAL, token_str)
|
101
|
+
else
|
102
|
+
raise "Unknown token '#{token_str}' at index #{idx}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.is_literal?(token_str)
|
109
|
+
literal_regex = /\.[a-zA-Z][a-zA-Z_]*/
|
110
|
+
!!(token_str =~ literal_regex)
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.is_number?(token_str)
|
114
|
+
number_regex = /\A
|
115
|
+
[+-]? # optional sign
|
116
|
+
(
|
117
|
+
0[xX][0-9a-fA-F]+ # hex
|
118
|
+
|
|
119
|
+
0[bB][01]+ # binary
|
120
|
+
|
|
121
|
+
0[oO]?[0-7]+ # octal (0o77 or classic 077)
|
122
|
+
|
|
123
|
+
\d+ # decimal
|
124
|
+
)
|
125
|
+
\z/x
|
126
|
+
!!(token_str =~ number_regex)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/lib/zon/parser.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require_relative "lexer"
|
2
|
+
|
3
|
+
Lexer = Zon::Lexer
|
4
|
+
|
5
|
+
module Zon
|
6
|
+
|
7
|
+
class Parser
|
8
|
+
STRUCT = 0
|
9
|
+
ARRAY = 1
|
10
|
+
|
11
|
+
def initialize(tokens)
|
12
|
+
@tokens = tokens
|
13
|
+
@curr = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def next
|
17
|
+
begin
|
18
|
+
@curr = @tokens.next
|
19
|
+
rescue
|
20
|
+
@curr = nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse
|
25
|
+
if self.next_token? Lexer::TokenType::LBRACE
|
26
|
+
self.next
|
27
|
+
|
28
|
+
if self.next_token? Lexer::TokenType::RBRACE # '.{ }'
|
29
|
+
self.next
|
30
|
+
return {} # this could either be a array or a struct... we don't know
|
31
|
+
end
|
32
|
+
|
33
|
+
v = self.parse
|
34
|
+
|
35
|
+
if v.is_a? Symbol and self.next_token? Lexer::TokenType::EQUALS
|
36
|
+
# This is a Zon struct, i.e. translate it to a Ruby hash
|
37
|
+
k = v
|
38
|
+
self.next_equals # skip equals
|
39
|
+
v = self.parse
|
40
|
+
|
41
|
+
h = {k => v}
|
42
|
+
|
43
|
+
loop do # fetch remaining key value pairs
|
44
|
+
self.next if self.next_token? Lexer::TokenType::COMMA
|
45
|
+
if self.next_token? Lexer::TokenType::RBRACE
|
46
|
+
self.next
|
47
|
+
break
|
48
|
+
end
|
49
|
+
|
50
|
+
k = self.next_key
|
51
|
+
self.next_equals
|
52
|
+
v = self.parse
|
53
|
+
h[k] = v
|
54
|
+
end
|
55
|
+
|
56
|
+
return h
|
57
|
+
else
|
58
|
+
# Must be an array
|
59
|
+
|
60
|
+
a = [v]
|
61
|
+
|
62
|
+
loop do # fetch remaining values
|
63
|
+
self.next if self.next_token? Lexer::TokenType::COMMA
|
64
|
+
if self.next_token? Lexer::TokenType::RBRACE
|
65
|
+
self.next
|
66
|
+
break
|
67
|
+
end
|
68
|
+
|
69
|
+
a.append self.parse
|
70
|
+
end
|
71
|
+
|
72
|
+
return a
|
73
|
+
end
|
74
|
+
else
|
75
|
+
return self.next_value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def next_token?(type)
|
80
|
+
self.peek_token.type == type
|
81
|
+
end
|
82
|
+
|
83
|
+
def peek_token
|
84
|
+
begin
|
85
|
+
@tokens.peek
|
86
|
+
rescue
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def next_key
|
92
|
+
self.next
|
93
|
+
|
94
|
+
if @curr and @curr.type == Lexer::TokenType::LITERAL
|
95
|
+
@curr.value[1..].to_sym # create sym from string without the '.'
|
96
|
+
elsif
|
97
|
+
raise "Expected literal token but got '#{@curr.value}'"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def next_value
|
102
|
+
self.next
|
103
|
+
|
104
|
+
raise "No token for value" if not @curr
|
105
|
+
|
106
|
+
value = nil
|
107
|
+
|
108
|
+
if @curr.type == Lexer::TokenType::LITERAL
|
109
|
+
value = @curr.value[1..].to_sym
|
110
|
+
elsif @curr.type == Lexer::TokenType::STRING
|
111
|
+
value = @curr.value[1..-2]
|
112
|
+
elsif @curr.type == Lexer::TokenType::NUMBER
|
113
|
+
begin
|
114
|
+
if @curr.value.include? "."
|
115
|
+
value = Float(@curr.value)
|
116
|
+
else
|
117
|
+
value = Integer(@curr.value)
|
118
|
+
end
|
119
|
+
rescue
|
120
|
+
raise "Unable to parse number from value '#{@curr.value}'"
|
121
|
+
end
|
122
|
+
elsif @curr.type == Lexer::TokenType::BOOLEAN
|
123
|
+
if @curr.value == "true"
|
124
|
+
value = true
|
125
|
+
else
|
126
|
+
value = false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
raise "Unable to parse value from '#{@curr.value}'" if not value
|
131
|
+
|
132
|
+
value
|
133
|
+
end
|
134
|
+
|
135
|
+
def next_equals
|
136
|
+
self.next
|
137
|
+
|
138
|
+
if not @curr or not @curr.type == Lexer::TokenType::EQUALS
|
139
|
+
raise "Expected '=' token but got '#{@curr.value}'"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module Zon
|
2
|
+
module Serializer
|
3
|
+
def self.serialize_object(obj, options = {}, level = 0)
|
4
|
+
|
5
|
+
defaults = { :indent_level => 0, :ibase => 16 }
|
6
|
+
options = defaults.merge(options)
|
7
|
+
|
8
|
+
if obj.is_a? Integer
|
9
|
+
s = ""
|
10
|
+
case options[:ibase]
|
11
|
+
when 16
|
12
|
+
s += "0x"
|
13
|
+
when 8
|
14
|
+
s += "0o"
|
15
|
+
when 2
|
16
|
+
s += "0b"
|
17
|
+
end
|
18
|
+
|
19
|
+
s += obj.to_s(options[:ibase])
|
20
|
+
s
|
21
|
+
elsif obj.is_a? Float
|
22
|
+
String(obj)
|
23
|
+
elsif obj.is_a? String
|
24
|
+
"\"" + obj + "\""
|
25
|
+
elsif obj.is_a? Array
|
26
|
+
out = ""
|
27
|
+
l = obj.length
|
28
|
+
|
29
|
+
out += ".{"
|
30
|
+
if options[:indent_level] > 0
|
31
|
+
out += "\n"
|
32
|
+
end
|
33
|
+
|
34
|
+
obj.each.with_index do |elem, idx|
|
35
|
+
if options[:indent_level] > 0
|
36
|
+
out += " " * (options[:indent_level] * (level + 1))
|
37
|
+
else
|
38
|
+
out += " "
|
39
|
+
end
|
40
|
+
|
41
|
+
out += self.serialize_object(elem, options, level + 1)
|
42
|
+
|
43
|
+
if options[:indent_level] > 0
|
44
|
+
out += ",\n"
|
45
|
+
else
|
46
|
+
out += "," if idx < l - 1 # l is at least 1 otherwise this loop is not entered
|
47
|
+
out += " " if idx == l - 1 # last elem
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
if options[:indent_level] > 0
|
52
|
+
out += " " * (options[:indent_level] * level)
|
53
|
+
end
|
54
|
+
out += "}"
|
55
|
+
out
|
56
|
+
elsif obj.is_a? Hash
|
57
|
+
out = ""
|
58
|
+
l = obj.length
|
59
|
+
|
60
|
+
out += ".{"
|
61
|
+
if options[:indent_level] > 0
|
62
|
+
out += "\n"
|
63
|
+
end
|
64
|
+
|
65
|
+
obj.each.with_index do |kv, idx|
|
66
|
+
key = kv[0]
|
67
|
+
value = kv[1]
|
68
|
+
|
69
|
+
raise "Key #{key} must be a Symbol" if not key.is_a? Symbol
|
70
|
+
|
71
|
+
if options[:indent_level] > 0
|
72
|
+
out += " " * (options[:indent_level] * (level + 1))
|
73
|
+
else
|
74
|
+
out += " "
|
75
|
+
end
|
76
|
+
|
77
|
+
out += "." + String(key)
|
78
|
+
out += " = "
|
79
|
+
out += self.serialize_object(value, options, level + 1)
|
80
|
+
if options[:indent_level] > 0
|
81
|
+
out += ",\n"
|
82
|
+
else
|
83
|
+
out += "," if idx < l - 1 # l is at least 1 otherwise this loop is not entered
|
84
|
+
out += " " if idx == l - 1 # last elem
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
if options[:indent_level] > 0
|
89
|
+
out += " " * (options[:indent_level] * level)
|
90
|
+
end
|
91
|
+
out += "}"
|
92
|
+
out
|
93
|
+
elsif obj.is_a? TrueClass
|
94
|
+
"true"
|
95
|
+
elsif obj.is_a? FalseClass
|
96
|
+
"false"
|
97
|
+
elsif obj.is_a? Symbol
|
98
|
+
"." + String(obj)
|
99
|
+
else
|
100
|
+
raise ArgumentError, "Unable to serialize '#{obj}' of Class '#{obj.class}' to ZON"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/zon/version.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
grammar ZonGrammar
|
2
|
+
rule zon
|
3
|
+
struct / array
|
4
|
+
end
|
5
|
+
|
6
|
+
rule array
|
7
|
+
'.{' space a_value? (space ',' space a_value)* space '}'
|
8
|
+
end
|
9
|
+
|
10
|
+
rule struct
|
11
|
+
'.{' space pair? (space ',' space pair)* space '}'
|
12
|
+
end
|
13
|
+
|
14
|
+
rule a_value
|
15
|
+
struct / array / string / number / literal / true / false / null
|
16
|
+
end
|
17
|
+
|
18
|
+
rule space
|
19
|
+
[\s\n\t\r]*
|
20
|
+
end
|
21
|
+
|
22
|
+
rule pair
|
23
|
+
literal space '=' space value
|
24
|
+
{
|
25
|
+
def value(parent)
|
26
|
+
parent[elements[0].value.to_sym] = elements[4].value
|
27
|
+
end
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
rule literal
|
32
|
+
'.' [a-zA-Z_]+
|
33
|
+
{
|
34
|
+
def value
|
35
|
+
elements[1].text_value
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
rule string
|
41
|
+
'"' ([^"\\] / "\\" . )* '"'
|
42
|
+
{
|
43
|
+
def value(_env=nil)
|
44
|
+
elements[1].text_value
|
45
|
+
end
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
data/lib/zon.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "zon/version"
|
4
|
+
require_relative "zon/lexer"
|
5
|
+
require_relative "zon/parser"
|
6
|
+
require_relative "zon/serializer"
|
7
|
+
|
8
|
+
##
|
9
|
+
# Zig Object Notation (ZON) de-/serializer.
|
10
|
+
module Zon
|
11
|
+
class Error < StandardError; end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Parse ZON data, represented as String or IO/File, into
|
15
|
+
# a Ruby object.
|
16
|
+
#
|
17
|
+
# An ArgumentError is raised if input is neither a String nor
|
18
|
+
# a IO object.
|
19
|
+
def self.parse(input)
|
20
|
+
data = nil
|
21
|
+
|
22
|
+
if input.is_a? String
|
23
|
+
data = input
|
24
|
+
elsif input.is_a? IO
|
25
|
+
data = input.read
|
26
|
+
else
|
27
|
+
raise ArgumentError, "Input must be a String or a File/IO object"
|
28
|
+
end
|
29
|
+
|
30
|
+
tokens = Zon::Lexer.parse data
|
31
|
+
parser = Zon::Parser.new tokens
|
32
|
+
parser.parse
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Serialize a Ruby object into a ZON string.
|
37
|
+
#
|
38
|
+
# Available options:
|
39
|
+
# - { :indent_level => <Integer> } - Define the indentation level for each block
|
40
|
+
# - { :ibase => <Integer> } - Specify the base (binary: 2, octal: 8, decimal: 10, hex: 16)
|
41
|
+
def self.serialize(obj, options = {})
|
42
|
+
Zon::Serializer::serialize_object(obj, options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/sig/zon.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zon-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David P. Sugar
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-09-27 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: This gem allows you to translate ZON data into Ruby objects and vice
|
13
|
+
versa.
|
14
|
+
email:
|
15
|
+
- david@thesugar.de
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/zon.rb
|
27
|
+
- lib/zon/lexer.rb
|
28
|
+
- lib/zon/parser.rb
|
29
|
+
- lib/zon/serializer.rb
|
30
|
+
- lib/zon/version.rb
|
31
|
+
- lib/zon/zon_grammar.treetop
|
32
|
+
- sig/zon.rbs
|
33
|
+
homepage: https://github.com/r4gus/zon-rb
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/r4gus/zon-rb
|
38
|
+
source_code_uri: https://github.com/r4gus/zon-rb
|
39
|
+
changelog_uri: https://github.com/r4gus/zon-rb/blob/master/CHANGELOG.md
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.6.2
|
55
|
+
specification_version: 4
|
56
|
+
summary: Zig Object Notation (ZON) support for Ruby
|
57
|
+
test_files: []
|