toJsonExpression method

String toJsonExpression(
  1. String key,
  2. bool privateField
)

Implementation

String toJsonExpression(String key, bool privateField) {
  final fieldKey =
      fixFieldName(key, typeDef: this, privateField: privateField);
  final thisKey = 'this.$fieldKey';
  if (isPrimitive) {
    return "data['$key'] = $thisKey;";
  } else if (name == 'List') {
    // class list
    return """if ($thisKey != null) {
    data['$key'] = $thisKey!.map((v) => ${_buildToJsonClass('v', false)}).toList();
  }""";
  } else {
    // class
    return """if ($thisKey != null) {
    data['$key'] = ${_buildToJsonClass(thisKey)};
  }""";
  }
}