toJsonElement property
Element?
get
toJsonElement
Implementation
Element? get toJsonElement {
final element = this;
final methods = switch (element) {
ClassElement(:final methods) => methods,
EnumElement(:final methods) => methods,
_ => <MethodElement>[],
};
// `0` is the plain `toJson()` shape. When `element` declares type
// parameters, `typeParameters.length` is also accepted -- the
// `genericArgumentFactories`-style shape where one `Object Function(T)`
// closure is required per type parameter, in declaration order.
final typeParamCount = switch (element) {
ClassElement(:final typeParameters) => typeParameters.length,
_ => 0,
};
final arities = {0, if (typeParamCount > 0) typeParamCount};
for (final method in methods) {
if (method.name != 'toJson') continue;
final requiredPositionalCount = method.formalParameters
.where((p) => p.isRequiredPositional)
.length;
if (!arities.contains(requiredPositionalCount)) continue;
return method;
}
return null;
}