sjson 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +50 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +66 -0
- data/README.md +105 -0
- data/Rakefile +12 -0
- data/lib/sjson/version.rb +5 -0
- data/lib/sjson.rb +315 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d0eec3e8fd1e87c15d2eab57a4cd336425e275856b4a4bf0d5643e4c56b7294
|
4
|
+
data.tar.gz: 28d7c3609967b904c994b7f767f596a296208af7057b95c3512a807cfb042ac6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d6b9993bd675f5193723095b9cd3dad8b89ef6c009e80eefea078230991f5931dfc269989932af4c108e3e07944a7270b4a8b92bb4c6bd93898f6db0def0f6b
|
7
|
+
data.tar.gz: efc0571efcfba251ea11ae56b90ecd736a24f52e0a0ec0813714a86caefd36a7a1ff2cafe043b2946efcedc4d94bc5cb2834f0cc2e9bb17a2e2b8a678e0df8d8
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/StringLiterals:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: double_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Layout/LineLength:
|
14
|
+
Max: 120
|
15
|
+
|
16
|
+
Metrics/AbcSize:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/MethodLength:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/BlockLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Layout/FirstHashElementIndentation:
|
26
|
+
EnforcedStyle: consistent
|
27
|
+
|
28
|
+
Layout/EndAlignment:
|
29
|
+
EnforcedStyleAlignWith: start_of_line
|
30
|
+
|
31
|
+
Naming/MethodParameterName:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Layout/MultilineMethodCallIndentation:
|
35
|
+
EnforcedStyle: indented
|
36
|
+
|
37
|
+
Layout/CaseIndentation:
|
38
|
+
EnforcedStyle: end
|
39
|
+
|
40
|
+
Style/ZeroLengthPredicate:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Metrics/ClassLength:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Metrics/CyclomaticComplexity:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Metrics/PerceivedComplexity:
|
50
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hey@vito.io. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in sjson.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem "simplecov"
|
15
|
+
|
16
|
+
gem "simplecov_json_formatter"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sjson (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
docile (1.4.0)
|
12
|
+
json (2.6.3)
|
13
|
+
parallel (1.22.1)
|
14
|
+
parser (3.2.1.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
rainbow (3.1.1)
|
17
|
+
rake (13.0.6)
|
18
|
+
regexp_parser (2.7.0)
|
19
|
+
rexml (3.2.5)
|
20
|
+
rspec (3.12.0)
|
21
|
+
rspec-core (~> 3.12.0)
|
22
|
+
rspec-expectations (~> 3.12.0)
|
23
|
+
rspec-mocks (~> 3.12.0)
|
24
|
+
rspec-core (3.12.1)
|
25
|
+
rspec-support (~> 3.12.0)
|
26
|
+
rspec-expectations (3.12.2)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.12.0)
|
29
|
+
rspec-mocks (3.12.3)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.12.0)
|
32
|
+
rspec-support (3.12.0)
|
33
|
+
rubocop (1.45.1)
|
34
|
+
json (~> 2.3)
|
35
|
+
parallel (~> 1.10)
|
36
|
+
parser (>= 3.2.0.0)
|
37
|
+
rainbow (>= 2.2.2, < 4.0)
|
38
|
+
regexp_parser (>= 1.8, < 3.0)
|
39
|
+
rexml (>= 3.2.5, < 4.0)
|
40
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
41
|
+
ruby-progressbar (~> 1.7)
|
42
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
43
|
+
rubocop-ast (1.24.1)
|
44
|
+
parser (>= 3.1.1.0)
|
45
|
+
ruby-progressbar (1.11.0)
|
46
|
+
simplecov (0.22.0)
|
47
|
+
docile (~> 1.1)
|
48
|
+
simplecov-html (~> 0.11)
|
49
|
+
simplecov_json_formatter (~> 0.1)
|
50
|
+
simplecov-html (0.12.3)
|
51
|
+
simplecov_json_formatter (0.1.4)
|
52
|
+
unicode-display_width (2.4.2)
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
arm64-darwin-22
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
rake (~> 13.0)
|
59
|
+
rspec (~> 3.0)
|
60
|
+
rubocop (~> 1.21)
|
61
|
+
simplecov
|
62
|
+
simplecov_json_formatter
|
63
|
+
sjson!
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.4.2
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Sjson
|
2
|
+
|
3
|
+
Sjson handles JSON streams and ensures a complete JSON object has been read
|
4
|
+
before returning it to the caller, ensuring it is minimally sane for parsing.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
$ bundle add sjson
|
11
|
+
|
12
|
+
If bundler is not being used to manage dependencies, install the gem by
|
13
|
+
executing:
|
14
|
+
|
15
|
+
$ gem install sjson
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Sjson takes characters from a stream, and returns the JSON string once the
|
20
|
+
structure is complete. For instance:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'sjson'
|
24
|
+
require 'json'
|
25
|
+
|
26
|
+
handler = Sjson.new
|
27
|
+
|
28
|
+
data = StringIO.new(<<-JSON)
|
29
|
+
{ "name": "Paul Appleseed" }
|
30
|
+
JSON
|
31
|
+
|
32
|
+
read = data.readpartial(14) # Reads '{ "name": "Pau'
|
33
|
+
read.each_char do |chr|
|
34
|
+
result = handler.feed(chr) # result is always nil.
|
35
|
+
end
|
36
|
+
|
37
|
+
read = data.readpartial(13) # Reads 'l Appleseed" '
|
38
|
+
read.each_char do |chr|
|
39
|
+
result = handler.feed(chr) # result is always nil.
|
40
|
+
end
|
41
|
+
|
42
|
+
# Feeding the last character that completes the structure returns the structure
|
43
|
+
# data:
|
44
|
+
|
45
|
+
read = data.readpartial(1) # Reads '}'
|
46
|
+
result = handler.feed(read) # Feed the last read character
|
47
|
+
result
|
48
|
+
# => "{\"name\":\"Paul Appleseed\"}"
|
49
|
+
|
50
|
+
# Which can now be parsed
|
51
|
+
JSON.parse(result)
|
52
|
+
# => {"name"=>"Paul Appleseed"}
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
58
|
+
run `rake spec` to run the tests. You can also run `bin/console` for an
|
59
|
+
interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
62
|
+
release a new version, update the version number in `version.rb`, and then run
|
63
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
64
|
+
git commits and the created tag, and push the `.gem` file to
|
65
|
+
[rubygems.org](https://rubygems.org).
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
Bug reports and pull requests are welcome on GitHub at
|
70
|
+
https://github.com/heyvito/sjson. This project is intended to be a safe,
|
71
|
+
welcoming space for collaboration, and contributors are expected to adhere
|
72
|
+
to the [code of conduct](https://github.com/heyvito/sjson/blob/master/CODE_OF_CONDUCT.md).
|
73
|
+
|
74
|
+
## Code of Conduct
|
75
|
+
|
76
|
+
Everyone interacting in the Sjson project's codebases, issue trackers, chat
|
77
|
+
rooms and mailing lists is expected to follow the
|
78
|
+
[code of conduct](https://github.com/heyvito/sjson/blob/master/CODE_OF_CONDUCT.md).
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
```
|
83
|
+
The MIT License (MIT)
|
84
|
+
|
85
|
+
Copyright (c) 2023 Victor Gama
|
86
|
+
|
87
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
88
|
+
of this software and associated documentation files (the "Software"), to deal
|
89
|
+
in the Software without restriction, including without limitation the rights
|
90
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
91
|
+
copies of the Software, and to permit persons to whom the Software is
|
92
|
+
furnished to do so, subject to the following conditions:
|
93
|
+
|
94
|
+
The above copyright notice and this permission notice shall be included in
|
95
|
+
all copies or substantial portions of the Software.
|
96
|
+
|
97
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
98
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
99
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
100
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
101
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
102
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
103
|
+
THE SOFTWARE.
|
104
|
+
|
105
|
+
```
|
data/Rakefile
ADDED
data/lib/sjson.rb
ADDED
@@ -0,0 +1,315 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
require_relative "sjson/version"
|
6
|
+
|
7
|
+
# Sjson handles JSON streams and ensures a complete JSON object has been read
|
8
|
+
# before returning it to the caller, ensuring it is minimally sane for parsing
|
9
|
+
class Sjson
|
10
|
+
class ParseError < StandardError; end
|
11
|
+
|
12
|
+
PFALSE = :p_false
|
13
|
+
PTRUE = :p_true
|
14
|
+
PNULL = :p_null
|
15
|
+
PSTRING = :p_string
|
16
|
+
POBJECT = :p_object
|
17
|
+
PARRAY = :p_array
|
18
|
+
PNUMBER = :p_number
|
19
|
+
POBJECT_KEY = :p_object_key
|
20
|
+
POBJECT_VALUE = :p_object_value
|
21
|
+
|
22
|
+
QUOTE = '"'
|
23
|
+
LEFT_CURLY = "{"
|
24
|
+
RIGHT_CURLY = "}"
|
25
|
+
LEFT_SQUARED = "["
|
26
|
+
RIGHT_SQUARED = "]"
|
27
|
+
MINUS = "-"
|
28
|
+
PLUS = "+"
|
29
|
+
DOT = "."
|
30
|
+
NEWLINE = "\n"
|
31
|
+
CARRIAGE_RETURN = "\r"
|
32
|
+
TAB = "\t"
|
33
|
+
SPACE = " "
|
34
|
+
COMMA = ","
|
35
|
+
COLON = ":"
|
36
|
+
E_DOWN = "e"
|
37
|
+
E_UP = "E"
|
38
|
+
WSP_CHARS = [SPACE, NEWLINE, CARRIAGE_RETURN, TAB].freeze
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@data = StringIO.new
|
42
|
+
@stack = []
|
43
|
+
end
|
44
|
+
|
45
|
+
def reset
|
46
|
+
@data.truncate(0)
|
47
|
+
@data.rewind
|
48
|
+
@stack.clear
|
49
|
+
end
|
50
|
+
|
51
|
+
def feed_all(s)
|
52
|
+
s.each_char do |chr|
|
53
|
+
res = feed(chr)
|
54
|
+
return res if res
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def feed(c)
|
59
|
+
return parse_value(c) if @stack.empty?
|
60
|
+
|
61
|
+
catch(:break_loop) do
|
62
|
+
loop do
|
63
|
+
catch(:retry) do
|
64
|
+
fail! "unexpected character '#{c}', as the parser state is not ready to handle it" if @stack.empty?
|
65
|
+
|
66
|
+
case state.first
|
67
|
+
when PFALSE
|
68
|
+
parse_false(c)
|
69
|
+
when PTRUE
|
70
|
+
parse_true(c)
|
71
|
+
when PNULL
|
72
|
+
parse_null(c)
|
73
|
+
when PNUMBER
|
74
|
+
parse_number(c)
|
75
|
+
when PSTRING
|
76
|
+
parse_string(c)
|
77
|
+
when PARRAY
|
78
|
+
parse_array(c)
|
79
|
+
when POBJECT
|
80
|
+
parse_object(c)
|
81
|
+
when POBJECT_KEY
|
82
|
+
parse_object_key(c)
|
83
|
+
when POBJECT_VALUE
|
84
|
+
parse_object_value(c)
|
85
|
+
else
|
86
|
+
# :nocov:
|
87
|
+
fail! "BUG: Unexpected parser state #{state.first}"
|
88
|
+
# :nocov:
|
89
|
+
end
|
90
|
+
|
91
|
+
throw :break_loop
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
return @data.string.dup if @stack.empty?
|
97
|
+
|
98
|
+
nil
|
99
|
+
end
|
100
|
+
alias << feed
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def wsp?(chr)
|
105
|
+
WSP_CHARS.include?(chr)
|
106
|
+
end
|
107
|
+
|
108
|
+
def push_state(state)
|
109
|
+
@stack << [state, @data.length - 1]
|
110
|
+
end
|
111
|
+
|
112
|
+
def state
|
113
|
+
@stack.last
|
114
|
+
end
|
115
|
+
|
116
|
+
def fail!(why)
|
117
|
+
raise(ParseError, "#{why} at position #{@data.length - 1}")
|
118
|
+
end
|
119
|
+
|
120
|
+
def pop_state
|
121
|
+
@stack.pop
|
122
|
+
end
|
123
|
+
|
124
|
+
def replace_state(new)
|
125
|
+
pop_state
|
126
|
+
push_state(new)
|
127
|
+
end
|
128
|
+
|
129
|
+
def retry!
|
130
|
+
pop_state
|
131
|
+
throw :retry
|
132
|
+
end
|
133
|
+
|
134
|
+
def append(c)
|
135
|
+
@data.write(c)
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
|
139
|
+
def prev_rel_byte
|
140
|
+
return 0x00 if @data.length.zero? || @data.length - 1 < state.last
|
141
|
+
|
142
|
+
data_at @data.length - 1
|
143
|
+
end
|
144
|
+
|
145
|
+
def prev_byte
|
146
|
+
return 0x00 if @data.length.zero?
|
147
|
+
|
148
|
+
data_at @data.length - 1
|
149
|
+
end
|
150
|
+
|
151
|
+
def data_at(idx)
|
152
|
+
return 0x00 if idx >= @data.length
|
153
|
+
|
154
|
+
@data.seek(idx, IO::SEEK_SET)
|
155
|
+
@data.getc.tap { @data.seek(0, IO::SEEK_END) }
|
156
|
+
end
|
157
|
+
|
158
|
+
def data_from(idx)
|
159
|
+
@data.seek(idx, IO::SEEK_SET)
|
160
|
+
@data.read.tap { @data.seek(0, IO::SEEK_END) }
|
161
|
+
end
|
162
|
+
|
163
|
+
def handle_word_parsing(word, c)
|
164
|
+
idx = @data.length - state.last
|
165
|
+
|
166
|
+
fail! "expected #{word[idx]} (reading '#{word}'), found '#{c} instead" if c != word[idx]
|
167
|
+
|
168
|
+
append(c)
|
169
|
+
|
170
|
+
pop_state if idx == word.length - 1
|
171
|
+
end
|
172
|
+
|
173
|
+
def parse_false(c)
|
174
|
+
handle_word_parsing("false", c)
|
175
|
+
end
|
176
|
+
|
177
|
+
def parse_true(c)
|
178
|
+
handle_word_parsing("true", c)
|
179
|
+
end
|
180
|
+
|
181
|
+
def parse_null(c)
|
182
|
+
handle_word_parsing("null", c)
|
183
|
+
end
|
184
|
+
|
185
|
+
def parse_string(c)
|
186
|
+
prev_rel = prev_rel_byte
|
187
|
+
append(c)
|
188
|
+
pop_state if c == QUOTE && prev_rel != "\\"
|
189
|
+
end
|
190
|
+
|
191
|
+
def parse_array(c)
|
192
|
+
return if wsp?(c)
|
193
|
+
|
194
|
+
prev_rel = prev_rel_byte
|
195
|
+
|
196
|
+
if c == RIGHT_SQUARED && prev_rel != COMMA
|
197
|
+
append(c)
|
198
|
+
pop_state
|
199
|
+
return
|
200
|
+
elsif c == COMMA && prev_rel != LEFT_SQUARED && prev_rel != COMMA
|
201
|
+
append(c)
|
202
|
+
return
|
203
|
+
end
|
204
|
+
|
205
|
+
return parse_value(c) if [LEFT_SQUARED, COMMA].include?(prev_rel)
|
206
|
+
|
207
|
+
fail! "expected ',', found `#{c}' instead"
|
208
|
+
end
|
209
|
+
|
210
|
+
def parse_object(c)
|
211
|
+
if c == RIGHT_CURLY
|
212
|
+
append(c)
|
213
|
+
pop_state
|
214
|
+
return
|
215
|
+
end
|
216
|
+
|
217
|
+
push_state POBJECT_KEY
|
218
|
+
throw :retry
|
219
|
+
end
|
220
|
+
|
221
|
+
def parse_object_key(c)
|
222
|
+
return if wsp?(c)
|
223
|
+
|
224
|
+
prev = prev_byte
|
225
|
+
if c != QUOTE && prev != QUOTE
|
226
|
+
fail! "expected '\"', found `#{c}"
|
227
|
+
elsif c == QUOTE && prev != QUOTE
|
228
|
+
append(c)
|
229
|
+
push_state PSTRING
|
230
|
+
return nil
|
231
|
+
end
|
232
|
+
|
233
|
+
fail! "expected ':', found #{c}" if c != COLON
|
234
|
+
|
235
|
+
append(c)
|
236
|
+
replace_state POBJECT_VALUE
|
237
|
+
nil
|
238
|
+
end
|
239
|
+
|
240
|
+
def parse_object_value(c)
|
241
|
+
return if wsp?(c)
|
242
|
+
|
243
|
+
prev_rel = prev_rel_byte
|
244
|
+
retry! if prev_rel != COLON && c == RIGHT_CURLY
|
245
|
+
|
246
|
+
if prev_rel != COLON && c == COMMA
|
247
|
+
append(c)
|
248
|
+
replace_state POBJECT_KEY
|
249
|
+
return nil
|
250
|
+
end
|
251
|
+
|
252
|
+
return parse_value(c) if prev_rel == COLON
|
253
|
+
|
254
|
+
fail! "unexpected `#{c}'"
|
255
|
+
end
|
256
|
+
|
257
|
+
def parse_value(c)
|
258
|
+
return if wsp?(c)
|
259
|
+
|
260
|
+
append(c)
|
261
|
+
|
262
|
+
case c
|
263
|
+
when "t"
|
264
|
+
push_state(PTRUE)
|
265
|
+
when "f"
|
266
|
+
push_state(PFALSE)
|
267
|
+
when "n"
|
268
|
+
push_state(PNULL)
|
269
|
+
when QUOTE
|
270
|
+
push_state(PSTRING)
|
271
|
+
when LEFT_CURLY
|
272
|
+
push_state(POBJECT)
|
273
|
+
when LEFT_SQUARED
|
274
|
+
push_state(PARRAY)
|
275
|
+
else
|
276
|
+
if c == MINUS || (c >= "0" && c <= "9")
|
277
|
+
push_state(PNUMBER)
|
278
|
+
return
|
279
|
+
end
|
280
|
+
|
281
|
+
fail! "expected t, f, n, \", {, [, -, or a number from 0-9, got `#{c}'"
|
282
|
+
end
|
283
|
+
|
284
|
+
nil
|
285
|
+
end
|
286
|
+
|
287
|
+
def parse_number(c)
|
288
|
+
prev_rel = prev_rel_byte
|
289
|
+
prev_parse = data_from(state.last)
|
290
|
+
|
291
|
+
case c
|
292
|
+
when MINUS
|
293
|
+
fail! "unexpected '-'" unless [0x00, E_DOWN, E_UP].include? prev_rel
|
294
|
+
return append(c)
|
295
|
+
when PLUS
|
296
|
+
fail! "unexpected '+'" unless [E_DOWN, E_UP].include? prev_rel
|
297
|
+
return append(c)
|
298
|
+
when DOT
|
299
|
+
fail! "unexpected '.'" if [DOT, E_DOWN, E_UP].any? { prev_parse.include? _1 } || prev_rel == MINUS
|
300
|
+
return append(c)
|
301
|
+
when E_DOWN, E_UP
|
302
|
+
fail! "unexpected '#{c}', expected a number" if prev_rel < "0" || prev_rel > "9"
|
303
|
+
return append(c)
|
304
|
+
when RIGHT_SQUARED, RIGHT_CURLY, COMMA, CARRIAGE_RETURN, NEWLINE, SPACE, TAB
|
305
|
+
fail! "unexpected '#{c}', expected a number" if [E_DOWN, E_UP, PLUS, MINUS, DOT].include?(prev_rel)
|
306
|
+
retry!
|
307
|
+
end
|
308
|
+
|
309
|
+
fail! "unexpected '#{c}'" if c < "0" || c > "9"
|
310
|
+
|
311
|
+
fail! "invalid number format" if ["-0", "0"].include?(prev_parse)
|
312
|
+
|
313
|
+
append(c)
|
314
|
+
end
|
315
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sjson
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Victor Gama
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Sjson handles json streams
|
14
|
+
email:
|
15
|
+
- hey@vito.io
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CODE_OF_CONDUCT.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/sjson.rb
|
28
|
+
- lib/sjson/version.rb
|
29
|
+
homepage: https://github.com/heyvito/sjson.rb
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata:
|
33
|
+
homepage_uri: https://github.com/heyvito/sjson.rb
|
34
|
+
source_code_uri: https://github.com/heyvito/sjson.rb
|
35
|
+
changelog_uri: https://github.com/heyvito/sjson.rb
|
36
|
+
rubygems_mfa_required: 'true'
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.6.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.4.2
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Sjson handles json streams
|
56
|
+
test_files: []
|