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