1 /**
2  * Copyright (c) 2008, http://www.snakeyaml.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.yaml.snakeyaml.json;
17 
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.Yaml;
20 
21 
22 import java.util.Map;
23 
24 public class JsonTest extends TestCase {
25 
26     private Yaml loader = new Yaml();
27 
28 
testLooksLikeJson()29     public void testLooksLikeJson() {
30         Map<String, Integer> map = (Map<String, Integer>) loader.load("{a: 1}");
31         assertEquals(new Integer(1), map.get("a"));
32     }
33 
testSpaceAfterColon()34     public void testSpaceAfterColon() {
35         Map<String, Integer> map = (Map<String, Integer>) loader.load("{\"a\": 1}");
36         assertEquals(new Integer(1), map.get("a"));
37     }
38 
testCounterintuitiveColon()39     public void testCounterintuitiveColon() {
40         try {
41             loader.load("{a:1}");
42             fail("We agree with libyaml and PyYAML.");
43         } catch (Exception e) {
44             assertTrue("':' in the flow context is a mess.", e.getMessage().contains("Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details."));
45         }
46     }
47 
testNoSpace()48     public void testNoSpace() {
49         Map<String, Integer> map = (Map<String, Integer>) loader.load("{\"a\":1}");
50         assertEquals(new Integer(1), map.get("a"));
51     }
52 
testNoSpaceBothDoubleQuoted()53     public void testNoSpaceBothDoubleQuoted() {
54         Map<String, Integer> map = (Map<String, Integer>) loader.load("{\"a\":\"1\"}");
55         assertEquals("1", map.get("a"));
56     }
57 
testNoSpaceSingleQouted()58     public void testNoSpaceSingleQouted() {
59         Map<String, Integer> map = (Map<String, Integer>) loader.load("{'a':1}");
60         assertEquals(new Integer(1), map.get("a"));
61     }
62 
testManyValues()63     public void testManyValues() {
64         Map<String, Object> map = (Map<String, Object>) loader.load("{\"a\":1,\"b\":true,\"c\":\"foo\"}");
65         assertEquals(3, map.size());
66         assertEquals(new Integer(1), map.get("a"));
67         assertTrue((Boolean) map.get("b"));
68         assertEquals("foo", map.get("c"));
69     }
70 
testConstructNull()71     public void testConstructNull() {
72         Map<String, Object> map = (Map<String, Object>) loader.load("{a: null}");
73         assertEquals(1, map.size());
74         assertNull(map.get("a"));
75     }
76 
testConstructNullFromEmpty()77     public void testConstructNullFromEmpty() {
78         Map<String, Object> map = (Map<String, Object>) loader.load("{a: }");
79         assertEquals(1, map.size());
80         assertNull(map.get("a"));
81     }
82 
testConstructBoolean()83     public void testConstructBoolean() {
84         Map<String, Object> map = (Map<String, Object>) loader.load("{a: true}");
85         assertEquals(1, map.size());
86         assertEquals(Boolean.TRUE, map.get("a"));
87     }
88 }
89