| Class | Line # | Actions | |||
|---|---|---|---|---|---|
| Property | 36 | 14 | 7 |
| 1 | package org.json; | |
| 2 | ||
| 3 | /* | |
| 4 | Copyright (c) 2002 JSON.org | |
| 5 | ||
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 7 | of this software and associated documentation files (the "Software"), to deal | |
| 8 | in the Software without restriction, including without limitation the rights | |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 10 | copies of the Software, and to permit persons to whom the Software is | |
| 11 | furnished to do so, subject to the following conditions: | |
| 12 | ||
| 13 | The above copyright notice and this permission notice shall be included in all | |
| 14 | copies or substantial portions of the Software. | |
| 15 | ||
| 16 | The Software shall be used for Good, not Evil. | |
| 17 | ||
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 24 | SOFTWARE. | |
| 25 | */ | |
| 26 | ||
| 27 | import java.util.Enumeration; | |
| 28 | import java.util.Properties; | |
| 29 | ||
| 30 | /** | |
| 31 | * Converts a Property file data into JSONObject and back. | |
| 32 | * | |
| 33 | * @author JSON.org | |
| 34 | * @version 2015-05-05 | |
| 35 | */ | |
| 36 | public class Property | |
| 37 | { | |
| 38 | /** | |
| 39 | * Converts a property file object into a JSONObject. The property file object | |
| 40 | * is a table of name value pairs. | |
| 41 | * | |
| 42 | * @param properties | |
| 43 | * java.util.Properties | |
| 44 | * @return JSONObject | |
| 45 | * @throws JSONException | |
| 46 | */ | |
| 47 | 0 | public static JSONObject toJSONObject(java.util.Properties properties) |
| 48 | throws JSONException | |
| 49 | { | |
| 50 | // can't use the new constructor for Android support | |
| 51 | // JSONObject jo = new JSONObject(properties == null ? 0 : | |
| 52 | // properties.size()); | |
| 53 | 0 | JSONObject jo = new JSONObject(); |
| 54 | 0 | if (properties != null && !properties.isEmpty()) |
| 55 | { | |
| 56 | 0 | Enumeration<?> enumProperties = properties.propertyNames(); |
| 57 | 0 | while (enumProperties.hasMoreElements()) |
| 58 | { | |
| 59 | 0 | String name = (String) enumProperties.nextElement(); |
| 60 | 0 | jo.put(name, properties.getProperty(name)); |
| 61 | } | |
| 62 | } | |
| 63 | 0 | return jo; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Converts the JSONObject into a property file object. | |
| 68 | * | |
| 69 | * @param jo | |
| 70 | * JSONObject | |
| 71 | * @return java.util.Properties | |
| 72 | * @throws JSONException | |
| 73 | */ | |
| 74 | 0 | public static Properties toProperties(JSONObject jo) throws JSONException |
| 75 | { | |
| 76 | 0 | Properties properties = new Properties(); |
| 77 | 0 | if (jo != null) |
| 78 | { | |
| 79 | // Don't use the new entrySet API to maintain Android support | |
| 80 | 0 | for (final String key : jo.keySet()) |
| 81 | { | |
| 82 | 0 | Object value = jo.opt(key); |
| 83 | 0 | if (!JSONObject.NULL.equals(value)) |
| 84 | { | |
| 85 | 0 | properties.put(key, value.toString()); |
| 86 | } | |
| 87 | } | |
| 88 | } | |
| 89 | 0 | return properties; |
| 90 | } | |
| 91 | } |