private void getJson()

in src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java [298:343]


    private void getJson(final HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        try {
            Map<String, Map<String, List<String>>> values = new HashMap<>();
            for (final AdaptableDescription desc : allAdaptables) {
                final Map<String, List<String>> adaptableObj;
                if (values.containsKey(desc.adaptable)) {
                    adaptableObj = values.get(desc.adaptable);
                } else {
                    adaptableObj = new HashMap<>();
                    values.put(desc.adaptable, adaptableObj);
                }
                for (final String adapter : desc.adapters) {
                    List<String> conditions = adaptableObj.get(desc.condition == null ? "" : desc.condition);
                    if (conditions == null) {
                        conditions = new ArrayList<>();
                        adaptableObj.put(desc.condition == null ? "" : desc.condition, conditions);
                    }
                    conditions.add(adapter);
                }
            }
            final JsonObjectBuilder obj = Json.createObjectBuilder();

            for (Map.Entry<String, Map<String, List<String>>> entry : values.entrySet()) {
                JsonObjectBuilder adaptable = Json.createObjectBuilder();

                for (Map.Entry<String, List<String>> subEnty : entry.getValue().entrySet()) {
                    if (subEnty.getValue().size() > 1) {
                        JsonArrayBuilder array = Json.createArrayBuilder();
                        for (String condition : subEnty.getValue()) {
                            array.add(condition);
                        }
                        adaptable.add(subEnty.getKey(), array);
                    } else {
                        adaptable.add(subEnty.getKey(), subEnty.getValue().get(0));
                    }
                }

                obj.add(entry.getKey(), adaptable);
            }

            Json.createGenerator(resp.getWriter()).write(obj.build()).flush();
        } catch (final JsonException e) {
            throw new ServletException("Unable to produce JSON", e);
        }
    }