This repository has been archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Add a first JSON macro based on package:json/json.dart. #68
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
part of 'package:foo/json_codable.dart'; | ||
|
||
import 'package:foo/json_codable.dart' as prefix0; | ||
import 'dart:core' as prefix1; | ||
|
||
augment class A { | ||
external prefix0.A.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json); | ||
external prefix1.Map<prefix1.String, prefix1.Object?> toJson(); | ||
|
||
augment prefix0.A.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json) : | ||
boolField = json[r'boolField'] as prefix1.bool, | ||
nullableBoolField = json[r'nullableBoolField'] as prefix1.bool?, | ||
stringField = json[r'stringField'] as prefix1.String, | ||
nullableStringField = json[r'nullableStringField'] as prefix1.String?, | ||
intField = json[r'intField'] as prefix1.int, | ||
nullableIntField = json[r'nullableIntField'] as prefix1.int?, | ||
doubleField = json[r'doubleField'] as prefix1.double, | ||
nullableDoubleField = json[r'nullableDoubleField'] as prefix1.double?, | ||
numField = json[r'numField'] as prefix1.num, | ||
nullableNumField = json[r'nullableNumField'] as prefix1.num?, | ||
listOfSerializableField = json[r'listOfSerializableField'] == null ? null : [for (final item in json[r'listOfSerializableField'] as prefix1.List<prefix1.Object?>) item == null ? null : prefix0.C.fromJson(item as prefix1.Map<prefix1.String, prefix1.Object?>], | ||
nullableListOfSerializableField = [for (final item in json[r'nullableListOfSerializableField'] as prefix1.List<prefix1.Object?>) item == null ? null : prefix0.C.fromJson(item as prefix1.Map<prefix1.String, prefix1.Object?>], | ||
setOfSerializableField = json[r'setOfSerializableField'] == null ? null : {for (final item in json[r'setOfSerializableField'] as prefix1.Set<prefix1.Object?>) item == null ? null : prefix0.C.fromJson(item as prefix1.Map<prefix1.String, prefix1.Object?>}, | ||
nullableSetOfSerializableField = {for (final item in json[r'nullableSetOfSerializableField'] as prefix1.Set<prefix1.Object?>) item == null ? null : prefix0.C.fromJson(item as prefix1.Map<prefix1.String, prefix1.Object?>}, | ||
mapOfSerializableField = json[r'mapOfSerializableField'] == null ? null : {for (final (:key, :value) in json[r'mapOfSerializableField'] as prefix1.Map<prefix1.String, prefix1.Object?>) key: value == null ? null : prefix0.C.fromJson(value as prefix1.Map<prefix1.String, prefix1.Object?>}, | ||
nullableMapOfSerializableField = {for (final (:key, :value) in json[r'nullableMapOfSerializableField'] as prefix1.Map<prefix1.String, prefix1.Object?>) key: value == null ? null : prefix0.C.fromJson(value as prefix1.Map<prefix1.String, prefix1.Object?>}; | ||
|
||
prefix1.Map<prefix1.String, prefix1.Object?> toJson() { | ||
final json = prefix1.Map<prefix1.String, prefix1.Object?>{}; | ||
json[r'boolField'] = boolField; | ||
json[r'nullableBoolField'] = nullableBoolField; | ||
json[r'stringField'] = stringField; | ||
json[r'nullableStringField'] = nullableStringField; | ||
json[r'intField'] = intField; | ||
json[r'nullableIntField'] = nullableIntField; | ||
json[r'doubleField'] = doubleField; | ||
json[r'nullableDoubleField'] = nullableDoubleField; | ||
json[r'numField'] = numField; | ||
json[r'nullableNumField'] = nullableNumField; | ||
json[r'listOfSerializableField'] = listOfSerializableField == null ? null : [for (final item in listOfSerializableField) item == null ? null : item.toJson()]; | ||
json[r'nullableListOfSerializableField'] = [for (final item in nullableListOfSerializableField) item == null ? null : item.toJson()]; | ||
json[r'setOfSerializableField'] = setOfSerializableField == null ? null : [for (final item in setOfSerializableField) item == null ? null : item.toJson()]; | ||
json[r'nullableSetOfSerializableField'] = [for (final item in nullableSetOfSerializableField) item == null ? null : item.toJson()]; | ||
json[r'mapOfSerializableField'] = mapOfSerializableField == null ? null : {for (final (:key, :value) in mapOfSerializableField.entries) key: value == null ? null : value.toJson()}; | ||
json[r'nullableMapOfSerializableField'] = {for (final (:key, :value) in nullableMapOfSerializableField.entries) key: value == null ? null : value.toJson()}; | ||
|
||
return json; | ||
}; | ||
|
||
} | ||
augment class C { | ||
external prefix0.C.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json); | ||
external prefix1.Map<prefix1.String, prefix1.Object?> toJson(); | ||
|
||
augment prefix0.C.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json) : | ||
boolField = json[r'boolField'] as prefix1.bool; | ||
|
||
prefix1.Map<prefix1.String, prefix1.Object?> toJson() { | ||
final json = prefix1.Map<prefix1.String, prefix1.Object?>{}; | ||
json[r'boolField'] = boolField; | ||
|
||
return json; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
import 'package:_test_macros/json_codable.dart'; | ||
|
||
@JsonCodable() | ||
class A { | ||
final bool boolField; | ||
|
||
final bool? nullableBoolField; | ||
|
||
final String stringField; | ||
|
||
final String? nullableStringField; | ||
|
||
final int intField; | ||
|
||
final int? nullableIntField; | ||
|
||
final double doubleField; | ||
|
||
final double? nullableDoubleField; | ||
|
||
final num numField; | ||
|
||
final num? nullableNumField; | ||
|
||
final List<C> listOfSerializableField; | ||
|
||
final List<C>? nullableListOfSerializableField; | ||
|
||
final Set<C> setOfSerializableField; | ||
|
||
final Set<C>? nullableSetOfSerializableField; | ||
|
||
final Map<String, C> mapOfSerializableField; | ||
|
||
final Map<String, C>? nullableMapOfSerializableField; | ||
} | ||
|
||
@JsonCodable() | ||
class C { | ||
final bool boolField; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This model for
properties
I think is going to fall apart here. We really don't want to be modeling all the possible properties for all the possible things with a single type?For instance,
returnType
is really not what the type of a field is, there is a return type on the getter sure, but not the field (it just has a type).When you are using this
Properties
class, you are also going to get terrible auto complete, with all possible things that might exist on all possible declaration kinds?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely: as we discussed we probably want to end up with something flat but probably still giving basic "available properties" at each point.
Added a TODO in
definitions.dart
// TODO(davidmorgan): make
Member
a union.