toJson method

Map<String, dynamic> toJson()

Serializes this event to JSON.

Implementation

Map<String, dynamic> toJson() {
  final json = <String, dynamic>{};

  json['event_id'] = eventId.toString();

  json['timestamp'] = formatDateAsIso8601WithMillisPrecision(timestamp);

  if (platform != null) {
    json['platform'] = platform;
  }

  if (logger != null) {
    json['logger'] = logger;
  }

  if (serverName != null) {
    json['server_name'] = serverName;
  }

  if (release != null) {
    json['release'] = release;
  }

  if (dist != null) {
    json['dist'] = dist;
  }

  if (environment != null) {
    json['environment'] = environment;
  }

  if (modules != null && modules!.isNotEmpty) {
    json['modules'] = modules;
  }

  Map<String, dynamic> messageMap;
  if (message != null && (messageMap = message!.toJson()).isNotEmpty) {
    json['message'] = messageMap;
  }

  if (transaction != null) {
    json['transaction'] = transaction;
  }

  final exceptionMap = exception?.toJson();
  final stackTraceMap = stackTrace?.toJson();
  if (exceptionMap?.isNotEmpty ?? false) {
    json['exception'] = {
      'values': [exceptionMap].toList(growable: false)
    };
  } else if (stackTraceMap?.isNotEmpty ?? false) {
    json['threads'] = {
      'values': [
        {
          'id': 0,
          'stacktrace': stackTraceMap,
          'crashed': true,
          'name': 'Current Isolate',
        }
      ]
    };
  }

  if (level != null) {
    json['level'] = level!.name;
  }

  if (culprit != null) {
    json['culprit'] = culprit;
  }

  if (tags?.isNotEmpty ?? false) {
    json['tags'] = tags;
  }

  if (extra?.isNotEmpty ?? false) {
    json['extra'] = extra;
  }

  final contextsMap = contexts.toJson();
  if (contextsMap.isNotEmpty) {
    json['contexts'] = contextsMap;
  }

  final userMap = user?.toJson();
  if (userMap?.isNotEmpty ?? false) {
    json['user'] = userMap;
  }

  if (fingerprint?.isNotEmpty ?? false) {
    json['fingerprint'] = fingerprint;
  }

  if (breadcrumbs?.isNotEmpty ?? false) {
    json['breadcrumbs'] =
        breadcrumbs?.map((b) => b.toJson()).toList(growable: false);
  }

  final sdkMap = sdk?.toJson();
  if (sdkMap?.isNotEmpty ?? false) {
    json['sdk'] = sdkMap;
  }

  final requestMap = request?.toJson();
  if (requestMap?.isNotEmpty ?? false) {
    json['request'] = requestMap;
  }

  final debugMetaMap = debugMeta?.toJson();
  if (debugMetaMap?.isNotEmpty ?? false) {
    json['debug_meta'] = debugMetaMap;
  }

  return json;
}