objectToJson function

String objectToJson(
  1. Object? obj
)

Implementation

String objectToJson(Object? obj) {
  if (obj is String) {
    if (isEmpty(obj)) {
      return "\"\"";
    } else {
      return stringFormat(obj);
    }
  } else if (obj is num) {
    return obj.toString();
  } else if (obj is VoltronArray) {
    var sb = StringBuffer();
    sb.write("[");
    var length = obj.size();
    for (var i = 0; i < length; i++) {
      sb.write(objectToJson(obj.get<Object>(i)));
      if (i < length - 1) {
        sb.write(",");
      }
    }
    sb.write("]");
    return sb.toString();
  } else if (obj is VoltronMap) {
    var sb = StringBuffer();
    sb.write("{");
    dynamic keys = obj.keySet();
    var i = 0;
    for (String key in keys) {
      sb.write("\"$key\":");
      sb.write(objectToJson(obj.get(key)));
      if (i < obj.size() - 1) {
        sb.write(",");
      }
      i++;
    }
    sb.write("}");
    return sb.toString();
  } else if (obj is bool) {
    return obj.toString();
  }
  return "\"\"";
}