Skip to content

Commit

Permalink
Release 2.2.14
Browse files Browse the repository at this point in the history
  • Loading branch information
k-paxian committed Sep 29, 2024
1 parent 90c1721 commit c5ae571
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
5 changes: 5 additions & 0 deletions mapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.2.14

* bump dependencies to latest
* Fix #227 Deserializing error: Unable to instantiate class. Improved error message

## 2.2.13

* bump dependencies to latest
Expand Down
3 changes: 1 addition & 2 deletions mapper/example/example.mapper.g.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file has been generated by the dart_json_mapper v2.2.10
// This file has been generated by the dart_json_mapper v2.2.14
// https://github.com/k-paxian/dart-json-mapper
// @dart = 2.12
import 'example.dart' as x0 show Car, Person, Color;
Expand All @@ -7,7 +7,6 @@ import 'package:dart_json_mapper/src/identifier_casing.dart' as x2 show CaseStyl
import 'package:dart_json_mapper/src/model/index.dart' as x1 show ConversionDirection;
// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.

import 'dart:core';
import 'example.dart' as prefix1;
import 'package:dart_json_mapper/src/model/annotations.dart' as prefix0;
Expand Down
25 changes: 16 additions & 9 deletions mapper/lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class _MissingTypeForDeserializationErrorImpl extends JsonMapperError

abstract class CannotCreateInstanceError extends JsonMapperError {
factory CannotCreateInstanceError(
TypeError typeError,
ClassInfo classInfo,
Iterable<String> positionalNullArguments,
Map<Symbol, dynamic> namedNullArguments) = _CannotCreateInstanceErrorImpl;
Expand All @@ -132,25 +133,31 @@ abstract class CannotCreateInstanceError extends JsonMapperError {
class _CannotCreateInstanceErrorImpl extends JsonMapperError
implements CannotCreateInstanceError {
final ClassInfo _classInfo;
final TypeError _typeError;
final Iterable<String> _positionalNullArguments;
final Map<Symbol, dynamic> _namedNullArguments;

_CannotCreateInstanceErrorImpl(
TypeError typeError,
ClassInfo classInfo,
Iterable<String> positionalNullArguments,
Map<Symbol, dynamic> namedNullArguments)
: _classInfo = classInfo,
_typeError = typeError,
_positionalNullArguments = positionalNullArguments,
_namedNullArguments = namedNullArguments;

@override
String toString() => [
"Unable to instantiate class '${_classInfo.classMirror.simpleName}'",
_positionalNullArguments.isEmpty
? null
: ' with null positional arguments [${_positionalNullArguments.join(', ')}]',
_namedNullArguments.keys.isEmpty
? null
: ' with null named arguments [${_namedNullArguments.keys.join(', ')}]'
].where((element) => element != null).join('\n');
String toString() =>
_typeError.toString().startsWith("type 'Null' is not a subtype of type")
? [
"Unable to instantiate class '${_classInfo.classMirror.simpleName}'",
_positionalNullArguments.isEmpty
? null
: ' with null positional arguments [${_positionalNullArguments.join(', ')}]',
_namedNullArguments.keys.isEmpty
? null
: ' with null named arguments [${_namedNullArguments.keys.join(', ')}]'
].where((element) => element != null).join('\n')
: _typeError.toString();
}
4 changes: 2 additions & 2 deletions mapper/lib/src/mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -979,14 +979,14 @@ class JsonMapper {
.constructorName,
positionalArguments,
namedArguments));
} on TypeError {
} on TypeError catch (typeError) {
final positionalNullArguments = positionalArgumentNames.where((element) =>
positionalArguments[positionalArgumentNames.indexOf(element)] ==
null);
final namedNullArguments = Map<Symbol, dynamic>.from(namedArguments);
namedNullArguments.removeWhere((key, value) => value != null);
throw CannotCreateInstanceError(
classInfo, positionalNullArguments, namedNullArguments);
typeError, classInfo, positionalNullArguments, namedNullArguments);
}

final im = _safeGetInstanceMirror(objectInstance)!;
Expand Down
8 changes: 4 additions & 4 deletions mapper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name: dart_json_mapper
version: 2.2.13
version: 2.2.14
description: >
This package allows programmers to annotate Dart objects in order to
serialize / deserialize them from / to JSON.
homepage: https://github.com/k-paxian/dart-json-mapper
environment:
sdk: '>=2.17.0 <4.0.0'
dependencies:
analyzer: ^6.5.0
analyzer: ^6.7.0
build: ^2.4.1
build_config: ^1.1.1
intl: ^0.19.0
meta: ^1.15.0
meta: ^1.16.0
path: ^1.8.3
reflectable: ^4.0.9
reflectable: ^4.0.10
collection: ^1.18.0
pubspec_parse: ^1.3.0
dev_dependencies:
Expand Down
1 change: 1 addition & 0 deletions perf-test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
unit_testing:
starlink:
dev_dependencies:
reflectable:
json_serializable:
built_value_generator:
lints:
Expand Down
3 changes: 1 addition & 2 deletions perf-test/test/unit/_test.mapper.g.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file has been generated by the dart_json_mapper v2.2.10
// This file has been generated by the dart_json_mapper v2.2.14
// https://github.com/k-paxian/dart-json-mapper
// @dart = 2.12
import 'package:dart_json_mapper/dart_json_mapper.dart' show JsonMapper, JsonMapperAdapter, SerializationOptions, DeserializationOptions, typeOf;
Expand All @@ -20,7 +20,6 @@ import 'test.tuple.dart' as x14 show Tuple, ConcreteClass;
import 'test.value.decorators.dart' as x15 show NavbarTab, NavbarConfig, NoticeItem, NoticeList, TestChain, Customer, ServiceOrderItemModel, ServiceOrderModel, Item, ListOfLists, ListOfListsWithConstructor;
// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.

import 'dart:core';
import 'dart:typed_data' as prefix24;
import 'package:dart_json_mapper/src/identifier_casing.dart' as prefix22;
Expand Down
17 changes: 16 additions & 1 deletion perf-test/test/unit/test.errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void testErrorHandling() {
expect(catchError(() => JsonMapper.serialize(car)), null);
});

test('Unable to create instance of class error handling', () {
test('Unable to create instance of class error handling - null arguments', () {
// given
final json = '{"name":"James"}';

Expand All @@ -166,6 +166,21 @@ void testErrorHandling() {
expect(error, TypeMatcher<CannotCreateInstanceError>());
});

test('Unable to create instance of class error handling', () {
// given
final json = '{"name":["James"],"age":{}}';

// when
final error =
catchError(() => JsonMapper.deserialize<PartialPerson>(json));

// then
expect(
error.toString(),
'type \'_Map<String, dynamic>\' is not a subtype of type \'int\'');
expect(error, TypeMatcher<CannotCreateInstanceError>());
});

test('Allow using same object same level during serialization', () {
final device = Device();
final us = UserSettings([device, device]);
Expand Down

0 comments on commit c5ae571

Please sign in to comment.