toJson function

String toJson (
  1. Object value
)

Implementation

String toJson(Object value) {
  return jsonEncode(value, toEncodable: (nonEncodable) {
    final toMap = <String, dynamic>{};

    final instanceMirror = reflect(nonEncodable);
    instanceMirror.type.instanceMembers.forEach((symbol, mirror) {
      if (mirror.isGetter &&
          symbol != _hashCodeSymbol &&
          symbol != _runtimeTypeSymbol) {
        final annotation = _getAnnotation(
            instanceMirror.type.declarations[symbol], SerializedName);
        String key;
        if (annotation is SerializedName) {
          key = annotation.value;
        } else {
          key = _nameOf(symbol);
        }
        toMap[key] = instanceMirror
            .getField(symbol)
            .reflectee;
      }
    });
    return toMap;
  });
}