Skip to content

Commit

Permalink
- JsonConverter (JsonEncoder & JsonDecoder):
Browse files Browse the repository at this point in the history
  - Added `isStandard`.
  • Loading branch information
gmpassos committed Jul 5, 2024
1 parent 97c00e1 commit 2cc547f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- `JsonDecoder`:
- `decode`: optimize resolution for `T`/`type` `dynamic`/`Object` (any type).

- `JsonConverter` (`JsonEncoder` & `JsonDecoder`):
- Added `isStandard`.

## 2.4.1

- `JsonEntityCache`:
Expand Down
37 changes: 34 additions & 3 deletions lib/src/reflection_factory_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ class JsonCodec {

/// JSON decoder integrated with [ReflectionFactory].
abstract class JsonConverter<S, T> implements dart_convert.Converter<S, T> {
/// Returns `true` if this instance's configuration is equivalent to the default configuration.
bool get isStandard;

static bool isPrimitiveType(Type type) {
return type == String ||
type == int ||
Expand Down Expand Up @@ -588,6 +591,19 @@ class _JsonEncoder extends dart_convert.Converter<Object?, String>
this.autoResetEntityCache,
) : entityCache = entityCache ?? JsonEntityCacheSimple();

@override
bool get isStandard =>
identical(this, _JsonEncoder._defaultEncoder) ||
(maskField == null &&
removeField == null &&
!removeNullFields &&
toEncodableProvider == null &&
toEncodable == null &&
!forceDuplicatedEntitiesAsID &&
autoResetEntityCache &&
maskText == '***' &&
entityCache is JsonEntityCacheSimple);

@override
void resetEntityCache() {
entityCache.clearCachedEntities();
Expand Down Expand Up @@ -915,7 +931,8 @@ abstract class JsonDecoder extends JsonConverter<String, Object?> {
JsonEntityCache? entityCache,
bool forceDuplicatedEntitiesAsID = false,
bool autoResetEntityCache = true}) {
if (jsomMapDecoderProvider == null &&
if (jsonValueDecoderProvider == null &&
jsomMapDecoderProvider == null &&
jsomMapDecoder == null &&
jsomMapDecoderAsyncProvider == null &&
jsomMapDecoderAsync == null &&
Expand Down Expand Up @@ -1095,6 +1112,20 @@ class _JsonDecoder extends dart_convert.Converter<String, Object?>
this.autoResetEntityCache,
) : entityCache = entityCache ?? JsonEntityCacheSimple();

@override
bool get isStandard =>
identical(this, _JsonDecoder._defaultDecoder) ||
(jsonValueDecoderProvider == null &&
jsomMapDecoderProvider == null &&
jsomMapDecoder == null &&
jsomMapDecoderAsyncProvider == null &&
jsomMapDecoderAsync == null &&
iterableCaster == null &&
mapCaster == null &&
!forceDuplicatedEntitiesAsID &&
autoResetEntityCache &&
entityCache is JsonEntityCacheSimple);

@override
void resetEntityCache() {
entityCache.clearCachedEntities();
Expand Down Expand Up @@ -1962,7 +1993,7 @@ class _JsonDecoder extends dart_convert.Converter<String, Object?>

var json = dart_convert.json.decode(encodedJson);

if (typeInfo.isAnyType) {
if (typeInfo.isAnyType && isStandard) {
return json as T;
}

Expand Down Expand Up @@ -2007,7 +2038,7 @@ class _JsonDecoder extends dart_convert.Converter<String, Object?>

var json = dart_convert.json.decode(encodedJson);

if (typeInfo.isAnyType) {
if (typeInfo.isAnyType && isStandard) {
return json as T;
}

Expand Down

0 comments on commit 2cc547f

Please sign in to comment.