json-rpc-objects 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.txt CHANGED
@@ -1,6 +1,11 @@
1
1
 
2
+ 0.4.0 (2011-07-20)
3
+ * multiple serializers support
4
+ * minor optimization of the version routine
5
+ * documentation corrections
6
+
2
7
  0.3.4 (2011-07-18)
3
- * dependency from yajl-ruby to multi_json for compatibility with all
8
+ * dependency from 'yajl-ruby' to 'multi_json' for compatibility with all
4
9
  other major JSON libraries or unsupported platforms
5
10
  * minor optimizations and corrections
6
11
  * upgrade to modern 'hash-utils' version
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  JSON-RPC Objects
2
2
  ================
3
3
 
4
- **JSON-RPC Objects** are implementation of by [JSON-RPC][1] defined
5
- objects with respect to specifications compliance and API backward
6
- compatibility. It implements all versions of the protocol and support
7
- for ability to communicate by the same protocol version which the
8
- other side uses by a transparent way.
4
+ **JSON-RPC Objects** is complete implementation of by [JSON-RPC][1]
5
+ defined objects with respect to specifications compliance and API
6
+ backward compatibility. It implements all versions of the protocol and
7
+ support for ability to communicate by the same protocol version which
8
+ the other side uses by a transparent way.
9
9
 
10
10
  It means, it implements following JSON-RPC versions:
11
11
 
@@ -28,7 +28,7 @@ homogenous. Application which can deal with 1.0 can deal with 2.0 too
28
28
  without any funcionallity or logic lost.
29
29
 
30
30
  ### Usage
31
-
31
+
32
32
  All object classes have three creating class methods:
33
33
 
34
34
  * `#create(<some arguments>, opts = { })` &ndash; which creates new
@@ -89,6 +89,35 @@ neither 1.0 nor 2.0 implements these objects, so it can simply cause
89
89
  Be limited by `Error`, `Request` and `Response` classes here or check
90
90
  the protocol version using `#VERSION` class constant.
91
91
 
92
+ ### Serializers
93
+
94
+ Multiple serializers support is implemented, so you aren't limited to
95
+ JSON[8] only, but you can use also built-in serializers to YAML[9] or
96
+ [Ruby marshaling][10] format. Also support for [BSON][11] serializing
97
+ format is available standalone through [`json-rpc-objects-bson`][12] gem.
98
+
99
+ You can set the default serializer for whole library session (both class
100
+ and instance of the class are supported):
101
+
102
+ require "json-rpc-objects/serializer/marshal"
103
+
104
+ JsonRpcObjects::default_serializer(JsonRpcObjects::Serializer::Marshal)
105
+
106
+ # it's setting default serializer for all new instances, without
107
+ # arguments it returns the default serializer
108
+
109
+ Or by individual object assigning (only instances are supported):
110
+
111
+ require "json-rpc-objects/serializer/marshal"
112
+
113
+ serializer = JsonRpcObjects::Serializer::Marshal::new
114
+ JsonRpcObjects::V10::Request::parse(data, serializer)
115
+
116
+ …and the same for constructor. The `#serializer` property is also
117
+ accessible, both readable and writable on all objects. By the same way,
118
+ serializer is received by the generic parsers `JsonRpcObjects::Request`
119
+ and so too.
120
+
92
121
  Contributing
93
122
  ------------
94
123
 
@@ -113,3 +142,7 @@ further details.
113
142
  [5]: http://groups.google.com/group/json-rpc/web/json-rpc-2-0
114
143
  [6]: http://github.com/martinkozak/json-rpc-objects/issues
115
144
  [7]: http://www.martinkozak.net/
145
+
146
+ [9]: http://www.yaml.org/
147
+ [10]: http://ruby-doc.org/core/classes/Marshal.html
148
+ [11]: http://bsonspec.org/
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.4.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{json-rpc-objects}
8
- s.version = "0.3.4"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Martin Kozák}]
12
- s.date = %q{2011-07-18}
12
+ s.date = %q{2011-07-20}
13
13
  s.email = %q{martinkozak@martinkozak.net}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -33,6 +33,10 @@ Gem::Specification.new do |s|
33
33
  "lib/json-rpc-objects/generic/response.rb",
34
34
  "lib/json-rpc-objects/request.rb",
35
35
  "lib/json-rpc-objects/response.rb",
36
+ "lib/json-rpc-objects/serializer.rb",
37
+ "lib/json-rpc-objects/serializer/json.rb",
38
+ "lib/json-rpc-objects/serializer/marshal.rb",
39
+ "lib/json-rpc-objects/serializer/yaml.rb",
36
40
  "lib/json-rpc-objects/v10/error.rb",
37
41
  "lib/json-rpc-objects/v10/request.rb",
38
42
  "lib/json-rpc-objects/v10/response.rb",
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "json-rpc-objects/v20/error"
3
5
 
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+ #
2
4
  # @deprecated (since 0.3.0)
3
5
 
4
6
  require "json-rpc-objects/generic/object"
@@ -2,8 +2,8 @@
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
4
  require "hash-utils/hash"
5
- require "multi_json"
6
5
  require "abstract"
6
+ require "json-rpc-objects/serializer"
7
7
  require "json-rpc-objects/version"
8
8
 
9
9
  ##
@@ -25,11 +25,20 @@ module JsonRpcObjects
25
25
 
26
26
  class Object
27
27
 
28
+ ##
29
+ # Holds assigned serializer.
30
+ # @since 0.4.0
31
+ #
32
+
33
+ attr_accessor :serializer
34
+ @serializer
35
+
28
36
  ##
29
37
  # Creates new one.
30
38
  #
31
39
  # @param [Array] args some arguments
32
- # @return [JsonRpc::Generic::Object] new object
40
+ # @return [JsonRpcObjects::Generic::Object] new object
41
+ # @abstract
33
42
  #
34
43
 
35
44
  def self.create(*args)
@@ -46,31 +55,52 @@ module JsonRpcObjects
46
55
  end
47
56
 
48
57
  ##
49
- # Parses JSON-RPC string.
58
+ # Parses serialized string.
50
59
  #
51
- # @param [String] string with the JSON data
60
+ # @param [Object] object with the serialized data
61
+ # @param [JsonRpcObjects::Serializer] serializer instance
62
+ # of serializer class
52
63
  # @return [Generic::Object] of the given class
53
64
  #
54
65
 
55
- def self.parse(string)
56
- self::new(MultiJson.decode(string))
66
+ def self.parse(string, serializer = JsonRpcObjects::default_serializer)
67
+ self::new(serializer.deserialize(string), serializer)
57
68
  end
58
-
69
+
59
70
  ##
60
- # Converts back to JSON.
71
+ # Converts object to JSON. It's deprecated and ineffective now.
72
+ # Use the {#serialize} method.
73
+ #
74
+ # @see #serialize
61
75
  # @return [String]
76
+ # @deprecated Since 0.4.0, replaced by +#serialize+.
62
77
  #
63
78
 
64
79
  def to_json
65
- MultiJson.encode(self.output)
80
+ JsonRpcObjects::Serializer::JSON::new.serialize(self.output)
81
+ end
82
+
83
+ ##
84
+ # Serializes the object by the serializer.
85
+ #
86
+ # @return [Object]
87
+ # @since 0.4.0
88
+ #
89
+
90
+ def serialize
91
+ @serializer.serialize(self.output)
66
92
  end
67
93
 
68
94
  ##
69
95
  # Constructor.
96
+ #
70
97
  # @param [Hash] data for initializing the object
98
+ # @param [JsonRpcObjects::Serializer] serializer instance
99
+ # of serializer class
71
100
  #
72
101
 
73
- def initialize(data)
102
+ def initialize(data, serializer = JsonRpcObjects::default_serializer)
103
+ @serializer = serializer
74
104
  self.data = data
75
105
  self.check!
76
106
  end
@@ -127,3 +157,4 @@ module JsonRpcObjects
127
157
  end
128
158
  end
129
159
  end
160
+
@@ -1,7 +1,9 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "json-rpc-objects/v20/request"
5
+ require "json-rpc-objects/serializer"
3
6
  require "hash-utils/object"
4
- require "multi_json"
5
7
 
6
8
  ##
7
9
  # Main JSON-RPC Objects module.
@@ -43,10 +45,12 @@ module JsonRpcObjects
43
45
  # @param [String] string JSON string for parse
44
46
  # @param [:wd, :alt] default_v11 type of the eventually returned
45
47
  # 1.1 object
48
+ # @param [JsonRpcObjects::Serializer] serializer instance of
49
+ # serializer class
46
50
  #
47
51
 
48
- def self.parse(string, default_v11 = :wd)
49
- data = MultiJson.decode(string)
52
+ def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
53
+ data = serializer.deserialize(string)
50
54
 
51
55
  if not data.hash?
52
56
  raise Exception::new("Data in JSON string aren't object.")
@@ -1,7 +1,9 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "json-rpc-objects/v20/response"
5
+ require "json-rpc-objects/serializer"
3
6
  require "hash-utils/object"
4
- require "multi_json"
5
7
 
6
8
  ##
7
9
  # Main JSON-RPC Objects module.
@@ -42,10 +44,12 @@ module JsonRpcObjects
42
44
  # @param [String] string JSON string for parse
43
45
  # @param [:wd, :alt] default_v11 type of the eventually returned
44
46
  # 1.1 object
47
+ # @param [JsonRpcObjects::Serializer] serializer instance of
48
+ # serializer class
45
49
  #
46
50
 
47
- def self.parse(string, default_v11 = :wd)
48
- data = MultiJson.decode(string)
51
+ def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
52
+ data = serializer.deserialize(string)
49
53
 
50
54
  if not data.hash?
51
55
  raise Exception::new("Data in JSON string aren't object.")
@@ -0,0 +1,106 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "json-rpc-objects/serializer/json"
5
+ require "abstract"
6
+
7
+ ##
8
+ # Main JSON-RPC Objects module.
9
+ #
10
+
11
+ module JsonRpcObjects
12
+
13
+ ##
14
+ # Holds the serializer.
15
+ #
16
+
17
+ @@serializer = JsonRpcObjects::Serializer::JSON::new
18
+
19
+ ##
20
+ # Sets the object serializer.
21
+ #
22
+ # @param [Class, Module] serializer the serializer module
23
+ # @since 0.4.0
24
+ #
25
+
26
+ def self.default_serializer(mod = nil)
27
+ if mod.kind_of? Class
28
+ mod = mod::new
29
+ end
30
+
31
+ @@serializer = mod if not mod.nil?
32
+ @@serializer # returns
33
+ end
34
+
35
+ ##
36
+ # Serializes data.
37
+ #
38
+ # @param [Object] data some data
39
+ # @return [Object] object in serialized form
40
+ # @since 0.4.0
41
+ #
42
+
43
+ def self.serialize(data)
44
+ @@serializer.serialize(data)
45
+ end
46
+
47
+ ##
48
+ # Deserializes data.
49
+ #
50
+ # @param [Object] data data in serialized form
51
+ # @return [Object] deserialized data
52
+ # @since 0.4.0
53
+ #
54
+
55
+ def self.deserialize(data)
56
+ @@serializer.deserialize(data)
57
+ end
58
+
59
+
60
+ ##
61
+ # Abstract serializer class.
62
+ #
63
+ # @since 0.4.0
64
+ # @abstract
65
+ #
66
+
67
+ class Serializer
68
+
69
+ ##
70
+ # Constructor.
71
+ #
72
+
73
+ def initialize
74
+ if self.instance_of? Serializer
75
+ not_implemented
76
+ end
77
+ end
78
+
79
+ ##
80
+ # Serializes data.
81
+ #
82
+ # @param [Object] data some data
83
+ # @return [Object] object in serialized form
84
+ # @abstract
85
+ #
86
+
87
+ def serialize(data)
88
+ not_implemented
89
+ end
90
+
91
+ ##
92
+ # Deserializes data.
93
+ #
94
+ # @param [Object] data data in serialized form
95
+ # @return [Object] deserialized data
96
+ # @abstract
97
+ #
98
+
99
+ def deserialize(data)
100
+ not_implemented
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "json-rpc-objects/serializer"
5
+ require "multi_json"
6
+
7
+ ##
8
+ # Main JSON-RPC Objects module.
9
+ #
10
+
11
+ module JsonRpcObjects
12
+
13
+ ##
14
+ # Abstract serializer class.
15
+ #
16
+ # @since 0.4.0
17
+ # @abstract
18
+ #
19
+
20
+ class Serializer
21
+
22
+ ##
23
+ # JSON serializer using +multi_json+.
24
+ # @since 0.4.0
25
+ #
26
+
27
+ class JSON < Serializer
28
+
29
+ ##
30
+ # Serializes data.
31
+ #
32
+ # @param [Object] data some data
33
+ # @return [Object] object in serialized form
34
+ #
35
+
36
+ def serialize(data)
37
+ MultiJson.encode(data)
38
+ end
39
+
40
+ ##
41
+ # Deserializes data.
42
+ #
43
+ # @param [Object] data data in serialized form
44
+ # @return [Object] deserialized data
45
+ #
46
+
47
+ def deserialize(data)
48
+ MultiJson.decode(data)
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "json-rpc-objects/serializer"
5
+
6
+ ##
7
+ # Main JSON-RPC Objects module.
8
+ #
9
+
10
+ module JsonRpcObjects
11
+
12
+ ##
13
+ # Abstract serializer class.
14
+ #
15
+ # @since 0.4.0
16
+ # @abstract
17
+ #
18
+
19
+ class Serializer
20
+
21
+ ##
22
+ # +Marshal+ serializer using internal Ruby marshaling.
23
+ # @since 0.4.0
24
+ #
25
+
26
+ class Marshal < Serializer
27
+
28
+ ##
29
+ # Serializes data.
30
+ #
31
+ # @param [Object] data some data
32
+ # @return [Object] object in serialized form
33
+ #
34
+
35
+ def serialize(data)
36
+ ::Marshal.dump(data)
37
+ end
38
+
39
+ ##
40
+ # Deserializes data.
41
+ #
42
+ # @param [Object] data data in serialized form
43
+ # @return [Object] deserialized data
44
+ #
45
+
46
+ def deserialize(data)
47
+ ::Marshal.load(data)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+
55
+
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "json-rpc-objects/serializer"
5
+ require "yaml"
6
+
7
+ ##
8
+ # Main JSON-RPC Objects module.
9
+ #
10
+
11
+ module JsonRpcObjects
12
+
13
+ ##
14
+ # Abstract serializer class.
15
+ #
16
+ # @since 0.4.0
17
+ # @abstract
18
+ #
19
+
20
+ class Serializer
21
+
22
+ ##
23
+ # YAML serializer using internal Ruby YAML.
24
+ # @since 0.4.0
25
+ #
26
+
27
+ class YAML < Serializer
28
+
29
+ ##
30
+ # Serializes data.
31
+ #
32
+ # @param [Object] data some data
33
+ # @return [Object] object in serialized form
34
+ #
35
+
36
+ def serialize(data)
37
+ ::YAML.dump(data)
38
+ end
39
+
40
+ ##
41
+ # Deserializes data.
42
+ #
43
+ # @param [Object] data data in serialized form
44
+ # @return [Object] deserialized data
45
+ #
46
+
47
+ def deserialize(data)
48
+ ::YAML.load(data)
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+
@@ -90,9 +90,9 @@ module JsonRpcObjects
90
90
  def output
91
91
  self.check!
92
92
  data = {
93
- :method => @method.to_s,
94
- :params => @params,
95
- :id => @id
93
+ "method" => @method.to_s,
94
+ "params" => @params,
95
+ "id" => @id
96
96
  }
97
97
  end
98
98
 
@@ -97,10 +97,17 @@ module JsonRpcObjects
97
97
 
98
98
  def output
99
99
  self.check!
100
+
101
+ if @error.nil?
102
+ error = nil
103
+ else
104
+ error = @error.output
105
+ end
106
+
100
107
  data = {
101
- :result => @result,
102
- :error => @error,
103
- :id => @id
108
+ "result" => @result,
109
+ "error" => error,
110
+ "id" => @id
104
111
  }
105
112
 
106
113
  return data
@@ -2,21 +2,22 @@
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
4
  $:.push("../../..")
5
+ require "json-rpc-objects/serializer/marshal"
6
+ JsonRpcObjects::default_serializer(JsonRpcObjects::Serializer::Marshal)
5
7
 
6
8
  require "../request"
7
9
  req = JsonRpcObjects::V10::Request::create(:alfa, [:beta], :id => 12345)
8
- puts req.to_json
10
+ puts req.serialize
9
11
 
10
12
  require "../response"
11
13
  res = JsonRpcObjects::V10::Response::create(true, nil, :id => 12345)
12
- puts res.to_json
14
+ puts res.serialize
13
15
 
14
16
  require "../response"
15
17
  res = JsonRpcObjects::V10::Response::create(nil, "some problem", :id => 12345)
16
- puts res.to_json
17
-
18
+ puts res.serialize
18
19
 
19
20
  require "../../request"
20
- puts JsonRpcObjects::Request::parse(req.to_json).inspect
21
+ puts JsonRpcObjects::Request::parse(req.serialize).inspect
21
22
  require "../../response"
22
- puts JsonRpcObjects::Response::parse(res.to_json).inspect
23
+ puts JsonRpcObjects::Response::parse(res.serialize).inspect
@@ -49,7 +49,7 @@ module JsonRpcObjects
49
49
 
50
50
  def output
51
51
  result = super()
52
- result.delete(:name)
52
+ result.delete("name")
53
53
 
54
54
  return result
55
55
  end
@@ -3,39 +3,43 @@
3
3
 
4
4
  $:.push("../../../..")
5
5
 
6
+ require "json-rpc-objects/serializer/marshal"
7
+ JsonRpcObjects::default_serializer(JsonRpcObjects::Serializer::Marshal)
8
+
9
+
6
10
  require "../procedure-call"
7
11
  req = JsonRpcObjects::V11::Alt::ProcedureCall::create(:alfa, [:foo, :bar], :kwparams => {"0" => :beta, "something" => :alfa}, :id => 12345, :"$whatever" => false)
8
- puts req.to_json
12
+ puts req.serialize
9
13
 
10
14
  require "../error"
11
15
  err = JsonRpcObjects::V11::Alt::Error::create(200, "some problem")
12
16
 
13
17
  require "../procedure-return"
14
18
  res = JsonRpcObjects::V11::Alt::ProcedureReturn::create(nil, err, :id => 12345)
15
- puts res.to_json
19
+ puts res.serialize
16
20
  res = JsonRpcObjects::V11::Alt::ProcedureReturn::create(true, nil, :id => 12345)
17
- puts res.to_json
21
+ puts res.serialize
18
22
 
19
23
  require "../service-description"
20
24
  sdesc = JsonRpcObjects::V11::Alt::ServiceDescription::create(:alfa, 100)
21
- puts sdesc.to_json
25
+ puts sdesc.serialize
22
26
 
23
27
  require "../service-procedure-description"
24
28
  sproc = JsonRpcObjects::V11::Alt::ServiceProcedureDescription::create(:some_proc)
25
29
  sdesc << sproc
26
- puts sdesc.to_json
30
+ puts sdesc.serialize
27
31
 
28
32
  require "../procedure-parameter-description"
29
33
  sparam1 = JsonRpcObjects::V11::Alt::ProcedureParameterDescription::create(:param1, :type => :str)
30
34
  sparam2 = JsonRpcObjects::V11::Alt::ProcedureParameterDescription::create(:param2, :type => JsonRpcObjects)
31
35
  sproc << sparam1
32
36
  sproc << sparam2
33
- puts sdesc.to_json
37
+ puts sdesc.serialize
34
38
 
35
39
 
36
40
  require "../../../request"
37
- puts JsonRpcObjects::Request::parse(req.to_json).inspect
41
+ puts JsonRpcObjects::Request::parse(req.serialize).inspect
38
42
  require "../../../response"
39
- puts JsonRpcObjects::Response::parse(res.to_json, :alt).inspect
43
+ puts JsonRpcObjects::Response::parse(res.serialize, :alt).inspect
40
44
 
41
45
  puts req.class::version.response::create(25)
@@ -2,6 +2,7 @@
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
4
  require "hash-utils/numeric"
5
+ require "hash-utils/hash"
5
6
  require "json-rpc-objects/generic"
6
7
  require "json-rpc-objects/v10/error"
7
8
  require "json-rpc-objects/v11/wd/extensions"
@@ -115,16 +116,16 @@ module JsonRpcObjects
115
116
  def output
116
117
  self.check!
117
118
  data = {
118
- :name => :JSONRPCError,
119
- :code => @code,
120
- :message => @message
119
+ "name" => "JSONRPCError",
120
+ "code" => @code,
121
+ "message" => @message
121
122
  }
122
123
 
123
124
  if not @data.nil?
124
- data[:error] = @data
125
+ data["error"] = @data
125
126
  end
126
127
 
127
- data.merge! @extensions
128
+ data.merge! @extensions.map_keys { |k| k.to_s }
128
129
  return data
129
130
  end
130
131
 
@@ -45,13 +45,13 @@ module JsonRpcObjects
45
45
  # Holds JSON-RPC version specification.
46
46
  #
47
47
 
48
- VERSION_NUMBER = :"1.1"
48
+ VERSION_NUMBER = "1.1"
49
49
 
50
50
  ##
51
51
  # Holds JSON-RPC version member identification.
52
52
  #
53
53
 
54
- VERSION_MEMBER = :version
54
+ VERSION_MEMBER = "version"
55
55
 
56
56
  ##
57
57
  # Holds keyword parameters.
@@ -99,7 +99,7 @@ module JsonRpcObjects
99
99
  end
100
100
 
101
101
  data.merge! @extensions
102
- return data
102
+ return data.map_keys! { |k| k.to_s }
103
103
  end
104
104
 
105
105
 
@@ -168,14 +168,14 @@ module JsonRpcObjects
168
168
 
169
169
  if not @params.nil?
170
170
  @params.each_index do |i|
171
- params[i.to_s.to_sym] = @params[i]
171
+ params[i.to_s] = @params[i]
172
172
  end
173
173
  end
174
174
  if not @keyword_params.nil?
175
175
  params.merge! @keyword_params
176
176
  end
177
177
 
178
- data[:params] = params
178
+ data[:params] = params.map_keys! { |k| k.to_s }
179
179
  end
180
180
 
181
181
  ##
@@ -201,7 +201,7 @@ module JsonRpcObjects
201
201
  #
202
202
 
203
203
  def __delete_version(data)
204
- data.delete(self.class::VERSION_MEMBER)
204
+ data.delete(self.class::VERSION_MEMBER.to_sym)
205
205
  end
206
206
 
207
207
  end
@@ -116,10 +116,10 @@ module JsonRpcObjects
116
116
 
117
117
  def output
118
118
  self.check!
119
- data = { :name => @name.to_s }
119
+ data = { "name" => @name.to_s }
120
120
 
121
121
  if not @type.nil?
122
- data[:type] = __object_to_type
122
+ data["type"] = __object_to_type.to_s
123
123
  end
124
124
 
125
125
  return data
@@ -5,6 +5,7 @@ require "json-rpc-objects/v10/response"
5
5
  require "json-rpc-objects/v11/wd/error"
6
6
  require "json-rpc-objects/v11/wd/extensions"
7
7
  require "hash-utils/object"
8
+ require "hash-utils/hash"
8
9
 
9
10
  ##
10
11
  # Main JSON-RPC Objects module.
@@ -43,13 +44,13 @@ module JsonRpcObjects
43
44
  # Holds JSON-RPC version specification.
44
45
  #
45
46
 
46
- VERSION_NUMBER = :"1.1"
47
+ VERSION_NUMBER = "1.1"
47
48
 
48
49
  ##
49
50
  # Holds JSON-RPC version member identification.
50
51
  #
51
52
 
52
- VERSION_MEMBER = :version
53
+ VERSION_MEMBER = "version"
53
54
 
54
55
  ##
55
56
  # Identifies the error object class.
@@ -80,18 +81,18 @@ module JsonRpcObjects
80
81
  __assign_version(data)
81
82
 
82
83
  if not @result.nil?
83
- data[:result] = @result
84
+ data["result"] = @result
84
85
  end
85
86
 
86
87
  if not @error.nil?
87
- data[:error] = @error
88
+ data["error"] = @error.output
88
89
  end
89
90
 
90
91
  if not @id.nil?
91
- data[:id] = @id
92
+ data["id"] = @id
92
93
  end
93
94
 
94
- data.merge! @extensions
95
+ data.merge! @extensions.map_keys { |k| k.to_s }
95
96
  return data
96
97
  end
97
98
 
@@ -149,7 +150,7 @@ module JsonRpcObjects
149
150
  #
150
151
 
151
152
  def __delete_version(data)
152
- data.delete(self.class::VERSION_MEMBER)
153
+ data.delete(self.class::VERSION_MEMBER.to_sym)
153
154
  end
154
155
 
155
156
  end
@@ -161,29 +161,29 @@ module JsonRpcObjects
161
161
  self.check!
162
162
 
163
163
  data = {
164
- :sdversion => :"1.0",
165
- :name => @name.to_s,
166
- :id => @id.to_s
164
+ "sdversion" => "1.0",
165
+ "name" => @name.to_s,
166
+ "id" => @id.to_s
167
167
  }
168
168
 
169
169
  if not @version.nil?
170
- data[:version] = @version
170
+ data["version"] = @version
171
171
  end
172
172
 
173
173
  if not @summary.nil?
174
- data[:summary] = @summary
174
+ data["summary"] = @summary
175
175
  end
176
176
 
177
177
  if not @help.nil?
178
- data[:help] = @help.to_s
178
+ data["help"] = @help.to_s
179
179
  end
180
180
 
181
181
  if not @address.nil?
182
- data[:address] = @address.to_s
182
+ data["address"] = @address.to_s
183
183
  end
184
184
 
185
185
  if not @procs.nil?
186
- data[:procs] = @procs
186
+ data["procs"] = @procs.map { |i| i.output }
187
187
  end
188
188
 
189
189
  return data
@@ -147,26 +147,26 @@ module JsonRpcObjects
147
147
 
148
148
  def output
149
149
  self.check!
150
- data = { :name => @name.to_s }
150
+ data = { "name" => @name.to_s }
151
151
 
152
152
  if not @summary.nil?
153
- data[:summary] = @summary
153
+ data["summary"] = @summary
154
154
  end
155
155
 
156
156
  if not @help.nil?
157
- data[:help] = @help.to_s
157
+ data["help"] = @help.to_s
158
158
  end
159
159
 
160
160
  if not @idempotent.nil?
161
- data[:idempotent] = @idempotent
161
+ data["idempotent"] = @idempotent
162
162
  end
163
163
 
164
164
  if not @params.nil?
165
- data[:params] = @params
165
+ data["params"] = @params.map { |i| i.output }
166
166
  end
167
167
 
168
168
  if not @return.nil?
169
- data[:return] = @return
169
+ data["return"] = @return.output
170
170
  end
171
171
 
172
172
  return data
@@ -3,39 +3,42 @@
3
3
 
4
4
  $:.push("../../../..")
5
5
 
6
+ require "json-rpc-objects/serializer/marshal"
7
+ JsonRpcObjects::default_serializer(JsonRpcObjects::Serializer::Marshal)
8
+
9
+
6
10
  require "../procedure-call"
7
11
  req = JsonRpcObjects::V11::WD::ProcedureCall::create(:alfa, {"0" => :beta, "something" => :alfa}, :id => 12345, :"$whatever" => false)
8
- puts req.to_json
12
+ puts req.serialize
9
13
 
10
14
  require "../error"
11
15
  err = JsonRpcObjects::V11::WD::Error::create(200, "some problem")
12
16
 
13
17
  require "../procedure-return"
14
18
  res = JsonRpcObjects::V11::WD::ProcedureReturn::create(nil, err, :id => 12345)
15
- puts res.to_json
19
+ puts res.serialize
16
20
  res = JsonRpcObjects::V11::WD::ProcedureReturn::create(true, nil, :id => 12345)
17
- puts res.to_json
21
+ puts res.serialize
18
22
 
19
23
  require "../service-description"
20
24
  sdesc = JsonRpcObjects::V11::WD::ServiceDescription::create(:alfa, 100, :version => "1.2.34")
21
- puts sdesc.to_json
25
+ puts sdesc.serialize
22
26
 
23
27
  require "../service-procedure-description"
24
28
  sproc = JsonRpcObjects::V11::WD::ServiceProcedureDescription::create(:some_proc)
25
29
  sdesc << sproc
26
- puts sdesc.to_json
30
+ puts sdesc.serialize
27
31
 
28
32
  require "../procedure-parameter-description"
29
33
  sparam1 = JsonRpcObjects::V11::WD::ProcedureParameterDescription::create(:param1, :type => :str)
30
34
  sparam2 = JsonRpcObjects::V11::WD::ProcedureParameterDescription::create(:param2, :type => JsonRpcObjects)
31
35
  sproc << sparam1
32
36
  sproc << sparam2
33
- puts sdesc.to_json
34
-
37
+ puts sdesc.serialize
35
38
 
36
39
  require "../../../request"
37
- puts JsonRpcObjects::Request::parse(req.to_json).inspect
40
+ puts JsonRpcObjects::Request::parse(req.serialize).inspect
38
41
  require "../../../response"
39
- puts JsonRpcObjects::Response::parse(res.to_json).inspect
42
+ puts JsonRpcObjects::Response::parse(res.serialize).inspect
40
43
 
41
44
  puts req.class::version.response::create(25)
@@ -55,9 +55,9 @@ module JsonRpcObjects
55
55
  def output
56
56
  result = super()
57
57
 
58
- if result.include? :error
59
- result[:data] = result[:error]
60
- result.delete(:error)
58
+ if result.include? "error"
59
+ result["data"] = result["error"]
60
+ result.delete("error")
61
61
  end
62
62
 
63
63
  return result
@@ -33,13 +33,13 @@ module JsonRpcObjects
33
33
  # Holds JSON-RPC version specification.
34
34
  #
35
35
 
36
- VERSION_NUMBER = :"2.0"
36
+ VERSION_NUMBER = "2.0"
37
37
 
38
38
  ##
39
39
  # Holds JSON-RPC version member identification.
40
40
  #
41
41
 
42
- VERSION_MEMBER = :jsonrpc
42
+ VERSION_MEMBER = "jsonrpc"
43
43
 
44
44
  ##
45
45
  # Indicates ID has been set.
@@ -68,9 +68,9 @@ module JsonRpcObjects
68
68
  result = super()
69
69
 
70
70
  if @_id_set and @id.nil?
71
- result[:id] = nil
71
+ result["id"] = nil
72
72
  end
73
-
73
+
74
74
  return result
75
75
  end
76
76
 
@@ -119,7 +119,7 @@ module JsonRpcObjects
119
119
  if not @params.nil? and not @params.empty?
120
120
  data[:params] = @params
121
121
  elsif not @keyword_params.nil? and not @keyword_params.empty?
122
- data[:params] = @keyword_params
122
+ data[:params] = @keyword_params.map_keys { |k| k.to_s }
123
123
  end
124
124
  end
125
125
 
@@ -39,13 +39,13 @@ module JsonRpcObjects
39
39
  # Holds JSON-RPC version specification.
40
40
  #
41
41
 
42
- VERSION_NUMBER = :"2.0"
42
+ VERSION_NUMBER = "2.0"
43
43
 
44
44
  ##
45
45
  # Holds JSON-RPC version member identification.
46
46
  #
47
47
 
48
- VERSION_MEMBER = :jsonrpc
48
+ VERSION_MEMBER = "jsonrpc"
49
49
 
50
50
  ##
51
51
  # Indicates ID has been set.
@@ -74,7 +74,7 @@ module JsonRpcObjects
74
74
  result = super()
75
75
 
76
76
  if @_id_set and @id.nil?
77
- result[:id] = nil
77
+ result["id"] = nil
78
78
  end
79
79
 
80
80
  return result
@@ -100,7 +100,7 @@ module JsonRpcObjects
100
100
  #
101
101
 
102
102
  def __assign_version(data)
103
- data[:jsonrpc] = :"2.0"
103
+ data["jsonrpc"] = "2.0"
104
104
  end
105
105
 
106
106
  end
@@ -3,24 +3,28 @@
3
3
 
4
4
  $:.push("../../..")
5
5
 
6
+ require "json-rpc-objects/serializer/marshal"
7
+ JsonRpcObjects::default_serializer(JsonRpcObjects::Serializer::Marshal)
8
+
9
+
6
10
  require "../request"
7
11
  req = JsonRpcObjects::V20::Request::create(:alfa, {:alfa => :beta}, :id => :"12345", :"$whatever" => false)
8
- puts req.to_json
12
+ puts req.serialize
9
13
  req = JsonRpcObjects::V20::Request::create(:alfa, [:beta], :"version" => :"1.0")
10
- puts req.to_json
14
+ puts req.serialize
11
15
 
12
16
  require "../error"
13
17
  err = JsonRpcObjects::V20::Error::create(200, "some problem", :data => "Additional data.")
14
18
 
15
19
  require "../response"
16
20
  res = JsonRpcObjects::V20::Response::create(nil, err, :id => 12345)
17
- puts res.to_json
21
+ puts res.serialize
18
22
  res = JsonRpcObjects::V20::Response::create(true, nil, :id => 12345)
19
- puts res.to_json
23
+ puts res.serialize
20
24
 
21
25
  require "../../request"
22
- puts JsonRpcObjects::Request::parse(req.to_json).inspect
26
+ puts JsonRpcObjects::Request::parse(req.serialize).inspect
23
27
  require "../../response"
24
- puts JsonRpcObjects::Response::parse(res.to_json).inspect
28
+ puts JsonRpcObjects::Response::parse(res.serialize).inspect
25
29
 
26
30
  puts req.class::version.response::create(25)
@@ -1,5 +1,8 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "hash-utils/object"
5
+ require "hash-utils/module"
3
6
 
4
7
  ##
5
8
  # Main JSON-RPC Objects module.
@@ -76,7 +79,7 @@ module JsonRpcObjects
76
79
  class_name.gsub!(self.class::CLASS_NAME_GENERATOR) { |s| s[1].chr.upcase }
77
80
 
78
81
  # Module name
79
- module_name = @module.name.dup << "::" << class_name
82
+ module_name = @module.name + "::" + class_name
80
83
 
81
84
  # File path
82
85
  file_path = "x" << module_name
@@ -90,7 +93,7 @@ module JsonRpcObjects
90
93
  @@files[file_path] = true
91
94
  end
92
95
 
93
- return Kernel::eval(module_name)
96
+ return Module.get_module(module_name)
94
97
  end
95
98
 
96
99
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: json-rpc-objects
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.4
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Martin Koz\xC3\xA1k"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-18 00:00:00 Z
13
+ date: 2011-07-20 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hash-utils
@@ -105,6 +105,10 @@ files:
105
105
  - lib/json-rpc-objects/generic/response.rb
106
106
  - lib/json-rpc-objects/request.rb
107
107
  - lib/json-rpc-objects/response.rb
108
+ - lib/json-rpc-objects/serializer.rb
109
+ - lib/json-rpc-objects/serializer/json.rb
110
+ - lib/json-rpc-objects/serializer/marshal.rb
111
+ - lib/json-rpc-objects/serializer/yaml.rb
108
112
  - lib/json-rpc-objects/v10/error.rb
109
113
  - lib/json-rpc-objects/v10/request.rb
110
114
  - lib/json-rpc-objects/v10/response.rb
@@ -151,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
155
  requirements:
152
156
  - - ">="
153
157
  - !ruby/object:Gem::Version
154
- hash: -1516690560256011752
158
+ hash: -3765115865130944718
155
159
  segments:
156
160
  - 0
157
161
  version: "0"