Ejemplo n.º 1
0
  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();
  }
  public static void simpleValueToStream(
      final StringBuilder iBuffer, final OType iType, final Object iValue) {
    if (iValue == null || iType == null) return;
    switch (iType) {
      case STRING:
        iBuffer.append('"');
        iBuffer.append(OStringSerializerHelper.encode(iValue.toString()));
        iBuffer.append('"');
        break;

      case BOOLEAN:
        iBuffer.append(String.valueOf(iValue));
        break;

      case INTEGER:
        iBuffer.append(String.valueOf(iValue));
        break;

      case FLOAT:
        iBuffer.append(String.valueOf(iValue));
        iBuffer.append('f');
        break;

      case DECIMAL:
        if (iValue instanceof BigDecimal) iBuffer.append(((BigDecimal) iValue).toPlainString());
        else iBuffer.append(String.valueOf(iValue));
        iBuffer.append('c');
        break;

      case LONG:
        iBuffer.append(String.valueOf(iValue));
        iBuffer.append('l');
        break;

      case DOUBLE:
        iBuffer.append(String.valueOf(iValue));
        iBuffer.append('d');
        break;

      case SHORT:
        iBuffer.append(String.valueOf(iValue));
        iBuffer.append('s');
        break;

      case BYTE:
        if (iValue instanceof Character) iBuffer.append((int) ((Character) iValue).charValue());
        else if (iValue instanceof String)
          iBuffer.append(String.valueOf((int) ((String) iValue).charAt(0)));
        else iBuffer.append(String.valueOf(iValue));
        iBuffer.append('b');
        break;

      case BINARY:
        iBuffer.append(OStringSerializerHelper.BINARY_BEGINEND);
        if (iValue instanceof Byte)
          iBuffer.append(OBase64Utils.encodeBytes(new byte[] {((Byte) iValue).byteValue()}));
        else iBuffer.append(OBase64Utils.encodeBytes((byte[]) iValue));
        iBuffer.append(OStringSerializerHelper.BINARY_BEGINEND);
        break;

      case DATE:
        if (iValue instanceof Date) {
          // RESET HOURS, MINUTES, SECONDS AND MILLISECONDS
          Calendar calendar = Calendar.getInstance();
          calendar.setTime((Date) iValue);
          calendar.set(Calendar.HOUR_OF_DAY, 0);
          calendar.set(Calendar.MINUTE, 0);
          calendar.set(Calendar.SECOND, 0);
          calendar.set(Calendar.MILLISECOND, 0);

          iBuffer.append(calendar.getTimeInMillis());
        } else iBuffer.append(iValue);
        iBuffer.append('a');
        break;

      case DATETIME:
        if (iValue instanceof Date) iBuffer.append(((Date) iValue).getTime());
        else iBuffer.append(iValue);
        iBuffer.append('t');
        break;
    }
  }