Search in sources :

Example 6 with JSONWriter

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

the class GetStarredExpressionsCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<String> starredExpressions = ((TopList) ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        writer.key("expressions");
        writer.array();
        for (String s : starredExpressions) {
            writer.object();
            writer.key("code");
            writer.value(s);
            writer.endObject();
        }
        writer.endArray();
        writer.endObject();
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : TopList(com.google.refine.preference.TopList) JSONWriter(org.json.JSONWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 7 with JSONWriter

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

the class ProjectMetadataUtilities method saveToFile.

protected static void saveToFile(ProjectMetadata projectMeta, File metadataFile) throws JSONException, IOException {
    Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
    try {
        JSONWriter jsonWriter = new JSONWriter(writer);
        projectMeta.write(jsonWriter);
    } finally {
        writer.close();
    }
}
Also used : JSONWriter(org.json.JSONWriter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 8 with JSONWriter

use of org.json.JSONWriter in project ig-json-parser by Instagram.

the class DeserializeTest method fieldAssignmentTest.

@Test
public void fieldAssignmentTest() throws IOException, JSONException {
    final int encodedValue = 25;
    final int deserializedValue = -encodedValue;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(FormatterUUT.FIELD_ASSIGNMENT_FIELD_NAME).value(encodedValue).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    FormatterUUT uut = FormatterUUT__JsonHelper.parseFromJson(jp);
    assertSame(deserializedValue, uut.getFieldAssignmentFormatter());
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) StringWriter(java.io.StringWriter) FormatterUUT(com.instagram.common.json.annotation.processor.uut.FormatterUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 9 with JSONWriter

use of org.json.JSONWriter in project ig-json-parser by Instagram.

the class DeserializeTest method enumTest.

@Test
public void enumTest() throws IOException, JSONException {
    final EnumUUT.EnumType value = EnumUUT.EnumType.VALUE2;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(EnumUUT.ENUM_FIELD_NAME).value(value.toString()).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    EnumUUT uut = EnumUUT__JsonHelper.parseFromJson(jp);
    assertSame(value, uut.enumField);
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) StringWriter(java.io.StringWriter) EnumUUT(com.instagram.common.json.annotation.processor.uut.EnumUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 10 with JSONWriter

use of org.json.JSONWriter in project ig-json-parser by Instagram.

the class DeserializeTest method TypeFormatterImportsTest.

@Test
public void TypeFormatterImportsTest() throws IOException, JSONException {
    final String encodedValue = "test";
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key("callee_ref").object().key("string_field").value(encodedValue).endObject().endObject();
    String inputString = stringWriter.toString();
    TypeFormatterImportsContainerUUT container = TypeFormatterImportsContainerUUT__JsonHelper.parseFromJson(inputString);
    TypeFormatterImportsUUT uut = container.mTypeFormatterImports;
    assertTrue(uut instanceof TypeFormatterImportsCompanionUUT);
    assertEquals(encodedValue, uut.mString);
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) TypeFormatterImportsUUT(com.instagram.common.json.annotation.processor.parent.TypeFormatterImportsUUT) StringWriter(java.io.StringWriter) TypeFormatterImportsCompanionUUT(com.instagram.common.json.annotation.processor.parent.TypeFormatterImportsCompanionUUT) TypeFormatterImportsContainerUUT(com.instagram.common.json.annotation.processor.dependent.TypeFormatterImportsContainerUUT) Test(org.junit.Test)

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