Search in sources :

Example 1 with JsonWriter

use of com.ibm.commons.util.io.json.util.JsonWriter in project org.openntf.domino by OpenNTF.

the class Document method toJson.

@Override
public String toJson(final boolean compact) {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw, compact);
    try {
        jw.startObject();
        jw.outStringProperty("@unid", getUniversalID());
        jw.outStringProperty("@noteid", getNoteID());
        jw.outStringProperty("@replicaid", getParentDatabase().getReplicaID());
        jw.outStringProperty("@metaversalid", getMetaversalID());
        try {
            jw.outStringProperty("@created", getCreated().toGMTISO());
            jw.outStringProperty("@lastmodified", getLastModified().toGMTISO());
            jw.outStringProperty("@lastaccessed", getLastAccessed().toGMTISO());
        } catch (Exception e) {
            DominoUtils.handleException(e, "Exception trying to index Dates.");
        }
        Set<String> keys = keySet();
        for (String key : keys) {
            Item currItem = getFirstItem(key);
            // A beer to anyone who can work out how this could happen, except for the person who identified it!
            if (null != currItem) {
                Type itemType = currItem.getTypeEx();
                try {
                    if (itemType == Type.ATTACHMENT) {
                        jw.outProperty(key, "ATTACHMENT");
                    } else if (itemType == Type.AUTHORS || itemType == Type.READERS || itemType == Type.NAMES || itemType == Type.TEXT || itemType == Type.NUMBERS) {
                        Vector<Object> values = currItem.getValues();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.elementAt(0));
                        } else {
                            jw.outProperty(key, values);
                        }
                    } else if (itemType == Type.DATETIMES) {
                        Vector<DateTime> values = currItem.getValueDateTimeArray();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.get(0).toGMTISO());
                        } else {
                            jw.outProperty(key, TypeUtils.toStrings(values));
                        }
                    } else if (itemType == Type.EMBEDDEDOBJECT) {
                        jw.outProperty(key, "EMBEDDED_OBJECT");
                    } else if (itemType == Type.RICHTEXT) {
                        RichTextItem rtItem = (RichTextItem) currItem;
                        jw.outProperty(key, rtItem.getUnformattedText());
                    } else if (itemType == Type.MIME_PART) {
                        MIMEEntity mimeEntity = currItem.getMIMEEntity();
                        if (mimeEntity != null) {
                            jw.outProperty(key, mimeEntity.getContentAsText());
                        } else {
                            jw.outProperty(key, "MIME_PART null");
                        }
                    }
                } catch (Exception e) {
                    DominoUtils.handleException(e, this);
                    // NTF - temporary
                    e.printStackTrace();
                }
            }
            // if (currItem.getMIMEEntity() == null) {
            // jw.outProperty(key, currItem.getText());
            // } else {
            // String abstractedText = currItem.abstractText(0, false, false);
            // if (null == abstractedText) {
            // jw.outProperty(key, "**MIME ITEM, VALUE CANNOT BE DECODED TO JSON**");
            // } else {
            // jw.outProperty(key, abstractedText);
            // }
            // }
            // Now output attachments
            jw.outProperty("@attachments", getAttachmentNames());
        }
        jw.endObject();
        jw.flush();
    } catch (IOException e) {
        DominoUtils.handleException(e, this);
        return null;
    } catch (JsonException e) {
        DominoUtils.handleException(e, this);
        return null;
    }
    return sw.toString();
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) IOException(java.io.IOException) JsonWriter(com.ibm.commons.util.io.json.util.JsonWriter) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) JsonException(com.ibm.commons.util.io.json.JsonException) DocumentWriteAccessException(org.openntf.domino.exceptions.DocumentWriteAccessException) DataNotCompatibleException(org.openntf.domino.exceptions.DataNotCompatibleException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) NotesException(lotus.domino.NotesException) DominoNonSummaryLimitException(org.openntf.domino.exceptions.DominoNonSummaryLimitException) IOException(java.io.IOException) ItemNotFoundException(org.openntf.domino.exceptions.ItemNotFoundException) DateTime(org.openntf.domino.DateTime) RichTextItem(org.openntf.domino.RichTextItem) Item(org.openntf.domino.Item) Type(org.openntf.domino.Item.Type) MIMEEntity(org.openntf.domino.MIMEEntity) StringWriter(java.io.StringWriter) RichTextItem(org.openntf.domino.RichTextItem) Vector(java.util.Vector) ItemVector(org.openntf.domino.iterators.ItemVector)

Example 2 with JsonWriter

use of com.ibm.commons.util.io.json.util.JsonWriter in project org.openntf.domino by OpenNTF.

the class ViewEntry method toJson.

public String toJson(boolean compact) {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw, compact);
    // $NON-NLS-1$
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    try {
        jw.startObject();
        // $NON-NLS-1$
        jw.outStringProperty("@unid", getUniversalID());
        // $NON-NLS-1$
        jw.outStringProperty("@noteid", getNoteID());
        // $NON-NLS-1$
        jw.outStringProperty("@replicaid", getAncestorDatabase().getReplicaID());
        // $NON-NLS-1$
        jw.outStringProperty("@metaversalid", getMetaversalID());
        boolean resetPreferDates = false;
        if (!isPreferJavaDates()) {
            setPreferJavaDates(true);
            resetPreferDates = true;
        }
        Map<String, Object> colVals = getColumnValuesMap();
        Set<String> keys = colVals.keySet();
        for (String key : keys) {
            Object colVal = colVals.get(key);
            if (colVal instanceof java.util.Date) {
                jw.outProperty(key, sdf.format((java.util.Date) colVal));
            } else {
                jw.outProperty(key, colVal.toString());
            }
        }
        if (resetPreferDates) {
            setPreferJavaDates(false);
        }
        jw.endObject();
        jw.flush();
    } catch (IOException e) {
        DominoUtils.handleException(e, this.getDocument());
        return null;
    } catch (JsonException e) {
        DominoUtils.handleException(e, this.getDocument());
        return null;
    }
    return sw.toString();
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) JsonWriter(com.ibm.commons.util.io.json.util.JsonWriter) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

JsonException (com.ibm.commons.util.io.json.JsonException)2 JsonWriter (com.ibm.commons.util.io.json.util.JsonWriter)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Vector (java.util.Vector)1 NotesException (lotus.domino.NotesException)1 DateTime (org.openntf.domino.DateTime)1 Item (org.openntf.domino.Item)1 Type (org.openntf.domino.Item.Type)1 MIMEEntity (org.openntf.domino.MIMEEntity)1 RichTextItem (org.openntf.domino.RichTextItem)1 DataNotCompatibleException (org.openntf.domino.exceptions.DataNotCompatibleException)1 DocumentWriteAccessException (org.openntf.domino.exceptions.DocumentWriteAccessException)1 DominoNonSummaryLimitException (org.openntf.domino.exceptions.DominoNonSummaryLimitException)1 ItemNotFoundException (org.openntf.domino.exceptions.ItemNotFoundException)1 OpenNTFNotesException (org.openntf.domino.exceptions.OpenNTFNotesException)1 UserAccessException (org.openntf.domino.exceptions.UserAccessException)1 ItemVector (org.openntf.domino.iterators.ItemVector)1