google / google/built_value.dart

Is it possible to distinguish between absent (no value) and null when serializing?

Open
#1,123 13 comments 3 reactions 1 assignee Claimed by @davidmorgan View on GitHub
question
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

Let's say I have a SimpleValue class with a nullableString:

```dart
abstract class SimpleValue implements Built {
@BuiltValueSerializer(serializeNulls: true)
static Serializer get serializer => _$simpleValueSerializer;

String? get nullableString;

factory SimpleValue([void Function(SimpleValueBuilder) updates]) = _$SimpleValue;
SimpleValue._();
}
```

I would like to be able to have this behaviour when serializing to JSON:

```dart
final noValue = SimpleValue();
print(serializers.toJson(SimpleValue.serializer, value)); // should print {}

final nullValue = SimpleValue((value) => value.nullableString = null);
print(serializers.toJson(SimpleValue.serializer, nullValue)); // should print { "nullableString": null }
```

Is it possible?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.