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();
}
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');
}
}
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);
}
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);
}
}
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);
}
Aggregations