json_rander 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7517ed283f406c8fbb2beec11d2795031284b2bc
4
+ data.tar.gz: 4143aba120fab75d4519656b6a61969ad3b2296b
5
+ SHA512:
6
+ metadata.gz: 971ffebffff92c8f8902f4459393e6c3dd9690126e4dc929850e577e34f2ac1616add0f35688fe55fc5f86d93aa986686d78decc1789aeedd930d422749ece57
7
+ data.tar.gz: cb1d3f82277709ec8b5e0130b3cc810ba9ad58a59b4b8383a03ff3b81470889030c97258e69e41c80740b5a1f35bc96dcd50240cd9b720868d6ab88d8ef67d19
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 fdwills
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # json-rander
2
+ random json string builder
3
+
4
+ ## install
5
+ include in Gemfile
6
+
7
+ gem 'json_rander'
8
+
9
+ install by bundle
10
+
11
+ bundle install
12
+ ## usage:
13
+
14
+ require 'json_rander'
15
+ puts JsonRander.get_random
data/json.txt ADDED
@@ -0,0 +1,136 @@
1
+ # ================syntax====================
2
+ # syntax error : lack element
3
+ false|
4
+ false| null
5
+ false| { : }
6
+ false| { : 123}
7
+ false| { "string" }
8
+ false| { 123 }
9
+ false| { "string" : }
10
+ false| {,}
11
+ false| [,]
12
+ false| [123, ]
13
+ false| {"string" : "string", }
14
+
15
+ # syntax error : lack syntax
16
+ # consider \[ \{ \[ \} as syntax error
17
+ # ex: {"aaa" : {\} }
18
+ false| {
19
+ false| }
20
+ false| [123
21
+ false| 123]
22
+ false| {"string" "string"}
23
+ false| {"string" : [\]}
24
+
25
+ # syntax error : syntax error
26
+ false| {"string", "string"}
27
+ false| ["string" : "string"]
28
+
29
+ # ================string====================
30
+ # string only
31
+ false| "string"
32
+
33
+ # special string
34
+ true| ["\""]
35
+ true| ["\\"]
36
+ true| ["\/"]
37
+ true| ["\b"]
38
+ true| ["\f"]
39
+ true| ["\n"]
40
+ true| ["\t"]
41
+ false| ["\u"]
42
+ true| ["\u4032"]
43
+ true| ["あ"]
44
+
45
+ # ================number====================
46
+ # number only
47
+ false| 12345
48
+
49
+ true| [123]
50
+ true| [-0.123]
51
+ true| [123e+111]
52
+ true| [-1.00123e+111]
53
+ true| [-1.00123E-111]
54
+
55
+ # ================object====================
56
+ # 0. general
57
+ # void object
58
+ true| {}
59
+
60
+ # single pair
61
+ true| { "string" : "string" }
62
+
63
+ # muitl pairs
64
+ true| { "string1" : "string1", "string2" : "string2"}
65
+
66
+ # 1. about value
67
+ # string value
68
+ true| { "string" : "value"}
69
+
70
+ # number value
71
+ true| { "string" : 123 }
72
+
73
+ # value null
74
+ true| { "string" : null }
75
+
76
+ # bool value
77
+ true| { "string" : true }
78
+ true| { "string" : false }
79
+
80
+ # value object null
81
+ true| { "string" : {} }
82
+
83
+ # object value
84
+ true| { "string" : { "string" : 123} }
85
+
86
+ # array value
87
+ true| { "string" : [1, 3] }
88
+
89
+ # 2. about key
90
+ # key null
91
+ false| { null : "string" }
92
+
93
+ # bool value
94
+ false| { true : "string" }
95
+ false| { false : "string" }
96
+
97
+ # string key
98
+ true| { "string" : "string"}
99
+ true| { "" : "string" }
100
+
101
+ # number key
102
+ false| { 123 : "string" }
103
+
104
+ # object key
105
+ false| { { "string" : "value" } : "value" }
106
+
107
+ # array key
108
+ false| { [123] : "value" }
109
+
110
+ # ========================array=======================
111
+ # 0. general
112
+ # void array
113
+ true| []
114
+
115
+ # single value
116
+ true| [1]
117
+
118
+ # multi values
119
+ true| [1, 2]
120
+
121
+ # 1. about value
122
+ # number value
123
+ true| [1]
124
+
125
+ # string value
126
+ true| ["string"]
127
+ true| [""]
128
+
129
+ # array value
130
+ true| [[]]
131
+ true| [ [1,2] ]
132
+
133
+ # abject value
134
+ true| [{}]
135
+ true| [ {"string" : "value"} ]
136
+
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path(File.join(File.dirname(__FILE__),'lib/'))
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'json_rander/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'json_rander'
9
+ s.version = JsonRander::VERSION
10
+ s.date = '2014-01-28'
11
+ s.summary = "generate random json string"
12
+ s.description = "generate random json string"
13
+ s.authors = ["wills"]
14
+ s.email = 'weirenzhong@gmail.com'
15
+ s.files = `git ls-files`.split($/)
16
+ s.homepage = 'https://github.com/fdwills/json_rander'
17
+ s.license = 'MIT'
18
+ end
@@ -0,0 +1,145 @@
1
+ require 'json_rander/json_num_builder'
2
+
3
+ module JsonRander
4
+
5
+ # all values types
6
+ $TYPE = ["JsonRander::JObject",
7
+ "JsonRander::JArray",
8
+ "JsonRander::JString",
9
+ "JsonRander::JNum",
10
+ "JsonRander::JSpecialValue"]
11
+
12
+ # max length of random string/array/hash
13
+ $MAX_RANDOM_LENGTH = 5
14
+
15
+ # json object: { xx : xx, yy :: yy}
16
+ class JObject
17
+ def initialize
18
+ @pairs = []
19
+
20
+ # add random json key/value pairs to json object
21
+ pair_num = Random.rand($MAX_RANDOM_LENGTH)
22
+ pair_num.times { self.add_random }
23
+ end
24
+
25
+ # get random key: string only
26
+ def rand_key
27
+ return JString.new
28
+ end
29
+
30
+ # get random value: string/num/hash/array
31
+ def rand_value
32
+ return Kernel.const_get($TYPE.sample).new
33
+ end
34
+
35
+ def add_random
36
+ @pairs << [rand_key, rand_value]
37
+ return self
38
+ end
39
+
40
+ # to json string
41
+ def to_s
42
+ key_value_strs = []
43
+ @pairs.each { | key, value | key_value_strs << "#{key.to_s} : #{value.to_s}" }
44
+ return "{ " + key_value_strs.join(", ") + " }"
45
+ end
46
+ end
47
+
48
+ # json array: [ xx, yy, zz]
49
+ class JArray
50
+ def initialize
51
+ @arrs = []
52
+
53
+ # add random json key/value pairs to json object
54
+ element_num = Random.rand($MAX_RANDOM_LENGTH)
55
+ element_num.times { self.add_random }
56
+ end
57
+
58
+ def add_random
59
+ @arrs << Kernel.const_get($TYPE.sample).new
60
+ return self
61
+ end
62
+
63
+ # to json string
64
+ def to_s
65
+ strs = []
66
+ @arrs.each { |value| strs << value.to_s}
67
+ return "[ " + strs.join(", ") + " ]"
68
+ end
69
+ end
70
+
71
+ # json string: 0..9a..zA..z\n\r\t\b\f etc
72
+ class JString
73
+ def initialize
74
+ chars = ("a".."z").to_a +
75
+ ("A".."Z").to_a +
76
+ ("0".."9").to_a +
77
+ ["\\\\", "\\\"", "\\/", "\\n", "\\t", "\\b", "\\f", "\\r"].to_a
78
+
79
+ unicode_chars = ("0".."9").to_a +
80
+ ("a".."f").to_a +
81
+ ("A".."F").to_a
82
+
83
+ @string = ""
84
+
85
+ # get random string with max length limited
86
+ string_length = Random.rand($MAX_RANDOM_LENGTH)
87
+ string_length.times do
88
+ case Random.rand(2)
89
+ when 0
90
+ # num/alp
91
+ @string << chars.sample
92
+ when 1
93
+ # unicode
94
+ @string << "\\u"
95
+ 4.times { @string << unicode_chars.sample}
96
+ end
97
+ end
98
+ end
99
+
100
+ # to json string
101
+ def to_s
102
+ return '"' + @string + '"'
103
+ end
104
+ end
105
+
106
+ # json special value: true/false/nil
107
+ class JSpecialValue
108
+ def initialize
109
+ value = [true, false, nil].sample
110
+ end
111
+
112
+ def to_s
113
+ case @value
114
+ when true
115
+ return 'true'
116
+ when false
117
+ return 'false'
118
+ when nil
119
+ return 'null'
120
+ end
121
+ end
122
+ end
123
+
124
+ # json num: build from JNumBuilder
125
+ class JNum
126
+ def initialize
127
+ @builder = JNumBuilder.instance
128
+ end
129
+
130
+ def to_s
131
+ return @builder.get_num.to_s
132
+ end
133
+ end
134
+
135
+ # correct json format builer
136
+ def self.get_random
137
+ # object({ xx : yy}) or array[xx, yy]
138
+ case Random.rand(2)
139
+ when 0
140
+ return JObject.new
141
+ when 1
142
+ return JArray.new
143
+ end
144
+ end
145
+ end #module
@@ -0,0 +1,146 @@
1
+ require 'singleton'
2
+
3
+ module JsonRander
4
+
5
+ # nodes at num walking map
6
+ # ref: http://www.json.org/json-ja.html
7
+ # node base class
8
+ class JNumNode
9
+ # next nodes symbol
10
+ def initialize(*nexts)
11
+ @next_nodes = nexts
12
+ end
13
+
14
+ # head node?
15
+ def is_head?
16
+ return false
17
+ end
18
+
19
+ # tail node?
20
+ def is_tail?
21
+ return false
22
+ end
23
+
24
+ # randomly get next node symbol
25
+ def random_next
26
+ return @next_nodes.sample
27
+ end
28
+
29
+ # to string method
30
+ def to_s
31
+ return ""
32
+ end
33
+ end
34
+
35
+ # node classes inhert from JNumNode
36
+ # head node class
37
+ class JNumHead < JNumNode
38
+ def is_head?
39
+ return true
40
+ end
41
+ end
42
+
43
+ # tail node class
44
+ class JNumTail < JNumNode
45
+ def is_tail?
46
+ return true
47
+ end
48
+ end
49
+
50
+ # '-'
51
+ class JNumMinus < JNumNode
52
+ def to_s
53
+ return '-'
54
+ end
55
+ end
56
+
57
+ # '+'
58
+ class JNumPlus < JNumNode
59
+ def to_s
60
+ return '+'
61
+ end
62
+ end
63
+
64
+ # 0~9
65
+ class JNumDigit < JNumNode
66
+ @@nums = (0..9).to_a
67
+ def to_s
68
+ return "#{@@nums.sample}"
69
+ end
70
+ end
71
+
72
+ # 1~9
73
+ class JNumPositiveDigit < JNumNode
74
+ @@nums = (1..9).to_a
75
+ def to_s
76
+ return "#{@@nums.sample}"
77
+ end
78
+ end
79
+
80
+ # 0
81
+ class JNumZero < JNumNode
82
+ def to_s
83
+ return '0'
84
+ end
85
+ end
86
+
87
+ # '.'
88
+ class JNumDot < JNumNode
89
+ def to_s
90
+ return '.'
91
+ end
92
+ end
93
+
94
+ # 'E'
95
+ class JNumBigE < JNumNode
96
+ def to_s
97
+ return 'E'
98
+ end
99
+ end
100
+
101
+ # 'e'
102
+ class JNumSmallE < JNumNode
103
+ def to_s
104
+ return 'e'
105
+ end
106
+ end
107
+
108
+ # Json Num Builder: singleton
109
+ class JNumBuilder
110
+ include Singleton
111
+
112
+ def initialize
113
+ @nodes = { n0: JNumHead.new(:n1, :n2, :n3),
114
+ n1: JNumMinus.new(:n2, :n3),
115
+ n2: JNumZero.new(:n5, :n7, :n8, :n12),
116
+ n3: JNumPositiveDigit.new(:n4, :n5, :n7, :n8, :n12),
117
+ n4: JNumDigit.new(:n3, :n4, :n5, :n7, :n8, :n12),
118
+ n5: JNumDot.new(:n6),
119
+ n6: JNumDigit.new(:n6, :n7, :n8, :n12),
120
+ n7: JNumBigE.new(:n9, :n10, :n11),
121
+ n8: JNumSmallE.new(:n9, :n10, :n11),
122
+ n9: JNumPlus.new(:n11),
123
+ n10: JNumMinus.new(:n11),
124
+ n11: JNumDigit.new(:n11, :n12),
125
+ n12: JNumTail.new
126
+ }
127
+ end
128
+
129
+ # get a valid json numer
130
+ def get_num
131
+ num_string = ""
132
+
133
+ # begin walking from :n0
134
+ current_node = @nodes[:n0]
135
+
136
+ # end walking when reaches tail
137
+ until current_node.is_tail? do
138
+ num_string << current_node.to_s
139
+ current_node = @nodes[current_node.random_next]
140
+ end
141
+
142
+ return num_string
143
+ end
144
+ end
145
+
146
+ end #module
@@ -0,0 +1,3 @@
1
+ module JsonRander
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_rander
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - wills
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: generate random json string
14
+ email: weirenzhong@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - LICENSE
21
+ - README.md
22
+ - json.txt
23
+ - json_rander.gemspec
24
+ - lib/json_rander.rb
25
+ - lib/json_rander/json_num_builder.rb
26
+ - lib/json_rander/version.rb
27
+ homepage: https://github.com/fdwills/json_rander
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.0.3
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: generate random json string
51
+ test_files: []