jsonseq 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/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +109 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jsonseq.gemspec +26 -0
- data/lib/jsonseq/reader.rb +117 -0
- data/lib/jsonseq/version.rb +3 -0
- data/lib/jsonseq/writer.rb +22 -0
- data/lib/jsonseq.rb +10 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b144f33248f3ccd78a6b98186e784495fedb0360
|
4
|
+
data.tar.gz: 501a8853494e00a3cce9f0879b70e82c67ef1954
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 58de576b8bde2111ead891cc97cd4a6bb4e5473ab5fa4449d98b6c34ae03811593691c26e0c88d49b7d291ebc25a6bc989df0477975a998ea8561f87b500bdb2
|
7
|
+
data.tar.gz: f522886c6a8eb61199dd470e0101f30933106c1329e3a93148583a863ea0e074fe880a98b824e7695af65e438d29f08ba88b7b65afecabf188de40614d7f6464
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# JSONSEQ
|
2
|
+
|
3
|
+
JSONSEQ implements reader and writer of JSON Text Sequence defined in [RFC 7464](https://tools.ietf.org/html/rfc7464).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jsonseq'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install jsonseq
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Reading JSON Text Sequence
|
24
|
+
|
25
|
+
Use `JSONSEQ::Reader` to read sequence of JSON objects.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require "jsonseq"
|
29
|
+
|
30
|
+
reader = JSONSEQ::Reader.new(io: io)
|
31
|
+
|
32
|
+
result = reader.read
|
33
|
+
object = reader.read_object
|
34
|
+
```
|
35
|
+
|
36
|
+
`JSONSEQ::Reader#read` tries to read one object from given IO and returns one of:
|
37
|
+
|
38
|
+
* `JSONSEQ::Reader::JSONObject`
|
39
|
+
* `JSONSEQ::Reader::ParsingError`
|
40
|
+
* `JSONSEQ::Reader::MaybeTruncated`
|
41
|
+
* `JSONSEQ::Reader::EndOfFile`
|
42
|
+
|
43
|
+
`JSONSEQ::Reader#read_object` tries to read one object from given IO, and returns if successfully read one JSON object.
|
44
|
+
When reached to end of file, returns `nil`.
|
45
|
+
Note that `read_object` returns `nil` also when it reads `null`.
|
46
|
+
|
47
|
+
It also provides `#each` and `#each_object`.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
reader.each do |result|
|
51
|
+
case result
|
52
|
+
when JSONSEQ::Reader::JSONObject
|
53
|
+
# ...
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
### Writing JSON Text Sequence
|
59
|
+
|
60
|
+
Use `JSONSEQ::Writer` to write sequence of JSON objects.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require "jsonseq"
|
64
|
+
|
65
|
+
writer = JSONSEQ::Writer.new(io: io)
|
66
|
+
|
67
|
+
writer << [1,2,3]
|
68
|
+
writer.write nil
|
69
|
+
```
|
70
|
+
|
71
|
+
It tries to `flush` given IO when it writes an object.
|
72
|
+
|
73
|
+
### Customizing JSON encoding/decoding
|
74
|
+
|
75
|
+
You can pass `encoder:` and `decoder:` option.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
JSONSEQ::Reader.new(io: io, decoder: -> (object) { JSON.parse(object, allow_nan: true) })
|
79
|
+
JSONSEQ::Writer.new(io: io, encoder: -> (object) { JSON.pretty_generate(object) })
|
80
|
+
```
|
81
|
+
|
82
|
+
## Incompatibility
|
83
|
+
|
84
|
+
### `JSONSEQ::Reader` accepts a sequence without the first *record separator*
|
85
|
+
|
86
|
+
The following input looks like invalid because it does not start with `"\x1E"` (*record separator*).
|
87
|
+
|
88
|
+
```
|
89
|
+
[1,2,3]
|
90
|
+
\x1E{}
|
91
|
+
```
|
92
|
+
|
93
|
+
However, `JSONSEQ::Reader` accepts that input.
|
94
|
+
|
95
|
+
### IO character encoding
|
96
|
+
|
97
|
+
RFC 7464 defines the format on UTF-8 encoded stream.
|
98
|
+
However, this library is implemented without considering IO encoding.
|
99
|
+
|
100
|
+
## Development
|
101
|
+
|
102
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
|
+
|
104
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/soutaro/jsonseq.
|
109
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jsonseq"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/jsonseq.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jsonseq/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jsonseq"
|
8
|
+
spec.version = JSONSEQ::VERSION
|
9
|
+
spec.authors = ["Soutaro Matsumoto"]
|
10
|
+
spec.email = ["matsumoto@soutaro.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Read/write JSON Text Sequence.}
|
13
|
+
spec.description = %q{Read/write JSON text sequence based on RFC7464.}
|
14
|
+
spec.homepage = "https://github.com/soutaro/jsonseq"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module JSONSEQ
|
2
|
+
class Reader
|
3
|
+
DEFAULT_DECODER = -> (string) { JSON.parse(string) }
|
4
|
+
|
5
|
+
class JSONObject
|
6
|
+
attr_reader :object
|
7
|
+
|
8
|
+
def initialize(object:)
|
9
|
+
@object = object
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class EndOfFile
|
14
|
+
end
|
15
|
+
|
16
|
+
class ParsingError
|
17
|
+
attr_reader :source
|
18
|
+
attr_reader :exception
|
19
|
+
|
20
|
+
def initialize(source:, exception:)
|
21
|
+
@source = source
|
22
|
+
@exception = exception
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MaybeTruncated
|
27
|
+
attr_reader :source
|
28
|
+
attr_reader :object
|
29
|
+
|
30
|
+
def initialize(source:, object:)
|
31
|
+
@source = source
|
32
|
+
@object = object
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :io
|
37
|
+
attr_reader :decoder
|
38
|
+
|
39
|
+
def initialize(io:, decoder: DEFAULT_DECODER)
|
40
|
+
@io = io
|
41
|
+
@decoder = decoder
|
42
|
+
end
|
43
|
+
|
44
|
+
def read
|
45
|
+
begin
|
46
|
+
source = io.readline(RS).chomp(RS)
|
47
|
+
end while source == ""
|
48
|
+
|
49
|
+
object = decode_string(source)
|
50
|
+
|
51
|
+
case object
|
52
|
+
when Numeric, TrueClass, FalseClass, nil
|
53
|
+
if truncated?(source)
|
54
|
+
MaybeTruncated.new(source: source, object: object)
|
55
|
+
else
|
56
|
+
JSONObject.new(object: object)
|
57
|
+
end
|
58
|
+
when ParsingError
|
59
|
+
object
|
60
|
+
else
|
61
|
+
JSONObject.new(object: object)
|
62
|
+
end
|
63
|
+
rescue EOFError
|
64
|
+
EndOfFile.new
|
65
|
+
end
|
66
|
+
|
67
|
+
def each
|
68
|
+
if block_given?
|
69
|
+
while true
|
70
|
+
object = read
|
71
|
+
yield object
|
72
|
+
return if object.is_a?(EndOfFile)
|
73
|
+
end
|
74
|
+
else
|
75
|
+
enum_for :each
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def read_object
|
80
|
+
value = read
|
81
|
+
case value
|
82
|
+
when MaybeTruncated, ParsingError
|
83
|
+
read_object
|
84
|
+
when EndOfFile
|
85
|
+
nil
|
86
|
+
when JSONObject
|
87
|
+
value.object
|
88
|
+
else
|
89
|
+
raise "Unexpected value: #{value}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def each_object
|
94
|
+
if block_given?
|
95
|
+
each do |value|
|
96
|
+
if value.is_a?(JSONObject)
|
97
|
+
yield value.object
|
98
|
+
end
|
99
|
+
end
|
100
|
+
else
|
101
|
+
enum_for :each_object
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def decode_string(string)
|
108
|
+
decoder[string]
|
109
|
+
rescue => exn
|
110
|
+
ParsingError.new(source: string, exception: exn)
|
111
|
+
end
|
112
|
+
|
113
|
+
def truncated?(string)
|
114
|
+
!string.end_with?(LF, "\x20", "\x09", "\x0d")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module JSONSEQ
|
2
|
+
class Writer
|
3
|
+
DEFAULT_ENCODER = -> (object) { JSON.dump(object) }
|
4
|
+
|
5
|
+
attr_reader :io
|
6
|
+
attr_reader :encoder
|
7
|
+
|
8
|
+
def initialize(io:, encoder: DEFAULT_ENCODER)
|
9
|
+
@io = io
|
10
|
+
@encoder = encoder
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(object)
|
14
|
+
io.write RS
|
15
|
+
io.write encoder[object]
|
16
|
+
io.write LF
|
17
|
+
io.flush
|
18
|
+
end
|
19
|
+
|
20
|
+
alias write <<
|
21
|
+
end
|
22
|
+
end
|
data/lib/jsonseq.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonseq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Soutaro Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Read/write JSON text sequence based on RFC7464.
|
56
|
+
email:
|
57
|
+
- matsumoto@soutaro.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- jsonseq.gemspec
|
70
|
+
- lib/jsonseq.rb
|
71
|
+
- lib/jsonseq/reader.rb
|
72
|
+
- lib/jsonseq/version.rb
|
73
|
+
- lib/jsonseq/writer.rb
|
74
|
+
homepage: https://github.com/soutaro/jsonseq
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.6.8
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Read/write JSON Text Sequence.
|
97
|
+
test_files: []
|