json_to_ruby_class 0.1.3 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +23 -1
- data/json_to_ruby_class.gemspec +0 -1
- data/lib/json_to_ruby_class.rb +17 -19
- data/lib/json_to_ruby_class/c_sharp_converter.rb +35 -0
- data/lib/json_to_ruby_class/ruby_converter.rb +20 -0
- data/lib/json_to_ruby_class/vb_dot_net_converter.rb +34 -0
- data/lib/json_to_ruby_class/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5313ce74144a44ca9effc173b01de5c88a3666d2
|
|
4
|
+
data.tar.gz: a0e3fcbd3fb98ae3307fce2efe2ec3f090200f0d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 72504284558c27ca722c3720d81047b7cd960823bf83bc5e3eac76825611da87fe0e92f355a84d050d698fe492632846e83579d24f810d72781e4c69ab4653a8
|
|
7
|
+
data.tar.gz: 7e2fd4f86226c557c45932490459bc0edc03bb04ca04eaa996552d49ddce6c2a9919e57f11e5214b70054fa51f743b74c0e6a1e1d931ac93c959d83b12099f61
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -58,6 +58,28 @@ class Example
|
|
|
58
58
|
end
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
Furthermore, the following types can be used to retrieve the proper classes in other languages:
|
|
62
|
+
* 'c#', for C# classes
|
|
63
|
+
* 'vb', for VB.net classes
|
|
64
|
+
|
|
65
|
+
Example use:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
puts JsonToRubyClass.produce_models(json, 'c#')
|
|
69
|
+
|
|
70
|
+
public class Student
|
|
71
|
+
{
|
|
72
|
+
public string firstName { get; set; }
|
|
73
|
+
public string lastName { get; set; }
|
|
74
|
+
public int age { get; set; }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public class Example
|
|
78
|
+
{
|
|
79
|
+
public Student[] students { get; set; }
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
61
83
|
## Development
|
|
62
84
|
|
|
63
85
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -66,7 +88,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
66
88
|
|
|
67
89
|
## Contributing
|
|
68
90
|
|
|
69
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arcanoid/json_to_ruby_class. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
|
70
92
|
|
|
71
93
|
|
|
72
94
|
## License
|
data/json_to_ruby_class.gemspec
CHANGED
|
@@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
-
|
|
22
21
|
spec.add_dependency 'activesupport'
|
|
23
22
|
spec.add_development_dependency "bundler", "~> 1.10"
|
|
24
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/json_to_ruby_class.rb
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
require "json_to_ruby_class/version"
|
|
2
|
+
require "json_to_ruby_class/ruby_converter"
|
|
3
|
+
require "json_to_ruby_class/c_sharp_converter"
|
|
4
|
+
require "json_to_ruby_class/vb_dot_net_converter"
|
|
2
5
|
require 'active_support'
|
|
3
6
|
require 'active_support/core_ext'
|
|
4
7
|
|
|
5
8
|
module JsonToRubyClass
|
|
6
|
-
|
|
9
|
+
VB_DOT_NET_LANGUAGE_TYPE = 'vb'
|
|
10
|
+
C_SHARP_LANGUAGE_TYPE = 'c#'
|
|
11
|
+
RUBY_LANGUAGE_TYPE = 'ruby'
|
|
12
|
+
|
|
13
|
+
def self.produce_models(hash, language = 'ruby')
|
|
7
14
|
models_array = collect_info_from_json(hash, nil)
|
|
8
|
-
|
|
15
|
+
|
|
16
|
+
case language
|
|
17
|
+
when C_SHARP_LANGUAGE_TYPE then CSharpConverter.prepare_c_sharp_models_from_hash models_array
|
|
18
|
+
when VB_DOT_NET_LANGUAGE_TYPE then VBDotNetConverter.prepare_vb_dot_net_models_from_hash models_array
|
|
19
|
+
else RubyConverter.prepare_ruby_models_from_hash models_array
|
|
20
|
+
end
|
|
9
21
|
end
|
|
10
22
|
|
|
11
23
|
#######
|
|
@@ -32,34 +44,20 @@ module JsonToRubyClass
|
|
|
32
44
|
end
|
|
33
45
|
end
|
|
34
46
|
|
|
35
|
-
accessors << key
|
|
47
|
+
accessors << { :key => "#{key.to_s}", :type => value.class }
|
|
36
48
|
end
|
|
37
49
|
|
|
38
50
|
model_name_to_be_used = (model_name.nil? ? 'Example' : model_name.to_s.camelcase)
|
|
39
|
-
accessors_to_be_used = accessors.map { |s| ":#{s.to_s.underscore}" }
|
|
40
51
|
|
|
41
52
|
if (hash = existing_models_array.find { |model| model[:name] == model_name_to_be_used })
|
|
42
|
-
hash[:accessors].push(
|
|
53
|
+
hash[:accessors].push(accessors).flatten!.uniq!
|
|
43
54
|
else
|
|
44
55
|
existing_models_array << {
|
|
45
56
|
:name => (model_name.nil? ? 'Example' : model_name.to_s.camelcase),
|
|
46
|
-
:accessors =>
|
|
57
|
+
:accessors => accessors
|
|
47
58
|
}
|
|
48
59
|
end
|
|
49
60
|
|
|
50
61
|
existing_models_array
|
|
51
62
|
end
|
|
52
|
-
|
|
53
|
-
def self.prepare_models_from_hash(models_array)
|
|
54
|
-
model_string = ''
|
|
55
|
-
|
|
56
|
-
models_array.each do |model|
|
|
57
|
-
model_string << "class #{model[:name].singularize}\n"
|
|
58
|
-
model_string << ' attr_accessor '
|
|
59
|
-
model_string << model[:accessors].join(",\n ")
|
|
60
|
-
model_string << "\nend\n\n"
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
model_string
|
|
64
|
-
end
|
|
65
63
|
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module JsonToRubyClass
|
|
2
|
+
class CSharpConverter
|
|
3
|
+
#######
|
|
4
|
+
private
|
|
5
|
+
#######
|
|
6
|
+
|
|
7
|
+
def self.prepare_c_sharp_models_from_hash(models_array)
|
|
8
|
+
model_string = ''
|
|
9
|
+
|
|
10
|
+
models_array.each do |model|
|
|
11
|
+
model_string << "public class #{model[:name].singularize}\n"
|
|
12
|
+
model_string << "{\n"
|
|
13
|
+
|
|
14
|
+
model[:accessors].each do |accessor|
|
|
15
|
+
type = case accessor[:type].to_s
|
|
16
|
+
when 'String' then 'string'
|
|
17
|
+
when 'Fixnum' then 'int'
|
|
18
|
+
when 'Float' then 'decimal'
|
|
19
|
+
when 'Array' then "#{accessor[:key].singularize.camelcase}[]"
|
|
20
|
+
when 'TrueClass' then 'bool'
|
|
21
|
+
when 'FalseClass' then 'bool'
|
|
22
|
+
when 'Hash' then "#{accessor[:key].singularize.camelcase}"
|
|
23
|
+
# TODO: How could you cover an array of integers?
|
|
24
|
+
else accessor[:type].to_s
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
model_string << " public #{type} #{accessor[:key]} { get; set; }\n"
|
|
28
|
+
end
|
|
29
|
+
model_string << "}\n\n"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
model_string
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module JsonToRubyClass
|
|
2
|
+
class RubyConverter
|
|
3
|
+
#######
|
|
4
|
+
private
|
|
5
|
+
#######
|
|
6
|
+
|
|
7
|
+
def self.prepare_ruby_models_from_hash(models_array)
|
|
8
|
+
model_string = ''
|
|
9
|
+
|
|
10
|
+
models_array.each do |model|
|
|
11
|
+
model_string << "class #{model[:name].singularize}\n"
|
|
12
|
+
model_string << ' attr_accessor '
|
|
13
|
+
model_string << model[:accessors].map { |accessor| ":#{accessor[:key].underscore}" }.join(",\n ")
|
|
14
|
+
model_string << "\nend\n\n"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
model_string
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module JsonToRubyClass
|
|
2
|
+
class VBDotNetConverter
|
|
3
|
+
#######
|
|
4
|
+
private
|
|
5
|
+
#######
|
|
6
|
+
|
|
7
|
+
def self.prepare_vb_dot_net_models_from_hash(models_array)
|
|
8
|
+
model_string = ''
|
|
9
|
+
|
|
10
|
+
models_array.each do |model|
|
|
11
|
+
model_string << "Public Class #{model[:name].singularize}\n"
|
|
12
|
+
|
|
13
|
+
model[:accessors].each do |accessor|
|
|
14
|
+
type = case accessor[:type].to_s
|
|
15
|
+
when 'String' then 'String'
|
|
16
|
+
when 'Fixnum' then 'Integer'
|
|
17
|
+
when 'Float' then 'Decimal'
|
|
18
|
+
when 'Array' then "#{accessor[:key].singularize.camelcase}()"
|
|
19
|
+
when 'TrueClass' then 'Boolean'
|
|
20
|
+
when 'FalseClass' then 'Boolean'
|
|
21
|
+
when 'Hash' then "#{accessor[:key].singularize.camelcase}"
|
|
22
|
+
# TODO: How could you cover an array of integers?
|
|
23
|
+
else accessor[:type].to_s
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
model_string << " Public Property #{accessor[:key]} As #{type}\n"
|
|
27
|
+
end
|
|
28
|
+
model_string << "End Class\n\n"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
model_string
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json_to_ruby_class
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vasilis Kalligas
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-08-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -86,6 +86,9 @@ files:
|
|
|
86
86
|
- bin/setup
|
|
87
87
|
- json_to_ruby_class.gemspec
|
|
88
88
|
- lib/json_to_ruby_class.rb
|
|
89
|
+
- lib/json_to_ruby_class/c_sharp_converter.rb
|
|
90
|
+
- lib/json_to_ruby_class/ruby_converter.rb
|
|
91
|
+
- lib/json_to_ruby_class/vb_dot_net_converter.rb
|
|
89
92
|
- lib/json_to_ruby_class/version.rb
|
|
90
93
|
homepage: https://rubygems.org/gems/json_to_ruby_class
|
|
91
94
|
licenses:
|