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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15d2f5dff6d3f941811d37ee84d8e789e3aa0160
4
- data.tar.gz: 18d8272bff2142be7f5842be8c32af429280ad5a
3
+ metadata.gz: 2b62f3eccb0904d6e26ae69676532a8017db82bc
4
+ data.tar.gz: cc524ae494417ecffe397a9ca9a21d6d8c3cfa61
5
5
  SHA512:
6
- metadata.gz: 6bb832dac8163b344f5be5eda23f24ff0f901242af6886b5cffcbceabd1938248867adb4c75539ab8eb666348a5f97cfc94451f4ffb6350cefad86c1d9a319a0
7
- data.tar.gz: c4b59eeeedab80b5964c544e05e566fec55e21269b0d68c2c68efab95c42e2bb162b2d8073b2fbeac7339fd0e52034cf315834cec33d9ec7baa53aff667a63b3
6
+ metadata.gz: 85572a67362b8993c255bbe2770636e36b22a15e05bfe3d2388d57b6c9a89020c55b5857ec2c82a9cfe75cd88d5f9a8c3c742b1e7a4a71790caa11597163f00f
7
+ data.tar.gz: cbf687da01e93d70ab53e7dd4a6c8972f342184d32feeccd867e896f6837fca2e2c66850cbf385d92371b8a8de63cf393e64935c27be6e7bce959ac6605c228e
@@ -0,0 +1,13 @@
1
+ inherit_gem:
2
+ meowcop:
3
+ - config/rubocop.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: true
10
+ style: always
11
+
12
+ Style/EmptyLineAfterMagicComment:
13
+ Enabled: true
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in mighty_json.gemspec
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require 'rake/testtask'
3
5
  task :default => :test
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Usage:
2
4
  # # The json is result of RuboCop in Readmine project
5
+ # # https://drive.google.com/file/d/0B0yU5j3W2zM9WW53b01jaWhFZHc/view?usp=sharing
3
6
  # $ ruby large.rb json
4
7
  #
5
8
  # Result:
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Usage:
2
4
  # $ ruby small.rb
3
5
  #
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "mighty_json"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "mighty_json/version"
2
4
  require 'mighty_json/errors'
3
5
  require 'mighty_json/type'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "mighty_json/version"
2
4
 
3
5
  module MightyJSON
@@ -16,6 +18,7 @@ module MightyJSON
16
18
  v = var.cur
17
19
  eval <<~END
18
20
  def this.coerce(#{v})
21
+ none = NONE
19
22
  #{type.compile(var: var, path: [])}
20
23
  end
21
24
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MightyJSON
2
4
  class Error < StandardError
3
5
  attr_reader :path, :type, :value
@@ -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
- 'NONE'
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? || NONE.equal?(#{v})
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}, i|
113
- #{@type.compile(var: var, path: path + [:array_index])} # TODO: optimize 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
- {}.tap do |#{result}|
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}.key?(#{key.inspect}) ? #{v}[#{key.inspect}] : NONE
167
+ #{new_var} = #{v}.fetch(#{key.inspect}, none)
149
168
  v = #{type.compile(var: var, path: path + [key])}
150
- if !NONE.equal?(v) &&
169
+ if !none.equal?(v) &&
151
170
  #{!NONE.equal?(type)} &&
152
- !(#{type.is_a?(Optional)} && NONE.equal?(#{new_var}))
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MightyJSON
2
4
  module Types
3
5
  def object(fields = {})
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Variable
2
4
  def initialize
3
5
  @count = 0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MightyJSON
2
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
3
5
  end
@@ -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.0
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-25 00:00:00.000000000 Z
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