1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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 
17 package org.json;
18 
19 class JSON {
20     /**
21      * Returns the input if it is a JSON-permissible value; throws otherwise.
22      */
checkDouble(double d)23     static double checkDouble(double d) throws JSONException {
24         if (Double.isInfinite(d) || Double.isNaN(d)) {
25             throw new JSONException("Forbidden numeric value: " + d);
26         }
27         return d;
28     }
29 
toBoolean(Object value)30     static Boolean toBoolean(Object value) {
31         if (value instanceof Boolean) {
32             return (Boolean) value;
33         } else if (value instanceof String) {
34             String stringValue = (String) value;
35             if ("true".equalsIgnoreCase(stringValue)) {
36                 return true;
37             } else if ("false".equalsIgnoreCase(stringValue)) {
38                 return false;
39             }
40         }
41         return null;
42     }
43 
toDouble(Object value)44     static Double toDouble(Object value) {
45         if (value instanceof Double) {
46             return (Double) value;
47         } else if (value instanceof Number) {
48             return ((Number) value).doubleValue();
49         } else if (value instanceof String) {
50             try {
51                 return Double.valueOf((String) value);
52             } catch (NumberFormatException ignored) {
53             }
54         }
55         return null;
56     }
57 
toInteger(Object value)58     static Integer toInteger(Object value) {
59         if (value instanceof Integer) {
60             return (Integer) value;
61         } else if (value instanceof Number) {
62             return ((Number) value).intValue();
63         } else if (value instanceof String) {
64             try {
65                 return (int) Double.parseDouble((String) value);
66             } catch (NumberFormatException ignored) {
67             }
68         }
69         return null;
70     }
71 
toLong(Object value)72     static Long toLong(Object value) {
73         if (value instanceof Long) {
74             return (Long) value;
75         } else if (value instanceof Number) {
76             return ((Number) value).longValue();
77         } else if (value instanceof String) {
78             try {
79                 return (long) Double.parseDouble((String) value);
80             } catch (NumberFormatException ignored) {
81             }
82         }
83         return null;
84     }
85 
toString(Object value)86     static String toString(Object value) {
87         if (value instanceof String) {
88             return (String) value;
89         } else if (value != null) {
90             return String.valueOf(value);
91         }
92         return null;
93     }
94 
typeMismatch(Object indexOrName, Object actual, String requiredType)95     public static JSONException typeMismatch(Object indexOrName, Object actual,
96             String requiredType) throws JSONException {
97         if (actual == null) {
98             throw new JSONException("Value at " + indexOrName + " is null.");
99         } else {
100             throw new JSONException("Value " + actual + " at " + indexOrName
101                     + " of type " + actual.getClass().getName()
102                     + " cannot be converted to " + requiredType);
103         }
104     }
105 
typeMismatch(Object actual, String requiredType)106     public static JSONException typeMismatch(Object actual, String requiredType)
107             throws JSONException {
108         if (actual == null) {
109             throw new JSONException("Value is null.");
110         } else {
111             throw new JSONException("Value " + actual
112                     + " of type " + actual.getClass().getName()
113                     + " cannot be converted to " + requiredType);
114         }
115     }
116 }
117