Search in sources :

Example 21 with JSONWriter

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

the class HttpUtilities method respondJSON.

public static void respondJSON(HttpServletResponse response, Jsonizable o, Properties options) throws IOException, JSONException {
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-Type", "application/json");
    Writer w = response.getWriter();
    JSONWriter writer = new JSONWriter(w);
    o.write(writer, options);
    w.flush();
    w.close();
}
Also used : JSONWriter(org.json.JSONWriter) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer)

Example 22 with JSONWriter

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

the class Project method saveToWriter.

protected void saveToWriter(Writer writer, Properties options) throws IOException {
    writer.write(RefineServlet.VERSION);
    writer.write('\n');
    writer.write("columnModel=\n");
    columnModel.save(writer, options);
    writer.write("history=\n");
    history.save(writer, options);
    for (String modelName : overlayModels.keySet()) {
        writer.write("overlayModel:");
        writer.write(modelName);
        writer.write("=");
        try {
            JSONWriter jsonWriter = new JSONWriter(writer);
            overlayModels.get(modelName).write(jsonWriter, options);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        writer.write('\n');
    }
    writer.write("rowCount=");
    writer.write(Integer.toString(rows.size()));
    writer.write('\n');
    for (Row row : rows) {
        row.save(writer, options);
        writer.write('\n');
    }
}
Also used : JSONWriter(org.json.JSONWriter) JSONException(org.json.JSONException)

Example 23 with JSONWriter

use of org.json.JSONWriter in project qi4j-sdk by Qi4j.

the class OrgJsonValueSerializer method adaptOutput.

// 
// Serialization
// 
@Override
protected OrgJsonOutput adaptOutput(OutputStream output) throws Exception {
    Writer writer = new OutputStreamWriter(output, "UTF-8");
    JSONWriter json = new JSONWriter(writer);
    return new OrgJsonOutput(writer, json);
}
Also used : JSONWriter(org.json.JSONWriter) OrgJsonOutput(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializer.OrgJsonOutput) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) JSONWriter(org.json.JSONWriter)

Example 24 with JSONWriter

use of org.json.JSONWriter in project qi4j-sdk by Qi4j.

the class MapEntityStoreMixin method writeEntityState.

protected void writeEntityState(DefaultEntityState state, Writer writer, String version, long lastModified) throws EntityStoreException {
    try {
        JSONWriter json = new JSONWriter(writer);
        JSONWriter properties = json.object().key("identity").value(state.identity().identity()).key("application_version").value(application.version()).key("type").value(first(state.entityDescriptor().types()).getName()).key("types").value(toList(map(Classes.toClassName(), state.entityDescriptor().mixinTypes()))).key("version").value(version).key("modified").value(lastModified).key("properties").object();
        EntityDescriptor entityType = state.entityDescriptor();
        for (PropertyDescriptor persistentProperty : entityType.state().properties()) {
            Object value = state.properties().get(persistentProperty.qualifiedName());
            json.key(persistentProperty.qualifiedName().name());
            if (value == null || ValueType.isPrimitiveValue(value)) {
                json.value(value);
            } else {
                String serialized = valueSerialization.serialize(value);
                if (serialized.startsWith("{")) {
                    json.value(new JSONObject(serialized));
                } else if (serialized.startsWith("[")) {
                    json.value(new JSONArray(serialized));
                } else {
                    json.value(serialized);
                }
            }
        }
        JSONWriter associations = properties.endObject().key("associations").object();
        for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
            EntityReference value = stateNameEntityReferenceEntry.getValue();
            associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
        }
        JSONWriter manyAssociations = associations.endObject().key("manyassociations").object();
        for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
            JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
            for (EntityReference entityReference : stateNameListEntry.getValue()) {
                assocs.value(entityReference.identity());
            }
            assocs.endArray();
        }
        manyAssociations.endObject().endObject();
    } catch (JSONException e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) EntityReference(org.qi4j.api.entity.EntityReference) JSONObject(org.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with JSONWriter

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

the class DeserializeTest method importsTest.

@Test
public void importsTest() throws IOException, JSONException {
    final String encodedValue = "test";
    final String deserializedValue = ":test";
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key("string_field").value(encodedValue).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    ImportsUUT uut = ImportsUUT__JsonHelper.parseFromJson(jp);
    assertEquals(deserializedValue, uut.mStringField);
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ImportsUUT(com.instagram.common.json.annotation.processor.uut.ImportsUUT) TypeFormatterImportsUUT(com.instagram.common.json.annotation.processor.parent.TypeFormatterImportsUUT) JsonParser(com.fasterxml.jackson.core.JsonParser) 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