mighty_json 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +13 -0
- data/Gemfile +2 -0
- data/README.md +6 -0
- data/Rakefile +2 -0
- data/benchmarks/large.rb +3 -0
- data/benchmarks/realtime.rb +26 -0
- data/benchmarks/small.rb +2 -0
- data/bin/console +1 -0
- data/lib/mighty_json.rb +2 -0
- data/lib/mighty_json/builder.rb +3 -0
- data/lib/mighty_json/errors.rb +2 -0
- data/lib/mighty_json/type.rb +27 -49
- data/lib/mighty_json/types.rb +2 -0
- data/lib/mighty_json/variable.rb +2 -0
- data/lib/mighty_json/version.rb +3 -1
- data/mighty_json.gemspec +5 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b62f3eccb0904d6e26ae69676532a8017db82bc
|
4
|
+
data.tar.gz: cc524ae494417ecffe397a9ca9a21d6d8c3cfa61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85572a67362b8993c255bbe2770636e36b22a15e05bfe3d2388d57b6c9a89020c55b5857ec2c82a9cfe75cd88d5f9a8c3c742b1e7a4a71790caa11597163f00f
|
7
|
+
data.tar.gz: cbf687da01e93d70ab53e7dd4a6c8972f342184d32feeccd867e896f6837fca2e2c66850cbf385d92371b8a8de63cf393e64935c27be6e7bce959ac6605c228e
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,8 +2,14 @@
|
|
2
2
|
|
3
3
|
A faster implementation of [soutaro/strong_json](https://github.com/soutaro/strong_json).
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/mighty_json.svg)](https://badge.fury.io/rb/mighty_json)
|
6
|
+
[![Build Status](https://travis-ci.org/pocke/mighty_json.svg?branch=master)](https://travis-ci.org/pocke/mighty_json)
|
7
|
+
|
5
8
|
## Benchmarking
|
6
9
|
|
10
|
+
|
11
|
+
![graph](https://cloud.githubusercontent.com/assets/4361134/26479369/9b5848c2-420d-11e7-9ada-83d5f16840df.png)
|
12
|
+
|
7
13
|
- In complex case, MightyJSON is faster 2.8x than StrongJSON ([code](https://github.com/pocke/mighty_json/blob/master/benchmarks/large.rb)).
|
8
14
|
- In simple case, MightyJSON is faster around 2x ~ 10x than StrongJSON ([code](https://github.com/pocke/mighty_json/blob/master/benchmarks/small.rb)).
|
9
15
|
|
data/Rakefile
CHANGED
data/benchmarks/large.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
# # The json is result of RuboCop in Readmine project
|
5
|
+
# # https://drive.google.com/file/d/0B0yU5j3W2zM9WW53b01jaWhFZHc/view?usp=sharing
|
6
|
+
# $ ruby realtime.rb json
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
require 'mighty_json'
|
10
|
+
require 'benchmark'
|
11
|
+
|
12
|
+
m = MightyJSON.new do
|
13
|
+
let :metadata, object(rubocop_version: string, ruby_engine: string, ruby_version: string, ruby_patchlevel: string, ruby_platform: string)
|
14
|
+
let :offense, object(severity: string, message: string, cop_name: string, corrected: boolean, location: object(line: number, column: number, length: number))
|
15
|
+
let :file, object(path: string, offenses: array(offense))
|
16
|
+
let :summary, object(offense_count: number, target_file_count: number, inspected_file_count: number)
|
17
|
+
|
18
|
+
let :o, object(metadata: metadata, files: array(file), summary: summary)
|
19
|
+
end
|
20
|
+
|
21
|
+
data = JSON.parse ARGF.read, symbolize_names: true
|
22
|
+
|
23
|
+
time = Benchmark.realtime do
|
24
|
+
20.times{m.o.coerce(data)}
|
25
|
+
end
|
26
|
+
p time
|
data/benchmarks/small.rb
CHANGED
data/bin/console
CHANGED
data/lib/mighty_json.rb
CHANGED
data/lib/mighty_json/builder.rb
CHANGED
data/lib/mighty_json/errors.rb
CHANGED
data/lib/mighty_json/type.rb
CHANGED
@@ -1,8 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module MightyJSON
|
2
4
|
module Type
|
3
5
|
NONE = Object.new
|
4
6
|
|
7
|
+
class Eval
|
8
|
+
def initialize(code)
|
9
|
+
@code = code
|
10
|
+
end
|
11
|
+
|
12
|
+
def inspect
|
13
|
+
@code
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
5
17
|
class Base
|
18
|
+
attr_reader :type
|
19
|
+
|
6
20
|
def initialize(type)
|
7
21
|
@type = type
|
8
22
|
end
|
@@ -15,7 +29,7 @@ module MightyJSON
|
|
15
29
|
if path == []
|
16
30
|
'raise IllegalTypeError.new(type: :ignored)'
|
17
31
|
else
|
18
|
-
'
|
32
|
+
'none'
|
19
33
|
end
|
20
34
|
when :any
|
21
35
|
v
|
@@ -67,7 +81,7 @@ module MightyJSON
|
|
67
81
|
def compile(var:, path:)
|
68
82
|
v = var.cur
|
69
83
|
<<~END
|
70
|
-
if #{v}.nil? ||
|
84
|
+
if #{v}.nil? || none.equal?(#{v})
|
71
85
|
nil
|
72
86
|
else
|
73
87
|
#{@type.compile(var: var, path: path)}
|
@@ -105,12 +119,13 @@ module MightyJSON
|
|
105
119
|
|
106
120
|
def compile(var:, path:)
|
107
121
|
v = var.cur
|
122
|
+
idx = var.next
|
108
123
|
child = var.next
|
109
124
|
<<~END
|
110
125
|
begin
|
111
126
|
raise Error.new(path: #{path.inspect}, type: #{self.to_s.inspect}, value: #{v}) unless #{v}.is_a?(::Array)
|
112
|
-
#{v}.map.with_index do |#{child},
|
113
|
-
#{@type.compile(var: var, path: path + [
|
127
|
+
#{v}.map.with_index do |#{child}, #{idx}|
|
128
|
+
#{@type.compile(var: var, path: path + [Eval.new(idx)])}
|
114
129
|
end
|
115
130
|
end
|
116
131
|
END
|
@@ -131,25 +146,29 @@ module MightyJSON
|
|
131
146
|
keys = @fields.keys.map(&:inspect)
|
132
147
|
|
133
148
|
result = var.next
|
149
|
+
is_fixed = @fields.values.none?{|type| type.is_a?(Optional) || (type.is_a?(Base) && type.type == :ignored)}
|
150
|
+
|
134
151
|
<<~END
|
135
152
|
begin
|
136
153
|
raise Error.new(path: #{path}, type: #{self.to_s.inspect}, value: object) unless #{v}.is_a?(Hash)
|
137
154
|
|
138
|
-
{}.
|
155
|
+
if #{!is_fixed} || #{@fields.size} != #{v}.size
|
139
156
|
#{v}.each do |key, value|
|
140
157
|
next if #{keys.map{|key| "#{key} == key"}.join('||')}
|
141
158
|
raise UnexpectedFieldError.new(path: #{path.inspect} + [key], value: #{v}) # TOOD: optimize path
|
142
159
|
end
|
160
|
+
end
|
143
161
|
|
162
|
+
{}.tap do |#{result}|
|
144
163
|
#{
|
145
164
|
@fields.map do |key, type|
|
146
165
|
new_var = var.next
|
147
166
|
<<~END2
|
148
|
-
#{new_var} = #{v}.
|
167
|
+
#{new_var} = #{v}.fetch(#{key.inspect}, none)
|
149
168
|
v = #{type.compile(var: var, path: path + [key])}
|
150
|
-
if !
|
169
|
+
if !none.equal?(v) &&
|
151
170
|
#{!NONE.equal?(type)} &&
|
152
|
-
!(#{type.is_a?(Optional)} &&
|
171
|
+
!(#{type.is_a?(Optional)} && none.equal?(#{new_var}))
|
153
172
|
#{result}[#{key.inspect}] = v
|
154
173
|
end
|
155
174
|
END2
|
@@ -196,46 +215,5 @@ module MightyJSON
|
|
196
215
|
"enum(#{types.map(&:to_s).join(", ")})"
|
197
216
|
end
|
198
217
|
end
|
199
|
-
|
200
|
-
class UnexpectedFieldError < StandardError
|
201
|
-
attr_reader :path, :value
|
202
|
-
|
203
|
-
def initialize(path: , value:)
|
204
|
-
@path = path
|
205
|
-
@value = value
|
206
|
-
end
|
207
|
-
|
208
|
-
def to_s
|
209
|
-
position = "#{path.join('.')}"
|
210
|
-
"Unexpected field of #{position} (#{value})"
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
class IllegalTypeError < StandardError
|
215
|
-
attr_reader :type
|
216
|
-
|
217
|
-
def initialize(type:)
|
218
|
-
@type = type
|
219
|
-
end
|
220
|
-
|
221
|
-
def to_s
|
222
|
-
"#{type} can not be put on toplevel"
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
class Error < StandardError
|
227
|
-
attr_reader :path, :type, :value
|
228
|
-
|
229
|
-
def initialize(path:, type:, value:)
|
230
|
-
@path = path
|
231
|
-
@type = type
|
232
|
-
@value = value
|
233
|
-
end
|
234
|
-
|
235
|
-
def to_s
|
236
|
-
position = path.empty? ? "" : " at .#{path.join('.')}"
|
237
|
-
"Expected type of value #{value}#{position} is #{type}"
|
238
|
-
end
|
239
|
-
end
|
240
218
|
end
|
241
219
|
end
|
data/lib/mighty_json/types.rb
CHANGED
data/lib/mighty_json/variable.rb
CHANGED
data/lib/mighty_json/version.rb
CHANGED
data/mighty_json.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
lib = File.expand_path("../lib", __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
require "mighty_json/version"
|
@@ -28,4 +30,7 @@ Gem::Specification.new do |spec|
|
|
28
30
|
|
29
31
|
spec.add_development_dependency 'minitest'
|
30
32
|
spec.add_development_dependency 'minitest-power_assert'
|
33
|
+
|
34
|
+
spec.add_development_dependency 'rubocop', '0.48.1'
|
35
|
+
spec.add_development_dependency 'meowcop', '1.10.0'
|
31
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mighty_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masataka Kuwabara
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,34 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.48.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.48.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: meowcop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.10.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.10.0
|
69
97
|
description: A faster implementation of soutaro/strong_json
|
70
98
|
email:
|
71
99
|
- kuwabara@pocke.me
|
@@ -74,6 +102,7 @@ extensions: []
|
|
74
102
|
extra_rdoc_files: []
|
75
103
|
files:
|
76
104
|
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
77
106
|
- ".travis.yml"
|
78
107
|
- Gemfile
|
79
108
|
- LICENSE.txt
|
@@ -81,6 +110,7 @@ files:
|
|
81
110
|
- README.md
|
82
111
|
- Rakefile
|
83
112
|
- benchmarks/large.rb
|
113
|
+
- benchmarks/realtime.rb
|
84
114
|
- benchmarks/small.rb
|
85
115
|
- bin/console
|
86
116
|
- bin/setup
|