use of org.json.JSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method postprocessTest.
@Test
public void postprocessTest() throws IOException, JSONException {
final int value = 25;
StringWriter stringWriter = new StringWriter();
JSONWriter writer = new JSONWriter(stringWriter);
writer.object().key(PostprocessingUUT.FIELD_NAME).value(value).endObject();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
PostprocessingUUT uut = PostprocessingUUT__JsonHelper.parseFromJson(jp);
assertSame(value + 1, uut.getValue());
}
use of org.json.JSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method valueExtractTest.
@Test
public void valueExtractTest() throws IOException, JSONException {
final int encodedValue = 25;
final int deserializedValue = 40;
StringWriter stringWriter = new StringWriter();
JSONWriter writer = new JSONWriter(stringWriter);
writer.object().key(FormatterUUT.VALUE_FORMATTER_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.getValueFormatter());
}
use of org.json.JSONWriter in project qi4j-sdk by Qi4j.
the class SQLEntityStoreMixin method writeEntityState.
protected void writeEntityState(DefaultEntityState state, Writer writer, String version) 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("version").value(version).key("modified").value(state.lastModified()).key("properties").object();
for (PropertyDescriptor persistentProperty : state.entityDescriptor().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 alfresco-remote-api by Alfresco.
the class AbstractCommentsWebScript method postActivity.
/**
* Post an activity entry for the comment added or deleted
*
* @param json
* - is not sent null with this activity type - only for delete
* @param req
* @param nodeRef
* @param activityType
*/
protected void postActivity(JSONObject json, WebScriptRequest req, NodeRef nodeRef, String activityType) {
String jsonActivityData = "";
String siteId = "";
String page = "";
String title = "";
if (nodeRef == null) {
// to post activity for parent node
return;
}
String strNodeRef = nodeRef.toString();
SiteInfo siteInfo = getSiteInfo(req, COMMENT_CREATED_ACTIVITY.equals(activityType));
// post an activity item, but only if we've got a site
if (siteInfo == null || siteInfo.getShortName() == null || siteInfo.getShortName().length() == 0) {
return;
} else {
siteId = siteInfo.getShortName();
}
// json is not sent null with this activity type - only for delete
if (COMMENT_CREATED_ACTIVITY.equals(activityType)) {
try {
org.json.JSONObject params = new org.json.JSONObject(getOrNull(json, JSON_KEY_PAGE_PARAMS));
String strParams = "";
Iterator<?> itr = params.keys();
while (itr.hasNext()) {
String strParam = itr.next().toString();
strParams += strParam + "=" + params.getString(strParam) + "&";
}
page = getOrNull(json, JSON_KEY_PAGE) + "?" + (strParams != "" ? strParams.substring(0, strParams.length() - 1) : "");
title = getOrNull(json, JSON_KEY_ITEM_TITLE);
} catch (Exception e) {
logger.warn("Error parsing JSON", e);
}
} else {
// COMMENT_DELETED_ACTIVITY
title = req.getParameter(JSON_KEY_ITEM_TITLE);
page = req.getParameter(JSON_KEY_PAGE) + "?" + JSON_KEY_NODEREF + "=" + strNodeRef;
}
try {
JSONWriter jsonWriter = new JSONStringer().object();
jsonWriter.key(JSON_KEY_TITLE).value(title);
jsonWriter.key(JSON_KEY_PAGE).value(page);
jsonWriter.key(JSON_KEY_NODEREF).value(strNodeRef);
jsonActivityData = jsonWriter.endObject().toString();
activityService.postActivity(activityType, siteId, COMMENTS_TOPIC_NAME, jsonActivityData);
} catch (Exception e) {
logger.warn("Error adding comment to activities feed", e);
}
}
use of org.json.JSONWriter in project NMEAParser by tvesalainen.
the class I18nServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ResourceBundle res = I18n.get(req.getLocale());
Map<String, String[]> map = req.getParameterMap();
resp.setContentType("application/json");
resp.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = resp.getWriter();
JSONWriter jw = new JSONWriter(writer);
jw.object();
for (String key : map.keySet()) {
jw.key(key);
String value = res.getString(key);
jw.value(value);
}
jw.endObject();
writer.flush();
}
Aggregations