jsonj-integration 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/jsonj.rb +128 -0
  3. metadata +43 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: !binary |-
4
+ ZWNhY2RiNDU0NDM2MzcwMmUyZGM4NDM0NzI2MjRkZWY1MTlmMWVhZg==
5
+ data.tar.gz: !binary |-
6
+ ZTA4YThhYzI4YTdkN2ZjMTk3NTNkOWEwNGQ3ZDg2NTAwYTMxZWU4NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTRhMjIyNDljYmQzZDRmZmY0MDk2MGIyMmVhMDZjMWVmMmQ5ZWE1MWQ1Zjk1
10
+ ZDJhOGQ4Yzk3MWQ4YjU5Mjk4Nzk1NTI0MGJmNTY5ODZlZjYyNjhlMzAxNGRj
11
+ NDNhODJlNjEzNGQxMWNjYTliYWVhNTVjMjEwMzk4ZDY1NTgzZGM=
12
+ data.tar.gz: !binary |-
13
+ ZmI0OGIyMDIwYjA3ZTFlZmY4ZjVmMzRjNmE4YzcxZGVhMDQwYTY0YzkzZWQ5
14
+ YzFkYWE1NjBjMTgzM2E5MTQ4MGYwZWQwZWQ1M2U1YjI5NjkzMGNmN2JhNTE2
15
+ ZDllZjM5N2M2NTAwODY2ZTkzM2NlYjRhNzQwMDFhODE5ZjVkNGE=
@@ -0,0 +1,128 @@
1
+ #
2
+ # This module integrates jsonj into jruby by adding ruby functions to various classes.
3
+ # This allows you to use conversion methods on hashes, lists, and any JsonElement
4
+ # Additionally, you can use [] for getting, putting. and adding to JsonObject and JsonArray
5
+ #
6
+ # This makes the integration nearly seemless and enables you to, for example, use JsonJ enabled
7
+ # business logic written in Java from a jruby API layer (e.g. rails or sinatra based).
8
+ #
9
+ java_import com.github.jsonj.JsonObject
10
+ java_import com.github.jsonj.JsonArray
11
+ java_import com.github.jsonj.JsonPrimitive
12
+ java_import com.github.jsonj.JsonType
13
+ java_import com.github.jsonj.tools.JsonBuilder
14
+
15
+ # this function is injected into JsonElement subclasses
16
+ def convertToRuby(value)
17
+ if value.isObject
18
+ result = Hash.new
19
+
20
+ value.entrySet.each{|e|
21
+ result[e.getKey] = convertToRuby(e.getValue)
22
+ }
23
+ result
24
+ elsif value.isArray
25
+ result = Array.new
26
+ value.each{|v| result << convertToRuby(v)}
27
+ result
28
+ elsif value.isPrimitive
29
+ if(value.type == JsonType::string)
30
+
31
+ value.asString
32
+ else
33
+ value.value
34
+ end
35
+ end
36
+ end
37
+
38
+ # java objects don't expose toString() via ruby's to_s(), so we fix this dynamically for JsonElement subclasses
39
+ def tos(caller)
40
+ caller.toString
41
+ end
42
+
43
+ JsonObject.send(:define_method, 'to_s') do
44
+ tos self
45
+ end
46
+
47
+ JsonArray.send(:define_method, 'to_s') do
48
+ tos self
49
+ end
50
+
51
+ JsonPrimitive.send(:define_method, 'to_s') do
52
+ tos self
53
+ end
54
+
55
+ JsonObject.send(:define_method, 'toRuby') do
56
+ convertToRuby self
57
+ end
58
+
59
+ JsonObject.send(:define_method, '[]=') do |key,value|
60
+ self.put(key.to_s,JsonBuilder::fromObject(value))
61
+ end
62
+
63
+
64
+ JsonObject.send(:define_method, '[]') do |key|
65
+ val = self.get(key)
66
+ if val.isPrimitive
67
+ case val.type.to_s
68
+ when 'string'
69
+ val.asString
70
+ when 'number'
71
+ val.asDouble
72
+ when 'bool'
73
+ val.asBoolean
74
+ when 'nullValue'
75
+ nil
76
+ end
77
+ else
78
+ val
79
+ end
80
+ end
81
+
82
+ JsonArray.send(:define_method, '[]') do |key|
83
+ val = self.get(key)
84
+ if val.isPrimitive
85
+ case val.type.to_s
86
+ when 'string'
87
+ val.asString
88
+ when 'number'
89
+ val.asDouble
90
+ when 'bool'
91
+ val.asBoolean
92
+ when 'nullValue'
93
+ nil
94
+ end
95
+ else
96
+ val
97
+ end
98
+ end
99
+
100
+ JsonArray.send(:define_method, '[]=') do |index,value|
101
+ self.set(index,JsonBuilder::fromObject(value))
102
+ end
103
+
104
+ JsonArray.send(:define_method, '<<') do |value|
105
+ self.add(JsonBuilder::fromObject(value))
106
+ end
107
+
108
+ JsonArray.send(:define_method, 'toRuby') do
109
+ convertToRuby self
110
+ end
111
+
112
+ JsonPrimitive.send(:define_method, 'toRuby') do
113
+ if(self.type.toString == 'string')
114
+ self.asString
115
+ else
116
+ self.value
117
+ end
118
+ end
119
+
120
+ # make sure jruby hashes know how to convert themselves to JsonObject
121
+ Hash.send(:define_method, 'toJsonJ') do
122
+ JsonBuilder.fromObject(self)
123
+ end
124
+
125
+ # make sure jruby arrays know how to convert themselves to JsonArray
126
+ Array.send(:define_method, 'toJsonJ') do
127
+ JsonBuilder.fromObject(self)
128
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonj-integration
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Jilles van Gurp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides seemless integration of the jsonj library by adding conversion methods to hashes, lists and JsonElement instances and support for using [] and << on the Java objects.
14
+ email: incoming@jillesvangurp.xom
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/jsonj.rb
20
+ homepage: https://github.com/jillesvangurp/jsonj-integration
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Jruby helper for integrating with the jsonj java library
43
+ test_files: []