-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a first JSON macro based on package:json/json.dart. (#68)
* Add a first JSON macro based on package:json/json.dart. * Address review comments. * Dartfmt.
- Loading branch information
1 parent
8903e77
commit 98bd197
Showing
12 changed files
with
474 additions
and
89 deletions.
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.