niclasnilsson-json2objects 0.0.3 → 0.0.4
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.
- data/lib/objects2json.rb +29 -0
- metadata +4 -2
data/lib/objects2json.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module Objects2Json
|
|
4
|
+
|
|
5
|
+
def to_json(*a)
|
|
6
|
+
result = "{ \"type\":\"#{self.class.name}\""
|
|
7
|
+
json_attrs.each do |property|
|
|
8
|
+
value = json(property)
|
|
9
|
+
result += ", #{value}" if value.length > 0
|
|
10
|
+
end
|
|
11
|
+
result += " }"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def json(property)
|
|
15
|
+
value = send property
|
|
16
|
+
return "" if value.nil?
|
|
17
|
+
return "\"#{property.to_s}\":[ #{json_array(value)} ]" if value.class == Array
|
|
18
|
+
"\"#{property.to_s}\":#{value.inspect}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def json_array(a)
|
|
22
|
+
separator = ""
|
|
23
|
+
a.map { |e| res = separator + e.to_json; separator = ", "; res }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: niclasnilsson-json2objects
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Niclas Nilsson
|
|
@@ -40,6 +40,7 @@ files:
|
|
|
40
40
|
- README.rdoc
|
|
41
41
|
- Rakefile
|
|
42
42
|
- lib/json2objects.rb
|
|
43
|
+
- lib/objects2json.rb
|
|
43
44
|
- script/console
|
|
44
45
|
- script/destroy
|
|
45
46
|
- script/generate
|
|
@@ -49,6 +50,7 @@ files:
|
|
|
49
50
|
- tasks/rspec.rake
|
|
50
51
|
has_rdoc: false
|
|
51
52
|
homepage: http://github.com/#{github_username}/#{project_name}
|
|
53
|
+
licenses:
|
|
52
54
|
post_install_message: PostInstall.txt
|
|
53
55
|
rdoc_options:
|
|
54
56
|
- --main
|
|
@@ -70,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
70
72
|
requirements: []
|
|
71
73
|
|
|
72
74
|
rubyforge_project: json2objects
|
|
73
|
-
rubygems_version: 1.
|
|
75
|
+
rubygems_version: 1.3.5
|
|
74
76
|
signing_key:
|
|
75
77
|
specification_version: 3
|
|
76
78
|
summary: A gem for serializing and deserializing json to objects
|