Search in sources :

Example 11 with JSONWriter

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());
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) PostprocessingUUT(com.instagram.common.json.annotation.processor.uut.PostprocessingUUT) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 12 with JSONWriter

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());
}
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 13 with JSONWriter

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);
    }
}
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) 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 14 with JSONWriter

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);
    }
}
Also used : JSONWriter(org.json.JSONWriter) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) JSONObject(org.json.simple.JSONObject) JSONStringer(org.json.JSONStringer) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 15 with JSONWriter

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();
}
Also used : JSONWriter(org.json.JSONWriter) ResourceBundle(java.util.ResourceBundle) PrintWriter(java.io.PrintWriter)

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