Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions json_serializable/lib/src/decode_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ mixin DecodeHelper implements HelperCore {
if (config.checked) {
final classLiteral = escapeDartString(element.name!);

final helperPrefix = jsonAnnotationHelperPrefix(element.library);
final sectionBuffer = StringBuffer()
..write('''
\$checkedCreate(
${helperPrefix}\$checkedCreate(
$classLiteral,
json,
(\$checkedConvert) {\n''')
Expand Down Expand Up @@ -202,7 +203,8 @@ mixin DecodeHelper implements HelperCore {
}

if (args.isNotEmpty) {
yield '\$checkKeys(json, ${args.map((e) => '$e, ').join()});\n';
final helperPrefix = jsonAnnotationHelperPrefix(element.library);
yield '${helperPrefix}\$checkKeys(json, ${args.map((e) => '$e, ').join()});\n';
}
}

Expand Down
10 changes: 7 additions & 3 deletions json_serializable/lib/src/type_helpers/enum_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:source_helper/source_helper.dart';
import '../enum_utils.dart';
import '../json_key_utils.dart';
import '../type_helper.dart';
import '../utils.dart';

final simpleExpression = RegExp('^[a-zA-Z_]+\$');

Expand Down Expand Up @@ -62,11 +63,14 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
);
}

String functionName;
final helperPrefix = jsonAnnotationHelperPrefix(
context.classElement.library,
);
final String functionName;
if (targetType.isNullableType || defaultProvided) {
functionName = r'$enumDecodeNullable';
functionName = '${helperPrefix}\$enumDecodeNullable';
} else {
functionName = r'$enumDecode';
functionName = '${helperPrefix}\$enumDecode';
}

context.addMember(memberContent);
Expand Down
23 changes: 23 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,26 @@ extension ExecutableElementExtension on ExecutableElement {
const jsonSerializableChecker = TypeChecker.fromUrl(
'package:json_annotation/src/json_serializable.dart#JsonSerializable',
);

/// Returns the import prefix used for `package:json_annotation`, including a
/// trailing `.`, or an empty string when the import is unprefixed.
///
/// Generated references to helpers like `$checkedCreate` and `$enumDecode` must
/// use this prefix so they resolve when the annotation library is imported with
/// a prefix.
String jsonAnnotationHelperPrefix(LibraryElement library) {
for (final fragment in library.fragments) {
for (final import in fragment.libraryImports) {
final uri = import.importedLibrary?.uri;
if (uri == null ||
uri.scheme != 'package' ||
uri.pathSegments.isEmpty ||
uri.pathSegments.first != 'json_annotation') {
continue;
}
final prefix = import.prefix?.name;
return prefix == null ? '' : '$prefix.';
}
}
return '';
}
12 changes: 12 additions & 0 deletions json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../test_utils.dart';
import 'converter_examples.dart';
import 'create_per_field_to_json_example.dart';
import 'field_map_example.dart';
import 'json_annotation_prefix_example.dart';
import 'json_enum_example.dart';
import 'json_keys_example.dart' as js_keys;
import 'json_test_common.dart' show Category, Platform, StatusCode;
Expand Down Expand Up @@ -492,4 +493,15 @@ void main() {
)..remove(r'$schema');
expect(nestedSchemaFromExample, standaloneSchema);
});

test('json_annotation import prefix helpers', () {
final checked = CheckedPrefixModel.fromJson({'field1': 'a', 'field2': 'b'});
expect(checked.field1, 'a');
expect(checked.field2, 'b');
expect(checked.toJson(), {'field1': 'a', 'field2': 'b'});

final enumModel = EnumPrefixModel.fromJson({'gender': 'female'});
expect(enumModel.gender, PrefixModelGender.female);
expect(enumModel.toJson(), {'gender': 'female'});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:json_annotation/json_annotation.dart' as ja;

part 'json_annotation_prefix_example.g.dart';

@ja.JsonSerializable(checked: true)
class CheckedPrefixModel {
final String field1;
final String field2;

CheckedPrefixModel({required this.field1, required this.field2});

factory CheckedPrefixModel.fromJson(Map<String, dynamic> json) =>
_$CheckedPrefixModelFromJson(json);

Map<String, dynamic> toJson() => _$CheckedPrefixModelToJson(this);
}

@ja.JsonSerializable()
class EnumPrefixModel {
@ja.JsonKey(required: true, disallowNullValue: true)
final PrefixModelGender gender;

EnumPrefixModel({required this.gender});

factory EnumPrefixModel.fromJson(Map<String, dynamic> json) =>
_$EnumPrefixModelFromJson(json);

Map<String, dynamic> toJson() => _$EnumPrefixModelToJson(this);
}

enum PrefixModelGender { male, female, other }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ Future<void> main() async {
'UnsupportedClass',
},
);

final jsonAnnotationPrefixTestReader =
await initializeLibraryReaderForDirectory(
p.join('test', 'src'),
'_json_annotation_prefix_test_input.dart',
);

testAnnotatedElements(
jsonAnnotationPrefixTestReader,
JsonSerializableGenerator(),
expectedAnnotatedTests: {'CheckedWithPrefix', 'EnumWithPrefix'},
);
}

const _expectedAnnotatedTests = {
Expand Down
52 changes: 52 additions & 0 deletions json_serializable/test/src/_json_annotation_prefix_test_input.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=3.8

import 'package:json_annotation/json_annotation.dart' as ja;
import 'package:source_gen_test/annotations.dart';

@ShouldGenerate(r'''
CheckedWithPrefix _$CheckedWithPrefixFromJson(Map<String, dynamic> json) =>
ja.$checkedCreate('CheckedWithPrefix', json, ($checkedConvert) {
final val = CheckedWithPrefix(
$checkedConvert('field1', (v) => v as String),
$checkedConvert('field2', (v) => v as String),
);
return val;
});
''')
@ja.JsonSerializable(checked: true, createToJson: false)
class CheckedWithPrefix {
final String field1;
final String field2;

CheckedWithPrefix(this.field1, this.field2);
}

@ShouldGenerate(r'''
EnumWithPrefix _$EnumWithPrefixFromJson(Map<String, dynamic> json) {
ja.$checkKeys(
json,
requiredKeys: const ['gender'],
disallowNullValues: const ['gender'],
);
return EnumWithPrefix(ja.$enumDecode(_$PrefixGenderEnumMap, json['gender']));
}

const _$PrefixGenderEnumMap = {
PrefixGender.male: 'male',
PrefixGender.female: 'female',
PrefixGender.other: 'other',
};
''')
@ja.JsonSerializable(createToJson: false)
class EnumWithPrefix {
@ja.JsonKey(required: true, disallowNullValue: true)
final PrefixGender gender;

EnumWithPrefix(this.gender);
}

enum PrefixGender { male, female, other }