toJson method

Map<String, dynamic> toJson()

Produces a Map that can be serialized to JSON.

Implementation

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

  forEach((key, value) {
    if (value == null) return;
    switch (key) {
      case SentryDevice.type:
        final deviceMap = device?.toJson();
        if (deviceMap?.isNotEmpty ?? false) {
          json[SentryDevice.type] = deviceMap;
        }
        break;
      case SentryOperatingSystem.type:
        final osMap = operatingSystem?.toJson();
        if (osMap?.isNotEmpty ?? false) {
          json[SentryOperatingSystem.type] = osMap;
        }
        break;

      case SentryApp.type:
        final appMap = app?.toJson();
        if (appMap?.isNotEmpty ?? false) {
          json[SentryApp.type] = appMap;
        }
        break;

      case SentryBrowser.type:
        final browserMap = browser?.toJson();
        if (browserMap?.isNotEmpty ?? false) {
          json[SentryBrowser.type] = browserMap;
        }
        break;

      case SentryGpu.type:
        final gpuMap = gpu?.toJson();
        if (gpuMap?.isNotEmpty ?? false) {
          json[SentryGpu.type] = gpuMap;
        }
        break;

      case SentryRuntime.listType:
        if (runtimes.length == 1) {
          final runtime = runtimes[0];
          final runtimeMap = runtime.toJson();
          if (runtimeMap.isNotEmpty) {
            final key = runtime.key ?? SentryRuntime.type;

            json[key] = runtimeMap;
          }
        } else if (runtimes.length > 1) {
          for (final runtime in runtimes) {
            final runtimeMap = runtime.toJson();
            if (runtimeMap.isNotEmpty) {
              var key = runtime.key ?? runtime.name!.toLowerCase();

              if (json.containsKey(key)) {
                var k = 0;
                while (json.containsKey(key)) {
                  key = '$key$k';
                  k++;
                }
              }
              json[key] = runtimeMap
                ..addAll(<String, String>{'type': SentryRuntime.type});
            }
          }
        }

        break;

      default:
        if (value != null) {
          json[key] = value;
        }
    }
  });

  return json;
}