toJson method

  1. @override
Map<String, dynamic> toJson()

Converts this class object into a JSON value.

Implementation

@override
Map<String, dynamic> toJson() {
  final result = {
    ...additionalFields,
  };

  final keyValuePairs = [
    ("@type", atType),
    ("title", title),
    ("titles", titles),
    ("description", description),
    ("descriptions", descriptions),
    ("const", constant),
    ("default", defaultValue),
    ("enum", enumeration),
    ("format", format),
    ("unit", unit),
    ("type", type),
    ("minimum", minimum),
    ("exclusiveMinimum", exclusiveMinimum),
    ("maximum", maximum),
    ("exclusiveMaximum", exclusiveMaximum),
    ("multipleOf", multipleOf),
    ("items", items),
    ("minItems", minItems),
    ("maxItems", maxItems),
    ("required", required),
    ("minLength", minLength),
    ("maxLength", maxLength),
    ("pattern", pattern),
    ("contentEncoding", contentEncoding),
    ("contentMediaType", contentMediaType),
    ("oneOf", oneOf),
    ("properties", properties),
  ];

  for (final (key, value) in keyValuePairs) {
    final dynamic convertedValue;

    switch (value) {
      case null:
        continue;
      case List<Serializable>():
        convertedValue = value.toJson();
      case Map<String, Serializable>():
        convertedValue = value.toJson();
      default:
        convertedValue = value;
    }

    result[key] = convertedValue;
  }

  final keyValuePairsWithDefault = [
    ("readOnly", readOnly, _defaultReadOnly),
    ("writeOnly", writeOnly, _defaultWriteOnly),
  ];

  for (final (key, value, defaultValue) in keyValuePairsWithDefault) {
    if (value != defaultValue) {
      result[key] = value;
    }
  }

  return result;
}