151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson/* 251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * Copyright (C) 2010 The Android Open Source Project 351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * 451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * you may not use this file except in compliance with the License. 651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * You may obtain a copy of the License at 751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * 851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * 1051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * Unless required by applicable law or agreed to in writing, software 1151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 1251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * See the License for the specific language governing permissions and 1451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * limitations under the License. 1551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson */ 1651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 1751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilsonpackage org.json; 1851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 1951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilsonclass JSON { 2051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson /** 21661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson * Returns the input if it is a JSON-permissible value; throws otherwise. 2251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson */ 2351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static double checkDouble(double d) throws JSONException { 2451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (Double.isInfinite(d) || Double.isNaN(d)) { 2551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throw new JSONException("Forbidden numeric value: " + d); 2651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 2751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return d; 2851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 2951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 3051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static Boolean toBoolean(Object value) { 3151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (value instanceof Boolean) { 3251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return (Boolean) value; 3351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof String) { 34661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson String stringValue = (String) value; 35661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson if ("true".equalsIgnoreCase(stringValue)) { 36661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson return true; 37661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } else if ("false".equalsIgnoreCase(stringValue)) { 38661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson return false; 39661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } 4051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 41661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson return null; 4251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 4351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 4451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static Double toDouble(Object value) { 4551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (value instanceof Double) { 4651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return (Double) value; 4751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof Number) { 4851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return ((Number) value).doubleValue(); 4951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof String) { 5051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 5151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return Double.valueOf((String) value); 52661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } catch (NumberFormatException ignored) { 5351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 5451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 5551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return null; 5651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 5751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 5851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static Integer toInteger(Object value) { 5951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (value instanceof Integer) { 6051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return (Integer) value; 6151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof Number) { 6251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return ((Number) value).intValue(); 6351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof String) { 6451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 651483c41ab440a713e2b935801792da9039ec5d61Jesse Wilson return (int) Double.parseDouble((String) value); 66661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } catch (NumberFormatException ignored) { 6751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 6851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 6951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return null; 7051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 7151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 7251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static Long toLong(Object value) { 7351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (value instanceof Long) { 7451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return (Long) value; 7551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof Number) { 7651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return ((Number) value).longValue(); 7751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value instanceof String) { 7851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 791483c41ab440a713e2b935801792da9039ec5d61Jesse Wilson return (long) Double.parseDouble((String) value); 80661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } catch (NumberFormatException ignored) { 8151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 8251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 8351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return null; 8451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 8551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 8651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson static String toString(Object value) { 8751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (value instanceof String) { 8851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return (String) value; 8951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else if (value != null) { 9051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return String.valueOf(value); 9151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 9251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson return null; 9351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 9451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 9551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public static JSONException typeMismatch(Object indexOrName, Object actual, 9651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson String requiredType) throws JSONException { 9751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (actual == null) { 9851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throw new JSONException("Value at " + indexOrName + " is null."); 9951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else { 10051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throw new JSONException("Value " + actual + " at " + indexOrName 10151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson + " of type " + actual.getClass().getName() 10251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson + " cannot be converted to " + requiredType); 10351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 10451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 10551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 10651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public static JSONException typeMismatch(Object actual, String requiredType) 10751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throws JSONException { 10851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson if (actual == null) { 10951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throw new JSONException("Value is null."); 11051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } else { 11151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson throw new JSONException("Value " + actual 11251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson + " of type " + actual.getClass().getName() 11351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson + " cannot be converted to " + requiredType); 11451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 11551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 11651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson} 117