Example usage for org.json.simple JSONObject keySet

List of usage examples for org.json.simple JSONObject keySet

Introduction

In this page you can find the example usage for org.json.simple JSONObject keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:org.geotools.data.couchdb.client.CouchDBUtils.java

public static SimpleFeatureType createFeatureType(JSONObject obj, String name) throws IOException {

    JSONObject props = (JSONObject) obj.get("properties");
    JSONObject geom = (JSONObject) obj.get("geometry");

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(name);//from  w w  w .  ja v  a 2  s .c om
    for (Object k : props.keySet()) {
        builder.add(k.toString(), String.class);
    }
    String geomType = geom.get("type").toString();
    Class clazz = Geometries.getForName(geomType).getBinding();
    if (clazz == null) {
        throw new IOException("Unable to parse geometry type from " + geomType);
    }
    builder.add("geometry", clazz, (CoordinateReferenceSystem) null); // @todo find type from name?
    return builder.buildFeatureType();
}

From source file:org.geotools.data.couchdb.CouchDBDataStore.java

@Override
protected List<Name> createTypeNames() throws IOException {
    List<Name> names = new ArrayList<Name>();
    JSONObject design = dbconn().getDesignDocument();

    // for now, expect that db is spatial
    // for each spatial view find a normal view with the same name
    // the spatial view should be setup to return id only
    // the normal view must return geojson
    // @todo delegate to strategy for db layout
    JSONObject spatial = (JSONObject) design.get("spatial"); // safe - internal to geocouch
    JSONObject views = (JSONObject) design.get("views"); // safe - internal to geocouch
    for (Object key : spatial.keySet()) {
        if (views.containsKey(key)) {
            // using the dot separator is ok, since dbnames cannot contain dots
            names.add(new NameImpl(couchURL, dbName + "." + key.toString()));
        }//from w w w .  ja va2s.c o m
    }
    return names;
}

From source file:org.geotools.data.couchdb.CouchDBFeatureReader.java

@Override
public SimpleFeature next() throws IOException, IllegalArgumentException, NoSuchElementException {
    JSONObject row = (JSONObject) features.get(index++);
    JSONObject feature = (JSONObject) row.get("value");
    JSONObject props = (JSONObject) feature.get("properties");
    JSONObject geom = (JSONObject) feature.get("geometry");
    for (Object k : props.keySet()) {
        builder.set(k.toString(), props.get(k));
    }//www. ja v a 2  s. c  om
    try {
        builder.set("geometry", read(geom));
    } catch (Exception ex) {
        throw new IOException("Error handling geometry", ex);
    }
    return builder.buildFeature(row.get("id").toString());
}

From source file:org.geotools.data.couchdb.CouchDBFeatureReader.java

private void visit(JSONObject obj, GeometryHandler handler) throws Exception {
    handler.startObject();/*from   w  w  w .j  av  a 2s.co  m*/
    for (Object k : obj.keySet()) {
        handler.startObjectEntry(k.toString());
        visit(obj.get(k), handler);
    }
    handler.endObject();
}

From source file:org.ihtsdo.otf.query.implementation.JSONToReport.java

/**
 * Load both set cardinality reports and reports for the specific
 * {@link java.util.Set} values.//from www. ja  v  a 2 s . co m
 *
 * @param jsonLine
 */
private void loadMap(String jsonLine) {
    try {
        JSONObject json = (JSONObject) new JSONParser().parse(jsonLine);
        for (Object key : json.keySet()) {
            String value = json.get(key).toString();
            if (value.matches("[0-9]+")) {
                this.resultMap.put(key.toString(), Integer.parseInt(value));
            } else if (value.matches("\\[[0-9]+.*\\]")) {
                Set<Long> longSet = new HashSet<>();
                for (String l : value.split(",")) {
                    l = StringUtils.remove(l, '[');
                    l = StringUtils.remove(l, ']');
                    l = l.trim();
                    if (l.matches("[0-9]+")) {
                        longSet.add(Long.parseLong(l));
                    }
                }
                this.resultMap.put(key.toString(), longSet);
            }

        }
    } catch (ParseException ex) {
        Logger.getLogger(JSONToReport.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.imsglobal.lti.toolProvider.service.ToolSettings.java

/**
 * Get the tool settings.//from   w ww.ja  va  2s  .c  o m
 *
 * @param int          $mode       Mode for request (optional, default is current level only)
 *
 * @return mixed The array of settings if successful, otherwise false
 */
@SuppressWarnings("rawtypes")
public Map<String, List<String>> get(int mode) {
    JSONObject response = new JSONObject();
    Map<String, List<String>> parameter = new HashMap<String, List<String>>();
    if (mode == MODE_ALL_LEVELS) {
        LTIUtil.setParameter(parameter, "bubble", "all");
    } else if (mode == MODE_DISTINCT_NAMES) {
        LTIUtil.setParameter(parameter, "bubble", "distinct");
    }
    LTIMessage http = this.send("GET", parameter);
    JSONObject responseJson = http.getResponseJson();
    if (!http.isOk()) {
        response = null;
    } else if (simple) {
        response = responseJson;
    } else if (responseJson.containsKey("@graph")) {
        JSONObject graph = (JSONObject) responseJson.get("@graph");
        for (Object level : graph.keySet()) {
            JSONObject jlevel = (JSONObject) level;
            JSONObject settings = (JSONObject) jlevel.get("custom");
            settings.remove("@id");

            String type = (String) jlevel.get("@type");
            String theLevel = LEVEL_NAMES.get(type);

            response.put(theLevel, settings);
        }
    }
    //response is a JSONObject, with keys pointing to JSON objects.  Need to parse this down to 
    //Map<String, List<String>> to match the rest of the library, and get it out of JSON
    return parseToMap(response);
}

From source file:org.imsglobal.lti.toolProvider.service.ToolSettings.java

private Map<String, List<String>> parseToMap(JSONObject json) {
    Map<String, List<String>> returnMap = new HashMap<String, List<String>>();
    for (Object key : json.keySet()) {
        Object values = json.get(key);
        if (values instanceof JSONArray) {
            for (Object v2 : (JSONArray) values) {
                JSONObject v2Json = (JSONObject) v2;
                //depends on json that is sent - debug with real json
                throw new RuntimeException("Got to JSON Parse: " + v2.toString());
            }//w  w  w.  j av a2 s .  c o  m
        }
    }
    return returnMap;
}

From source file:org.imsglobal.lti2.LTI2Util.java

@SuppressWarnings({ "unchecked", "unused" })
public static Object getSettings(HttpServletRequest request, String scope, JSONObject link_settings,
        JSONObject binding_settings, JSONObject proxy_settings, String link_url, String binding_url,
        String proxy_url) {/*from w ww . j a va  2s .c  o  m*/
    // Check to see if we are doing the bubble
    String bubbleStr = request.getParameter("bubble");
    String acceptHdr = request.getHeader("Accept");
    String contentHdr = request.getContentType();

    if (bubbleStr != null && bubbleStr.equals("all")
            && acceptHdr.indexOf(StandardServices.TOOLSETTINGS_FORMAT) < 0) {
        return "Simple format does not allow bubble=all";
    }

    if (SCOPE_LtiLink.equals(scope) || SCOPE_ToolProxyBinding.equals(scope) || SCOPE_ToolProxy.equals(scope)) {
        // All good
    } else {
        return "Bad Setttings Scope=" + scope;
    }

    boolean bubble = bubbleStr != null && "GET".equals(request.getMethod());
    boolean distinct = bubbleStr != null && "distinct".equals(bubbleStr);
    boolean bubbleAll = bubbleStr != null && "all".equals(bubbleStr);

    // Check our output format
    boolean acceptComplex = acceptHdr == null || acceptHdr.indexOf(StandardServices.TOOLSETTINGS_FORMAT) >= 0;

    if (distinct && link_settings != null && scope.equals(SCOPE_LtiLink)) {
        Iterator<String> i = link_settings.keySet().iterator();
        while (i.hasNext()) {
            String key = (String) i.next();
            if (binding_settings != null)
                binding_settings.remove(key);
            if (proxy_settings != null)
                proxy_settings.remove(key);
        }
    }

    if (distinct && binding_settings != null && scope.equals(SCOPE_ToolProxyBinding)) {
        Iterator<String> i = binding_settings.keySet().iterator();
        while (i.hasNext()) {
            String key = (String) i.next();
            if (proxy_settings != null)
                proxy_settings.remove(key);
        }
    }

    // Lets get this party started...
    JSONObject jsonResponse = null;
    if ((distinct || bubbleAll) && acceptComplex) {
        jsonResponse = new JSONObject();
        jsonResponse.put(LTI2Constants.CONTEXT, StandardServices.TOOLSETTINGS_CONTEXT);
        JSONArray graph = new JSONArray();
        boolean started = false;
        if (link_settings != null && SCOPE_LtiLink.equals(scope)) {
            JSONObject cjson = new JSONObject();
            cjson.put(LTI2Constants.JSONLD_ID, link_url);
            cjson.put(LTI2Constants.TYPE, SCOPE_LtiLink);
            cjson.put(LTI2Constants.CUSTOM, link_settings);
            graph.add(cjson);
            started = true;
        }
        if (binding_settings != null && (started || SCOPE_ToolProxyBinding.equals(scope))) {
            JSONObject cjson = new JSONObject();
            cjson.put(LTI2Constants.JSONLD_ID, binding_url);
            cjson.put(LTI2Constants.TYPE, SCOPE_ToolProxyBinding);
            cjson.put(LTI2Constants.CUSTOM, binding_settings);
            graph.add(cjson);
            started = true;
        }
        if (proxy_settings != null && (started || SCOPE_ToolProxy.equals(scope))) {
            JSONObject cjson = new JSONObject();
            cjson.put(LTI2Constants.JSONLD_ID, proxy_url);
            cjson.put(LTI2Constants.TYPE, SCOPE_ToolProxy);
            cjson.put(LTI2Constants.CUSTOM, proxy_settings);
            graph.add(cjson);
        }
        jsonResponse.put(LTI2Constants.GRAPH, graph);

    } else if (distinct) { // Simple format output
        jsonResponse = proxy_settings;
        if (SCOPE_LtiLink.equals(scope)) {
            jsonResponse.putAll(binding_settings);
            jsonResponse.putAll(link_settings);
        } else if (SCOPE_ToolProxyBinding.equals(scope)) {
            jsonResponse.putAll(binding_settings);
        }
    } else { // bubble not specified
        jsonResponse = new JSONObject();
        jsonResponse.put(LTI2Constants.CONTEXT, StandardServices.TOOLSETTINGS_CONTEXT);
        JSONObject theSettings = null;
        String endpoint = null;
        if (SCOPE_LtiLink.equals(scope)) {
            endpoint = link_url;
            theSettings = link_settings;
        } else if (SCOPE_ToolProxyBinding.equals(scope)) {
            endpoint = binding_url;
            theSettings = binding_settings;
        }
        if (SCOPE_ToolProxy.equals(scope)) {
            endpoint = proxy_url;
            theSettings = proxy_settings;
        }
        if (acceptComplex) {
            JSONArray graph = new JSONArray();
            JSONObject cjson = new JSONObject();
            cjson.put(LTI2Constants.JSONLD_ID, endpoint);
            cjson.put(LTI2Constants.TYPE, scope);
            cjson.put(LTI2Constants.CUSTOM, theSettings);
            graph.add(cjson);
            jsonResponse.put(LTI2Constants.GRAPH, graph);
        } else {
            jsonResponse = theSettings;
        }
    }
    return jsonResponse;
}

From source file:org.imsglobal.lti2.LTI2Util.java

public static boolean mergeLTI2Custom(Properties custom, String customstr) {
    if (customstr == null || customstr.length() < 1)
        return true;
    JSONObject json = null;
    try {/*ww w . ja va 2s.co  m*/
        json = (JSONObject) JSONValue.parse(customstr.trim());
    } catch (Exception e) {
        M_log.warning("mergeLTI2Custom could not parse\n" + customstr);
        M_log.warning(e.getLocalizedMessage());
        return false;
    }

    // This could happen if the old settings service was used
    // on an LTI 2.x placement to put in settings that are not
    // JSON - we just ignore it.
    if (json == null)
        return false;
    Iterator<?> keys = json.keySet().iterator();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        if (custom.containsKey(key))
            continue;
        Object value = json.get(key);
        if (value instanceof String) {
            setProperty(custom, key, (String) value);
        }
    }
    return true;
}

From source file:org.jaggeryjs.jaggery.app.mgt.TomcatJaggeryWebappsDeployer.java

private static void addErrorPages(Context context, JSONObject obj) {
    JSONObject arr = (JSONObject) obj.get(JaggeryCoreConstants.JaggeryConfigParams.ERROR_PAGES);
    if (arr != null) {
        for (Object keys : arr.keySet()) {
            ErrorPage errPage = new ErrorPage();
            errPage.setErrorCode((String) keys);
            errPage.setLocation((String) arr.get(keys));
            context.addErrorPage(errPage);
        }//from  w  ww  . j a v  a  2 s .  c  om
    }
}