Finished documentation.
[arvados.git] / spec / google / api_client / parsers / json_parser_spec.rb
1 # Copyright 2010 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'spec_helper'
16
17 require 'json'
18 require 'google/api_client/parsers/json_parser'
19
20 describe Google::APIClient::JSONParser, 'generates json from hash' do
21   before do
22     @parser = Google::APIClient::JSONParser
23   end
24
25   it 'should translate simple hash to JSON string' do
26     @parser.serialize('test' => 23).should == '{"test":23}'
27   end
28
29   it 'should translate simple nested into to nested JSON string' do
30     @parser.serialize({
31       'test' => 23, 'test2' => {'foo' => 'baz', 12 => 3.14 }
32     }).should ==
33       '{"test2":{"12":3.14,"foo":"baz"},"test":23}'
34   end
35 end
36
37 describe Google::APIClient::JSONParser, 'parses json string into hash' do
38   before do
39     @parser = Google::APIClient::JSONParser
40   end
41
42   it 'should parse simple json string into hash' do
43     @parser.parse('{"test":23}').should == {'test' => 23}
44   end
45
46   it 'should parse nested json object into hash' do
47     @parser.parse('{"test":23, "test2":{"bar":"baz", "foo":3.14}}').should == {
48       'test' => 23, 'test2' => {'bar' => 'baz', 'foo' => 3.14}
49     }
50   end
51 end