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-permissable value; throws otherwise. 22 */ 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 30 static Boolean toBoolean(Object value) { 31 if (value instanceof Boolean) { 32 return (Boolean) value; 33 } else if (value instanceof String) { 34 return Boolean.valueOf(((String) value)); 35 } else { 36 return null; 37 } 38 } 39 40 static Double toDouble(Object value) { 41 if (value instanceof Double) { 42 return (Double) value; 43 } else if (value instanceof Number) { 44 return ((Number) value).doubleValue(); 45 } else if (value instanceof String) { 46 try { 47 return Double.valueOf((String) value); 48 } catch (NumberFormatException e) { 49 } 50 } 51 return null; 52 } 53 54 static Integer toInteger(Object value) { 55 if (value instanceof Integer) { 56 return (Integer) value; 57 } else if (value instanceof Number) { 58 return ((Number) value).intValue(); 59 } else if (value instanceof String) { 60 try { 61 return (int) Double.parseDouble((String) value); 62 } catch (NumberFormatException e) { 63 } 64 } 65 return null; 66 } 67 68 static Long toLong(Object value) { 69 if (value instanceof Long) { 70 return (Long) value; 71 } else if (value instanceof Number) { 72 return ((Number) value).longValue(); 73 } else if (value instanceof String) { 74 try { 75 return (long) Double.parseDouble((String) value); 76 } catch (NumberFormatException e) { 77 } 78 } 79 return null; 80 } 81 82 static String toString(Object value) { 83 if (value instanceof String) { 84 return (String) value; 85 } else if (value != null) { 86 return String.valueOf(value); 87 } 88 return null; 89 } 90 91 public static JSONException typeMismatch(Object indexOrName, Object actual, 92 String requiredType) throws JSONException { 93 if (actual == null) { 94 throw new JSONException("Value at " + indexOrName + " is null."); 95 } else { 96 throw new JSONException("Value " + actual + " at " + indexOrName 97 + " of type " + actual.getClass().getName() 98 + " cannot be converted to " + requiredType); 99 } 100 } 101 102 public static JSONException typeMismatch(Object actual, String requiredType) 103 throws JSONException { 104 if (actual == null) { 105 throw new JSONException("Value is null."); 106 } else { 107 throw new JSONException("Value " + actual 108 + " of type " + actual.getClass().getName() 109 + " cannot be converted to " + requiredType); 110 } 111 } 112 } 113