public void writeRecord(final ORecord<?> iRecord, final String iFetchPlan, String iFormat) throws IOException { if (iFormat == null) iFormat = JSON_FORMAT; final String format = iFetchPlan != null ? iFormat + ",fetchPlan:" + iFetchPlan : iFormat; if (iRecord != null) send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, iRecord.toJSON(format), null); }
public OJSONWriter writeRecord( final int iIdentLevel, final boolean iNewLine, final Object iName, final ORecord<?> iRecord) throws IOException { if (!firstAttribute) out.append(","); format(iIdentLevel, iNewLine); if (iName != null) out.append("\"" + iName.toString() + "\":"); out.append(iRecord.toJSON(format)); firstAttribute = false; return this; }
public static String writeValue(Object iValue, final String iFormat) throws IOException { final StringBuilder buffer = new StringBuilder(); final boolean oldAutoConvertSettings; if (iValue instanceof ORecordLazyMultiValue) { oldAutoConvertSettings = ((ORecordLazyMultiValue) iValue).isAutoConvertToRecord(); ((ORecordLazyMultiValue) iValue).setAutoConvertToRecord(false); } else oldAutoConvertSettings = false; if (iValue == null) buffer.append("null"); else if (iValue instanceof Boolean || iValue instanceof Number) buffer.append(iValue.toString()); else if (iValue instanceof OIdentifiable) { final OIdentifiable linked = (OIdentifiable) iValue; if (linked.getIdentity().isValid()) { buffer.append('\"'); linked.getIdentity().toString(buffer); buffer.append('\"'); } else { if (iFormat != null && iFormat.contains("shallow")) buffer.append("{}"); else { final ORecord<?> rec = linked.getRecord(); if (rec != null) buffer.append(rec.toJSON(iFormat)); else buffer.append("null"); } } } else if (iValue.getClass().isArray()) { if (iValue instanceof byte[]) { buffer.append('\"'); final byte[] source = (byte[]) iValue; if (iFormat != null && iFormat.contains("shallow")) buffer.append(source.length); else buffer.append(OBase64Utils.encodeBytes(source)); buffer.append('\"'); } else { buffer.append('['); int size = Array.getLength(iValue); if (iFormat != null && iFormat.contains("shallow")) buffer.append(size); else for (int i = 0; i < size; ++i) { if (i > 0) buffer.append(","); buffer.append(writeValue(Array.get(iValue, i), iFormat)); } buffer.append(']'); } } else if (iValue instanceof Iterator<?>) iteratorToJSON((Iterator<?>) iValue, iFormat, buffer); else if (iValue instanceof Iterable<?>) iteratorToJSON(((Iterable<?>) iValue).iterator(), iFormat, buffer); else if (iValue instanceof Map<?, ?>) mapToJSON((Map<Object, Object>) iValue, iFormat, buffer); else if (iValue instanceof Map.Entry<?, ?>) { final Map.Entry<?, ?> entry = (Entry<?, ?>) iValue; buffer.append('{'); buffer.append(writeValue(entry.getKey(), iFormat)); buffer.append(":"); buffer.append(writeValue(entry.getValue(), iFormat)); buffer.append('}'); } else if (iValue instanceof Date) { if (iFormat.indexOf("dateAsLong") > -1) buffer.append(((Date) iValue).getTime()); else { buffer.append('"'); buffer.append(ODateHelper.getDateTimeFormatInstance().format(iValue)); buffer.append('"'); } } else if (iValue instanceof BigDecimal) buffer.append(((BigDecimal) iValue).toPlainString()); else if (iValue instanceof ORecordLazyMultiValue) iteratorToJSON(((ORecordLazyMultiValue) iValue).rawIterator(), iFormat, buffer); else if (iValue instanceof Iterable<?>) iteratorToJSON(((Iterable<?>) iValue).iterator(), iFormat, buffer); else { // TREAT IT AS STRING final String v = iValue.toString(); buffer.append('"'); buffer.append(encode(v)); buffer.append('"'); } if (iValue instanceof ORecordLazyMultiValue) ((ORecordLazyMultiValue) iValue).setAutoConvertToRecord(oldAutoConvertSettings); return buffer.toString(); }