View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.util;
23  
24  import static org.opencastproject.util.data.functions.Misc.cast;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  public final class JsonVal {
30    private final Object val;
31  
32    public JsonVal(Object val) {
33      this.val = val;
34    }
35  
36    public boolean isObj() {
37      return val instanceof Map;
38    }
39  
40    public boolean isArr() {
41      return val instanceof List;
42    }
43  
44    public Object get() {
45      return val;
46    }
47  
48    public static String asString(Object o) {
49      return cast(o, String.class);
50    }
51    public static Integer asInteger(Object o) {
52      return cast(o, Integer.class);
53    }
54    public static Long asLong(Object o) {
55      return cast(o, Long.class);
56    }
57    public static Float asFloat(Object o) {
58      return cast(o, Float.class);
59    }
60    public static Double asDouble(Object o) {
61      return cast(o, Double.class);
62    }
63    public static Boolean asBoolean(Object o) {
64      return cast(o, Boolean.class);
65    }
66    public static JsonObj asJsonObj(Object o) {
67      return JsonObj.jsonObj((Map) o);
68    }
69    public static JsonArr asJsonArr(Object o) {
70      return new JsonArr((List) o);
71    }
72    public static JsonVal asJsonVal(Object o) {
73      return new JsonVal(o);
74    }
75  }