/ app / src / main / java / com / reandroid / json / Property.java
Property.java
 1  /*
 2   * Copyright (c) 2002 JSON.org (now "Public Domain")
 3   * This is NOT property of REAndroid
 4   * This package is renamed from org.json.* to avoid class conflict when used on android platforms
 5  */
 6  package com.reandroid.json;
 7  
 8  import java.util.Enumeration;
 9  import java.util.Properties;
10  
11  public class Property {
12  
13      public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
14          // can't use the new constructor for Android support
15          // JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
16          JSONObject jo = new JSONObject();
17          if (properties != null && !properties.isEmpty()) {
18              Enumeration<?> enumProperties = properties.propertyNames();
19              while(enumProperties.hasMoreElements()) {
20                  String name = (String)enumProperties.nextElement();
21                  jo.put(name, properties.getProperty(name));
22              }
23          }
24          return jo;
25      }
26  
27      public static Properties toProperties(JSONObject jo)  throws JSONException {
28          Properties  properties = new Properties();
29          if (jo != null) {
30          	// Don't use the new entrySet API to maintain Android support
31              for (final String key : jo.keySet()) {
32                  Object value = jo.opt(key);
33                  if (!JSONObject.NULL.equals(value)) {
34                      properties.put(key, value.toString());
35                  }
36              }
37          }
38          return properties;
39      }
40  }