Java Code Examples for org.json.simple.JSONObject#entrySet()

The following examples show how to use org.json.simple.JSONObject#entrySet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqoopIDFUtils.java    From sqoop-on-spark with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("unchecked")
public static Map<Object, Object> toMap(JSONObject object) {
  Map<Object, Object> elementMap = new HashMap<Object, Object>();
  Set<Map.Entry<Object, Object>> entries = object.entrySet();
  for (Map.Entry<Object, Object> entry : entries) {
    Object value = entry.getValue();

    if (value instanceof JSONArray) {
      value = toList((JSONArray) value);
    }

    else if (value instanceof JSONObject) {
      value = toMap((JSONObject) value);
    }
    elementMap.put(entry.getKey(), value);
  }
  return elementMap;
}
 
Example 2
Source File: ModelItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void jsonPrettyPrintObject(JSONObject jsonObject, StringBuilder sb, int indent) {
    print(sb, "{\n", indent);
    boolean first = true;
    for (Object o : jsonObject.entrySet()) {
        if (!first) {
            sb.append(",\n");
        }
        Map.Entry en = (Map.Entry)o;
        Object value = en.getValue();
        String key = "\"" + en.getKey() + "\"";
        if (value instanceof JSONObject) {
            print(sb, key+": ", indent+2);
            jsonPrettyPrintObject((JSONObject)value, sb, indent+2);
        } else if (value instanceof JSONArray) {
            print(sb, key+": ", indent+2);
            jsonPrettyPrintArray((JSONArray)value, sb, indent+2);
        } else if (value instanceof String) {
            print(sb, key+": \""+ ((String)value).replace("\"", "\\\"")+"\"", indent+2);
        } else {
            print(sb, key+": "+ value, indent+2);
        }
        first = false;
    }
    sb.append("\n");
    print(sb, "}", indent);
}
 
Example 3
Source File: SubmissionBean.java    From sqoop-on-spark with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Counters restoreCounters(JSONObject object) {
  Set<Map.Entry<String, JSONObject>> groupSet = object.entrySet();
  Counters counters = new Counters();

  for (Map.Entry<String, JSONObject> groupEntry : groupSet) {
    CounterGroup group = new CounterGroup(groupEntry.getKey());
    Set<Map.Entry<String, Long>> counterSet = groupEntry.getValue().entrySet();
    for (Map.Entry<String, Long> counterEntry : counterSet) {
      Counter counter = new Counter(counterEntry.getKey(), counterEntry.getValue());
      group.addCounter(counter);
    }
    counters.addCounterGroup(group);
  }
  return counters;
}
 
Example 4
Source File: LibraryProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the installed libraries/packages.
 * 
 * @return map that maps the library name to the installed version.
 * Returns {@code null} when the attempt to determine the installed
 * libraries failed. When no library is installed then an empty array
 * is returned.
 */
public Map<String,String> installedLibraries() {
    Map<String,String> result = null;
    NpmExecutable executable = NpmExecutable.getDefault(project, false);
    if (executable != null) {
        JSONObject json = executable.list(0);
        if (json != null) {
            result = new HashMap<>();
            JSONObject dependencies = (JSONObject)json.get("dependencies"); // NOI18N
            if (dependencies != null) {
                Set<Map.Entry<Object, Object>> entrySet = dependencies.entrySet();
                for (Map.Entry<Object, Object> entry : entrySet) {
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    if (value instanceof JSONObject) {
                        JSONObject libraryInfo = (JSONObject)value;
                        String versionName = (String)libraryInfo.get("version"); // NOI18N
                        if (versionName != null) {
                            String libraryName = key.toString();
                            result.put(libraryName, versionName);
                        }
                    }
                }
            }
        }
    }
    return result;
}
 
Example 5
Source File: LibraryProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the installed libraries/packages.
 * 
 * @return map that maps the library name to the installed version.
 * Returns {@code null} when the attempt to determine the installed
 * libraries failed. When no library is installed then an empty array
 * is returned.
 */
public Map<String,String> installedLibraries() {
    Map<String,String> result = null;
    BowerExecutable executable = BowerExecutable.getDefault(project, false);
    if (executable != null) {
        JSONObject json = executable.list();
        if (json != null) {
            result = new HashMap<>();
            JSONObject dependencies = (JSONObject)json.get("dependencies"); // NOI18N
            if (dependencies != null) {
                Set<Map.Entry<Object, Object>> entrySet = dependencies.entrySet();
                for (Map.Entry<Object, Object> entry : entrySet) {
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    if (value instanceof JSONObject) {
                        JSONObject libraryInfo = (JSONObject)value;
                        JSONObject metaInfo = (JSONObject)libraryInfo.get("pkgMeta"); // NOI18N
                        if (metaInfo != null) {
                            String versionName = (String)metaInfo.get("version"); // NOI18N
                            if (versionName == null) {
                                versionName = Library.Version.LATEST_VERSION_PLACEHOLDER;
                            }
                            String libraryName = key.toString();
                            result.put(libraryName, versionName);
                        }
                    }
                }
            }
        }
    }
    return result;
}
 
Example 6
Source File: JsonSimpleConfigParser.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
private Map<String, String> parseForcedVariations(JSONObject forcedVariationJson) {
    Map<String, String> userIdToVariationKeyMap = new HashMap<String, String>();
    for (Object obj : forcedVariationJson.entrySet()) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;
        userIdToVariationKeyMap.put(entry.getKey(), entry.getValue());
    }

    return userIdToVariationKeyMap;
}
 
Example 7
Source File: ValidationResultBean.java    From sqoop-on-spark with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private ConfigValidationResult restoreValidationResult(JSONObject item) {
  ConfigValidationResult result  = new ConfigValidationResult();
  Set<Map.Entry<String, JSONArray>> entrySet = item.entrySet();
  for(Map.Entry<String, JSONArray> entry : entrySet) {
    result.addMessages(entry.getKey(), restoreMessageList(entry.getValue()));
  }


  return result;
}
 
Example 8
Source File: AvroStorage.java    From Cubert with Apache License 2.0 5 votes vote down vote up
/**
 * build a property map from a json object
 *
 * @param jsonString  json object in string format
 * @return a property map
 * @throws ParseException
 */
@SuppressWarnings("unchecked")
protected Map<String, Object> parseJsonString(String jsonString) throws ParseException {

    /*parse the json object */
    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(jsonString);

    Set<Entry<String, Object>> entries = obj.entrySet();
    for (Entry<String, Object> entry : entries) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (key.equalsIgnoreCase("debug") || key.equalsIgnoreCase("index")) {
            /* convert long values to integer */
            int v = ((Long) value).intValue();
            obj.put(key, v);
        }
        else if (key.equalsIgnoreCase("schema") || key.matches("field\\d+")) {
           /* convert avro schema (as json object) to string */
            obj.put(key, value.toString().trim());
        }

    }
    return obj;
}
 
Example 9
Source File: AvroStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * build a property map from a json object
 *
 * @param jsonString  json object in string format
 * @return a property map
 * @throws ParseException
 */
@SuppressWarnings("unchecked")
protected Map<String, Object> parseJsonString(String jsonString) throws ParseException {

    /*parse the json object */
    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(jsonString);

    Set<Entry<String, Object>> entries = obj.entrySet();
    for (Entry<String, Object> entry : entries) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (key.equalsIgnoreCase("debug") || key.equalsIgnoreCase("index")) {
            /* convert long values to integer */
            int v = ((Long) value).intValue();
            obj.put(key, v);
        }
        else if (key.equalsIgnoreCase("schema") || key.matches("field\\d+")) {
           /* convert avro schema (as json object) to string */
            obj.put(key, value.toString().trim());
        }

    }
    return obj;
}
 
Example 10
Source File: ChromeManagerAccessor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private ExtensionManager.ExtensitionStatus isInstalledImpl() {
    JSONObject preferences = findPreferences();
    LOGGER.log(Level.FINE, "Chrome preferences: {0}", preferences);
    if (preferences == null) {
        return ExtensionManager.ExtensitionStatus.MISSING;
    }
    LOGGER.log(Level.FINE, "Chrome preferences -> extensions: {0}", preferences.get("extensions"));
    JSONObject extensions = (JSONObject)preferences.get("extensions");
    if (extensions == null) {
        return ExtensionManager.ExtensitionStatus.MISSING;
    }
    LOGGER.log(Level.FINE, "Chrome preferences -> extensions -> settings: {0}", extensions.get("settings"));
    JSONObject settings = (JSONObject)extensions.get("settings");
    if (settings == null) {
        return ExtensionManager.ExtensitionStatus.MISSING;
    }
    for (Object item : settings.entrySet()) {
        Map.Entry e = (Map.Entry)item;
        Object value = e.getValue();
        LOGGER.log(Level.FINE, "Chrome preferences - extensions -> settings -> value/extension: {0}", value);
        // #251250
        if (value instanceof JSONObject) {
            JSONObject extension = (JSONObject) value;
            String path = (String)extension.get("path");
            if (path != null && (path.contains("/extbrowser.chrome/plugins/chrome")
                    || path.contains("\\extbrowser.chrome\\plugins\\chrome")))
            {
                return ExtensionManager.ExtensitionStatus.INSTALLED;
            }
            LOGGER.log(Level.FINE, "Chrome preferences - extensions -> settings -> value/extension -> manifest: {0}", extension.get("manifest"));
            JSONObject manifest = (JSONObject)extension.get("manifest");
            if (manifest != null && PLUGIN_PUBLIC_KEY.equals((String)manifest.get("key"))) {
                String version = (String)manifest.get("version");
                if (isUpdateRequired( version )){
                    return ExtensionManager.ExtensitionStatus.NEEDS_UPGRADE;
                }
                Number n = (Number)extension.get("state");
                if (n != null && n.intValue() != 1) {
                    return ExtensionManager.ExtensitionStatus.DISABLED;
                }
                return ExtensionManager.ExtensitionStatus.INSTALLED;
            }
        }
    }
    return ExtensionManager.ExtensitionStatus.MISSING;
}
 
Example 11
Source File: SequenceUtils.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Gets parameter definitions from swagger
 *
 * @param swaggerObj swagger json
 * @param resource   rest resource object
 * @param method     http method
 * @return parameter mapping for a resource from the swagger definitions
 */
public static List<JSONObject> getResourceParametersFromSwagger(JSONObject swaggerObj, JSONObject resource,
        String method) {
    Map content = (HashMap) resource.get(method);
    JSONArray parameters = (JSONArray) content.get(SOAPToRESTConstants.Swagger.PARAMETERS);
    List<JSONObject> mappingList = new ArrayList<>();
    for (Object param : parameters) {
        String inputType = String.valueOf(((JSONObject) param).get(SOAPToRESTConstants.Swagger.IN));
        if (inputType.equals(SOAPToRESTConstants.Swagger.BODY)) {
            JSONObject schema = (JSONObject) ((JSONObject) param).get(SOAPToRESTConstants.Swagger.SCHEMA);
            String definitionPath = String.valueOf(schema.get(SOAPToRESTConstants.Swagger.REF));
            String definition = definitionPath
                    .replaceAll(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT, SOAPToRESTConstants.EMPTY_STRING);
            JSONObject definitions = (JSONObject) ((JSONObject) swaggerObj
                    .get(SOAPToRESTConstants.Swagger.DEFINITIONS)).get(definition);
            JSONObject properties = (JSONObject) definitions.get(SOAPToRESTConstants.Swagger.PROPERTIES);

            for (Object property : properties.entrySet()) {
                Map.Entry entry = (Map.Entry) property;
                String paramName = String.valueOf(entry.getKey());
                JSONObject value = (JSONObject) entry.getValue();
                JSONArray propArray = new JSONArray();
                if (value.get(SOAPToRESTConstants.Swagger.REF) != null) {
                    String propDefinitionRef = String.valueOf(value.get(SOAPToRESTConstants.Swagger.REF))
                            .replaceAll(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT,
                                    SOAPToRESTConstants.EMPTY_STRING);
                    getNestedDefinitionsFromSwagger(
                            (JSONObject) swaggerObj.get(SOAPToRESTConstants.Swagger.DEFINITIONS), propDefinitionRef,
                            propDefinitionRef, propArray);
                    JSONObject refObj = new JSONObject();
                    refObj.put(paramName, propArray);
                    if (log.isDebugEnabled()) {
                        log.debug("Properties for from resource parameter: " + paramName + " are: " + propArray
                                .toJSONString());
                    }
                    mappingList.add(refObj);
                } else if (String.valueOf(value.get(SOAPToRESTConstants.Swagger.TYPE))
                        .equals(SOAPToRESTConstants.ParamTypes.ARRAY)) {
                    JSONObject arrObj = new JSONObject();
                    arrObj.put(((Map.Entry) property).getKey(), ((Map.Entry) property).getValue());
                    mappingList.add(arrObj);
                    if (log.isDebugEnabled()) {
                        log.debug("Properties for from array type resource parameter: " + ((Map.Entry) property)
                                .getKey() + " are: " + arrObj.toJSONString());
                    }
                }
            }
        } else {
            JSONObject queryObj = new JSONObject();
            queryObj.put(((JSONObject) param).get(SOAPToRESTConstants.Swagger.NAME), param);
            mappingList.add(queryObj);
            if (log.isDebugEnabled()) {
                log.debug("Properties for from query type resource parameter: " + queryObj.toJSONString());
            }
        }
    }
    return mappingList;
}