json property

String get json

Returns a minified JSON representation of the underlying FlexBuffer value.

This method involves materializing the entire object tree, which may be expensive. It is more efficient to work with Reference and access only the needed data. Blob values are represented as base64 encoded string.

Implementation

String get json {
  if (_valueType == ValueType.Bool) {
    return boolValue! ? 'true' : 'false';
  }
  if (_valueType == ValueType.Null) {
    return 'null';
  }
  if (ValueTypeUtils.isNumber(_valueType)) {
    return jsonEncode(numValue);
  }
  if (_valueType == ValueType.String) {
    return jsonEncode(stringValue);
  }
  if (_valueType == ValueType.Blob) {
    return jsonEncode(base64Encode(blobValue!));
  }
  if (ValueTypeUtils.isAVector(_valueType)) {
    final result = StringBuffer();
    result.write('[');
    for (var i = 0; i < length; i++) {
      result.write(this[i].json);
      if (i < length - 1) {
        result.write(',');
      }
    }
    result.write(']');
    return result.toString();
  }
  if (_valueType == ValueType.Map) {
    final result = StringBuffer();
    result.write('{');
    for (var i = 0; i < length; i++) {
      result.write(jsonEncode(_keyForIndex(i)));
      result.write(':');
      result.write(_valueForIndex(i).json);
      if (i < length - 1) {
        result.write(',');
      }
    }
    result.write('}');
    return result.toString();
  }
  throw UnsupportedError(
      'Type: $_valueType is not supported for JSON conversion');
}