View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.json;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.collections.*;
26  import org.apache.juneau.parser.*;
27  import org.junit.jupiter.api.*;
28  
29  class JsonParser_Test extends TestBase {
30  
31  	private static final JsonParser p = JsonParser.DEFAULT;
32  	private static final JsonParser sp = JsonParser.DEFAULT_STRICT;
33  
34  	//====================================================================================================
35  	// Test invalid input
36  	//====================================================================================================
37  	@Test void a01_invalidJson() {
38  		assertThrows(ParseException.class, ()->p.parse("{\na:1,\nb:xxx\n}", Object.class));
39  	}
40  
41  	@Test void a02_nonExistentAttribute() throws Exception {
42  		var json = "{foo:,bar:}";
43  		var m = p.parse(json, JsonMap.class);
44  		assertEquals("{foo:null,bar:null}", m.toString());
45  	}
46  
47  	@Test void a03_nonStringAsString() throws Exception {
48  		var json = "123";
49  		var s = p.parse(json, String.class);
50  
51  		// Strict mode does not allow unquoted values.
52  		assertThrowsWithMessage(Exception.class, "Did not find quote character", ()->sp.parse("123", String.class));
53  
54  		assertEquals("123", s);
55  
56  		json = " 123 ";
57  		// Strict mode does not allow unquoted values.
58  		assertThrowsWithMessage(Exception.class, "Did not find quote character", ()->sp.parse(" 123 ", String.class));
59  
60  		s = p.parse(json, String.class);
61  		assertEquals("123", s);
62  
63  		json = "{\"fa\":123}";
64  		assertThrowsWithMessage(Exception.class, "Did not find quote character", ()->sp.parse("{\"fa\":123}", A.class));
65  
66  		var a = p.parse(json, A.class);
67  		assertEquals("123", a.fa);
68  
69  		json = " { \"fa\" : 123 } ";
70  		assertThrowsWithMessage(Exception.class, "Did not find quote character", ()->sp.parse(" { \"fa\" : 123 } ", A.class));
71  
72  		a = p.parse(json, A.class);
73  		assertEquals("123", a.fa);
74  
75  		assertThrowsWithMessage(Exception.class, "Invalid quote character", ()->sp.parse("'123'", String.class));
76  	}
77  
78  	public static class A {
79  		public String fa;
80  	}
81  
82  	@Test void a04_strictMode() {
83  		var p2 = sp;
84  		assertThrowsWithMessage(Exception.class, "Missing value detected.", ()->p2.parse("{\"foo\":,\"bar\":}", JsonMap.class));
85  		assertThrowsWithMessage(Exception.class, "Invalid quote character", ()->p2.parse("{\"foo\":'bar'}", JsonMap.class));
86  		assertThrowsWithMessage(Exception.class, "Invalid quote character", ()->p2.parse("{'foo':\"bar\"}", JsonMap.class));
87  		assertThrowsWithMessage(Exception.class, "Unquoted attribute detected.", ()->p2.parse("{foo:\"bar\"}", JsonMap.class));
88  		assertThrowsWithMessage(Exception.class, "String concatenation detected.", ()->p2.parse("{\"foo\":\"bar\"+\"baz\"}", JsonMap.class));
89  		assertThrowsWithMessage(Exception.class, "String concatenation detected.", ()->p2.parse("{\"foo\":\"bar\" + \"baz\"}", JsonMap.class));
90  		assertThrowsWithMessage(Exception.class, "Javascript comment detected.", ()->p2.parse("{\"foo\":/*comment*/\"bar\"}", JsonMap.class));
91  	}
92  
93  	/**
94  	 * JSON numbers and booleans should be representable as strings and converted accordingly.
95  	 */
96  	@Test void a05_primitivesAsStrings() throws Exception {
97  		var p2 = JsonParser.DEFAULT;
98  		var s = Json5Serializer.DEFAULT;
99  
100 		var json = "{f01:'1',f02:'1',f03:'true',f04:'true',f05:'1',f06:'1',f07:'1',f08:'1',f09:'1',f10:'1'}";
101 		var b = p2.parse(json, B.class);
102 		assertEquals("{f01:1,f02:1,f03:true,f04:true,f05:1.0,f06:1.0,f07:1,f08:1,f09:1,f10:1}", s.toString(b));
103 
104 		json = "{f01:'',f02:'',f03:'',f04:'',f05:'',f06:'',f07:'',f08:'',f09:'',f10:''}";
105 		b = p2.parse(json, B.class);
106 		assertEquals("{f01:0,f02:0,f03:false,f04:false,f05:0.0,f06:0.0,f07:0,f08:0,f09:0,f10:0}", s.toString(b));
107 	}
108 
109 	public static class B {
110 		public int f01;
111 		public Integer f02;
112 		public boolean f03;
113 		public Boolean f04;
114 		public float f05;
115 		public Float f06;
116 		public long f07;
117 		public Long f08;
118 		public byte f09;
119 		public Byte f10;
120 	}
121 
122 	//====================================================================================================
123 	// testInvalidJsonNumbers
124 	// Lax parser allows octal and hexadecimal numbers.  Strict parser does not.
125 	//====================================================================================================
126 	@Test void a06_invalidJsonNumbers() throws Exception {
127 		var p1 = JsonParser.DEFAULT;
128 		var p2 = JsonParser.DEFAULT_STRICT;
129 
130 		// Lax allows blank strings interpreted as 0, strict does not.
131 		var s = "\"\"";
132 		var r = p1.parse(s, Number.class);
133 		assertEquals(0, r.intValue());
134 		assertTrue(r instanceof Integer);
135 		assertThrowsWithMessage(Exception.class, "Invalid JSON number", ()->p2.parse("\"\"", Number.class));
136 
137 		// Either should allow 0 or -0.
138 		s = "0";
139 		r = p1.parse(s, Number.class);
140 		assertEquals(0, r.intValue());
141 		assertTrue(r instanceof Integer);
142 		r = p2.parse(s, Number.class);
143 		assertEquals(0, r.intValue());
144 		assertTrue(r instanceof Integer);
145 
146 		s = "-0";
147 		r = p1.parse(s, Number.class);
148 		assertEquals(0, r.intValue());
149 		assertTrue(r instanceof Integer);
150 		r = p2.parse(s, Number.class);
151 		assertEquals(0, r.intValue());
152 		assertTrue(r instanceof Integer);
153 
154 		// Lax allows 0123 and -0123, strict does not.
155 		s = "0123";
156 		r = p1.parse(s, Number.class);
157 		assertEquals(0123, r.intValue());
158 		assertTrue(r instanceof Integer);
159 		assertThrowsWithMessage(Exception.class, "Invalid JSON number", ()->p2.parse("0123", Number.class));
160 		s = "-0123";
161 		r = p1.parse(s, Number.class);
162 		assertEquals(-0123, r.intValue());
163 		assertTrue(r instanceof Integer);
164 		assertThrowsWithMessage(Exception.class, "Invalid JSON number", ()->p2.parse("-0123", Number.class));
165 
166 		// Lax allows 0x123 and -0x123, strict does not.
167 		s = "0x123";
168 		r = p1.parse(s, Number.class);
169 		assertEquals(0x123, r.intValue());
170 		assertTrue(r instanceof Integer);
171 		assertThrowsWithMessage(Exception.class, "Invalid JSON number", ()->p2.parse("0x123", Number.class));
172 		s = "-0x123";
173 		r = p1.parse(s, Number.class);
174 		assertEquals(-0x123, r.intValue());
175 		assertTrue(r instanceof Integer);
176 		assertThrowsWithMessage(Exception.class, "Invalid JSON number", ()->p2.parse("-0x123", Number.class));
177 	}
178 
179 	//====================================================================================================
180 	// testUnquotedStrings
181 	// Lax parser allows unquoted strings if POJO can be converted from a string.
182 	//====================================================================================================
183 	@Test void a07_unquotedStrings() throws Exception {
184 		var p1 = JsonParser.DEFAULT;
185 		var p2 = JsonParser.DEFAULT_STRICT;
186 
187 		var s = "foobar";
188 		var c = p1.parse(s, C.class);
189 		assertEquals("f=foobar", c.toString());
190 
191 		assertThrows(ParseException.class, ()->p2.parse(s, C.class));
192 	}
193 
194 	public static class C {
195 		String f;
196 		public static C valueOf(String s) {
197 			var c = new C();
198 			c.f = s;
199 			return c;
200 		}
201 		@Override /* Object */
202 		public String toString() {
203 			return "f="+f;
204 		}
205 	}
206 
207 	//====================================================================================================
208 	// testStreamsAutoClose
209 	// Validates PARSER_autoCloseStreams.
210 	//====================================================================================================
211 	@Test void a08_streamsAutoClose() throws Exception {
212 		var p2 = JsonParser.DEFAULT.copy().autoCloseStreams().build();
213 		var r = reader("{foo:'bar'}{baz:'qux'}");
214 
215 		var x = p2.parse(r, JsonMap.class);
216 		assertBean(x, "foo", "bar");
217 		assertThrowsWithMessage(Exception.class, "Reader is closed", ()->p2.parse(r, JsonMap.class));
218 	}
219 
220 	//====================================================================================================
221 	// testMultipleObjectsInStream
222 	// Validates that readers are not closed so that we can read streams of POJOs.
223 	//====================================================================================================
224 	@Test void a09_multipleObjectsInStream() throws Exception {
225 		var p2 = JsonParser.create().unbuffered().build();
226 		var r = reader("{foo:'bar'}{baz:'qux'}");
227 
228 		var x = (Object)p2.parse(r, JsonMap.class);
229 		assertBean(x, "foo", "bar");
230 		x = p2.parse(r, JsonMap.class);
231 		assertBean(x, "baz", "qux");
232 
233 		r = reader("[123][456]");
234 		x = p2.parse(r, JsonList.class);
235 		assertList(x, "123");
236 		x = p2.parse(r, JsonList.class);
237 		assertList(x, "456");
238 	}
239 
240 	private Reader reader(String in) {
241 		return new CloseableStringReader(in);
242 	}
243 }