toJsonObject method

dynamic toJsonObject(
  1. Value value
)

Implementation

dynamic toJsonObject(Value value) {
  if (value.geoPointValue != null) {
    final geoPointValue = value.geoPointValue!;
    return '$_locationPrefix${geoPointValue.latitude}'
        '/'
        '${geoPointValue.longitude}';
  }

  if (value.mapValue != null) {
    final map = <String, dynamic>{};
    final entries = value.mapValue!.fields?.entries;
    if (entries != null) {
      for (final entry in entries) {
        map[entry.key] = toJsonObject(entry.value);
      }
    }
    return map;
  }

  if (value.arrayValue != null) {
    final list = <dynamic>[];
    final values = value.arrayValue!.values;
    if (values != null) {
      for (final value in values) {
        list.add(toJsonObject(value));
      }
    }
    return list;
  }

  if (value.doubleValue != null) {
    return value.doubleValue;
  }

  if (value.integerValue != null) {
    return int.parse(value.integerValue!);
  }

  if (value.nullValue != null) {
    return null;
  }

  if (value.referenceValue != null) {
    return '$_referencePrefix${value.referenceValue}';
  }

  if (value.stringValue != null) {
    return value.stringValue;
  }

  if (value.timestampValue != null) {
    final datetime =
        DateTime.parse(value.timestampValue!).toUtc().toIso8601String();

    return '$_datetimePrefix$datetime';
  }

  if (value.booleanValue != null) {
    return value.booleanValue;
  }

  if (value.bytesValue != null) {
    return '$_bytesPrefix${value.bytesValue}';
  }

  throw FormatException('Unknown data type: value=$value, '
      'toJson=${value.toJson()}');
}