2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util.json;
21 import gov.nasa.jpf.util.test.TestJPF;
23 import org.junit.Test;
27 * @author Ivan Mushketik
29 public class JSONParserTest extends TestJPF {
32 public void testOneLevelJSON() {
34 + "\"key1\" : \"str\","
37 JSONObject o = parseJSON(json);
39 Value key1 = o.getValue("key1");
40 assert key1.getString().equals("str");
41 Value key2 = o.getValue("key2");
42 assert key2.getDouble() == 123;
46 public void testEmptyObject() {
48 JSONObject o = parseJSON(json);
50 assert o.getValue("noValue") == null;
54 public void testArrayParse() {
57 + "{ \"key1\" : 123 },"
58 + "{ \"key2\" : \"str\" } ]"
60 JSONObject o = parseJSON(json);
62 Value objects[] = o.getValue("key").getArray();
63 assert objects[0].getObject().getValue("key1").getDouble() == 123;
64 assert objects[1].getObject().getValue("key2").getString().equals("str") == true;
68 public void testEmptyArray() {
69 String json = "{ \"emptyArr\" : [] }";
70 JSONObject o = parseJSON(json);
72 assert o.getValue("noArray") == null;
76 public void testIdentifacatorsParsing() {
82 JSONObject o = parseJSON(json);
84 assert o.getValue("Null").getString() == null;
85 assert o.getValue("True").getBoolean() == true;
86 assert o.getValue("False").getBoolean() == false;
90 public void testCGCallParsing() {
92 + "\"id\" : CGName(1, \"str\", true, false, null)"
95 JSONObject o = parseJSON(json);
96 CGCall cgCall = o.getCGCall("id");
98 assert cgCall.getName().equals("CGName") == true;
99 Value params[] = cgCall.getValues();
101 assertEquals(1d, params[0].getDouble(), 0.0001);
102 assert params[1].getString().equals("str") == true;
103 assert params[2].getBoolean() == true;
104 assert params[3].getBoolean() == false;
105 assert params[4].getDouble() == null;
108 private JSONObject parseJSON(String json) {
109 JSONLexer lexer = new JSONLexer(json);
110 JSONParser parser = new JSONParser(lexer);
111 JSONObject o = parser.parse();