niclasnilsson-json2objects 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-08-29
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/json2objects.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ spec/json2objects_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on json2objects, see http://json2objects.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = json2objects
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 Niclas Nilsson
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/json2objects'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'json2objects' do
14
+ self.developer 'Niclas Nilsson', 'niclas@niclasnilsson.se'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,43 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'json'
5
+ require 'facets'
6
+
7
+ class Json2Objects
8
+ VERSION = '0.0.2'
9
+
10
+ def create(json)
11
+ data = JSON.parse(json)
12
+ create_object data
13
+ end
14
+
15
+ private
16
+
17
+ def create_object(data)
18
+ type_name = data['Type']
19
+ o = eval "#{type_name}.new"
20
+
21
+ data.each do |key, value|
22
+ next if key == "Type"
23
+
24
+ if value.class == Hash
25
+ raise "Hashes not supported yet"
26
+ elsif value.class == Array
27
+ eval "o.#{key.snakecase} = create_array value"
28
+ else
29
+ eval "o.#{key.snakecase} = #{value.inspect}"
30
+ end
31
+ end
32
+
33
+ o
34
+ end
35
+
36
+ def create_array(data)
37
+ arr = []
38
+ data.each do |k|
39
+ arr << create_object(k)
40
+ end
41
+ arr
42
+ end
43
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/json2objects.rb'}"
9
+ puts "Loading json2objects gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ class Person
4
+ attr_accessor :id, :name, :married, :age, :weight, :addresses
5
+
6
+ end
7
+
8
+ class Address
9
+ attr_accessor :street, :zip, :city
10
+ end
11
+
12
+
13
+ class Json2ObjectsSpec
14
+
15
+ describe "Json2Objects" do
16
+ it "should parse simple things" do
17
+ json = <<EOT
18
+ {
19
+ "Type":"Person",
20
+ "Id":620049,
21
+ "Name":"John Smith",
22
+ "Age":32,
23
+ "Weight":73.5
24
+ }
25
+ EOT
26
+ objectifyer = Json2Objects.new
27
+
28
+ person = objectifyer.create(json)
29
+
30
+ person.class.should == Person
31
+ person.id.should == 620049
32
+ person.name.should == 'John Smith'
33
+ person.age.should == 32
34
+ person.weight.should == 73.5
35
+ end
36
+
37
+ it "should create nested objects in lists" do
38
+ json = <<EOT
39
+ {
40
+ "Type":"Person",
41
+ "Id":620049,
42
+ "Name":"John Smith",
43
+ "Married":true,
44
+ "Age":32,
45
+ "Weight":73.5,
46
+ "Addresses":
47
+ [
48
+ {
49
+ "Type":"Address",
50
+ "Street":"Storgatan 3",
51
+ "Zip":"311 01",
52
+ "City":"Falkenberg"
53
+ },
54
+ {
55
+ "Type":"Address",
56
+ "Street":"Avenyn 1",
57
+ "Zip":"411 72",
58
+ "City":"Gothenburg"
59
+ }
60
+ ]
61
+ }
62
+ EOT
63
+
64
+ objectifyer = Json2Objects.new
65
+
66
+ person = objectifyer.create(json)
67
+
68
+ person.class.should == Person
69
+ person.id.should == 620049
70
+ person.name.should == 'John Smith'
71
+ person.married.should == true
72
+ person.age.should == 32
73
+ person.weight.should == 73.5
74
+
75
+ person.addresses.size.should == 2
76
+ person.addresses.class.should == Array
77
+ person.addresses[0].class.should == Address
78
+ person.addresses[1].class.should == Address
79
+
80
+ person.addresses[0].street.should == "Storgatan 3"
81
+ person.addresses[1].street.should == "Avenyn 1"
82
+
83
+ person.addresses[0].zip.should == "311 01"
84
+ person.addresses[1].zip.should == "411 72"
85
+
86
+ person.addresses[0].city.should == "Falkenberg"
87
+ person.addresses[1].city.should == "Gothenburg"
88
+
89
+ end
90
+
91
+
92
+ end
93
+
94
+ end
95
+
96
+
97
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'json2objects'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: niclasnilsson-json2objects
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Niclas Nilsson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: A gem for serializing and deserializing json to objects
26
+ email:
27
+ - niclas@niclasnilsson.se
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - PostInstall.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - PostInstall.txt
40
+ - README.rdoc
41
+ - Rakefile
42
+ - lib/json2objects.rb
43
+ - script/console
44
+ - script/destroy
45
+ - script/generate
46
+ - spec/json2objects_spec.rb
47
+ - spec/spec.opts
48
+ - spec/spec_helper.rb
49
+ - tasks/rspec.rake
50
+ has_rdoc: false
51
+ homepage: http://github.com/#{github_username}/#{project_name}
52
+ licenses:
53
+ post_install_message: PostInstall.txt
54
+ rdoc_options:
55
+ - --main
56
+ - README.rdoc
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: json2objects
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A gem for serializing and deserializing json to objects
78
+ test_files: []
79
+