Search in sources :

Example 16 with JSONWriter

use of org.json.JSONWriter in project OpenRefine by OpenRefine.

the class RefineBrokerImpl method openProject.

// ---------------------------------------------------------------------------------
@Override
protected void openProject(HttpServletResponse response, String pid) throws Exception {
    Project project = getProject(pid);
    Writer w = response.getWriter();
    JSONWriter writer = new JSONWriter(w);
    writer.object();
    writer.key("status");
    writer.value("ok");
    writer.key("data");
    writer.value(project.data);
    writer.key("metadata");
    writer.value(new JSONObject(project.metadata));
    writer.key("transformations");
    writer.array();
    for (String s : project.transformations) {
        writer.value(new JSONObject(s));
    }
    writer.endArray();
    writer.endObject();
    w.flush();
    w.close();
}
Also used : JSONWriter(org.json.JSONWriter) JSONObject(org.json.JSONObject) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer)

Example 17 with JSONWriter

use of org.json.JSONWriter in project OpenRefine by OpenRefine.

the class AppEngineRefineBrokerImpl method openProject.

// ---------------------------------------------------------------------------------
protected void openProject(HttpServletResponse response, String pid) throws Exception {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Project project = getProject(pm, pid);
        Writer w = response.getWriter();
        JSONWriter writer = new JSONWriter(w);
        writer.object();
        writer.key("data");
        writer.value(project.data.toString());
        writer.key("transformations");
        writer.array();
        for (Text s : project.transformations) {
            writer.value(s.toString());
        }
        writer.endArray();
        writer.endObject();
        w.flush();
        w.close();
    } finally {
        pm.close();
    }
}
Also used : JSONWriter(org.json.JSONWriter) PersistenceManager(javax.jdo.PersistenceManager) Text(com.google.appengine.api.datastore.Text) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer)

Example 18 with JSONWriter

use of org.json.JSONWriter in project OpenRefine by OpenRefine.

the class GetExpressionLanguageInfoCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        Properties options = new Properties();
        writer.object();
        writer.key("functions");
        writer.object();
        {
            for (Entry<String, Function> entry : ControlFunctionRegistry.getFunctionMapping()) {
                writer.key(entry.getKey());
                entry.getValue().write(writer, options);
            }
        }
        writer.endObject();
        writer.key("controls");
        writer.object();
        {
            for (Entry<String, Control> entry : ControlFunctionRegistry.getControlMapping()) {
                writer.key(entry.getKey());
                entry.getValue().write(writer, options);
            }
        }
        writer.endObject();
        writer.endObject();
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Entry(java.util.Map.Entry) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 19 with JSONWriter

use of org.json.JSONWriter in project OpenRefine by OpenRefine.

the class GetImportingConfigurationCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Writer w = response.getWriter();
    response.setContentType("application/json");
    JSONWriter writer = new JSONWriter(w);
    try {
        writer.object();
        writer.key("config");
        ImportingManager.writeConfiguration(writer, new Properties());
        writer.endObject();
    } catch (JSONException e) {
        throw new ServletException(e);
    } finally {
        w.flush();
        w.close();
    }
}
Also used : JSONWriter(org.json.JSONWriter) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) Properties(java.util.Properties) Writer(java.io.Writer) JSONWriter(org.json.JSONWriter)

Example 20 with JSONWriter

use of org.json.JSONWriter in project OpenRefine by OpenRefine.

the class GetLanguagesCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String modname = request.getParameter("module");
    if (modname == null) {
        modname = "core";
    }
    ButterflyModule module = this.servlet.getModule(modname);
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        writer.key("languages");
        writer.array();
        // we always have English and it's always first
        writeLangData(writer, "en", "English");
        FileFilter fileFilter = new WildcardFileFilter("translation-*.json");
        for (File file : new File(module.getPath() + File.separator + "langs").listFiles(fileFilter)) {
            String lang = file.getName().split("-")[1].split("\\.")[0];
            if (!"en".equals(lang) && !"default".equals(lang)) {
                JSONObject json = LoadLanguageCommand.loadLanguage(this.servlet, "core", lang);
                if (json != null) {
                    String label = json.getString("name");
                    writeLangData(writer, lang, label);
                }
            }
        }
        writer.endArray();
        writer.endObject();
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) FileFilter(java.io.FileFilter) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) File(java.io.File) ButterflyModule(edu.mit.simile.butterfly.ButterflyModule)

Aggregations

JSONWriter (org.json.JSONWriter)26 Writer (java.io.Writer)9 StringWriter (java.io.StringWriter)8 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)6 JSONException (org.json.JSONException)6 Test (org.junit.Test)6 JsonFactory (com.fasterxml.jackson.core.JsonFactory)5 JsonParser (com.fasterxml.jackson.core.JsonParser)5 JSONObject (org.json.JSONObject)5 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)3 Properties (java.util.Properties)3 ServletException (javax.servlet.ServletException)3 JSONStringer (org.json.JSONStringer)3 TypeFormatterImportsUUT (com.instagram.common.json.annotation.processor.parent.TypeFormatterImportsUUT)2 FormatterUUT (com.instagram.common.json.annotation.processor.uut.FormatterUUT)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2