今天我們來學(xué)習(xí)一下如何使用jaguar_serializer包替裆,這里說明一下墓拜,該包在三端平臺都是可用的(Flutter催式,dart web ,dart vm)奴迅,學(xué)習(xí)過安卓的版确,都知道gson吧户誓,該包類似于gson
1.引入包
#pubspec.yaml
dependencies:
jaguar_serializer: ^2.2.0
dev_dependencies:
build_runner:
jaguar_serializer_cli:
執(zhí)行pub get
命令
導(dǎo)包
import 'package:jaguar_serializer/jaguar_serializer.dart';
2.初始化
- 新增一個實體類
class Person {
String name;
int age;
}
- 添加生成腳本標記
@GenSerializer()
class PersonSerializer extends Serializer<Person> with _$PersonSerializer {
}
- 運行腳本
使用腳本命令pub run build_runner build
- 導(dǎo)入生成代碼
回到person
這個類的文件中梢灭,添加
part 'person.jser.dart';
3.使用
main() async {
Jaguar(port: 1000)
..get('/person', (ctx){
PersonSerializer personSerializer=new PersonSerializer();
Person person=new Person()
..name='rhyme'
..age=18;
//personSerializer.toMap(person)獲取到map對象
return Response.json( personSerializer.toMap(person));
})
..log.onRecord.listen(print)
..serve(logRequests: true);
}
然后我們來請求一下
成功返回了json數(shù)據(jù)
4.使用詳解
-
json與對象相互轉(zhuǎn)換
import 'dart:convert';
main(){
String jsonString='{"name":"rhyme","age":18}';
PersonSerializer personSerializer=new PersonSerializer();
// json to entity
Person person=personSerializer.fromMap(json.decode(jsonString));
print('name:${person.name} ,age: ${person.age}');
//entity to json
String toJson= json.encode( personSerializer.toMap(person));
print(toJson);
}
-
json 與對象列相互轉(zhuǎn)換
main(){
String jsonString='[{"name":"rhyme","age":18},{"name":"ben","age":21}]';
PersonSerializer personSerializer=new PersonSerializer();
// list: json to entity
//因為目前fromList不支持dynamic的值夷家,所以,我們需要吧json.decode出來的List<dynamic>轉(zhuǎn)化為List<Map>敏释,不然會報異常
List<Map> mapList=(json.decode(jsonString) as List<dynamic>).map((dy)=>dy as Map).toList();
List<Person> persons=personSerializer.fromList(mapList);
for(Person p in persons){
print('name:${p.name} ,age: ${p.age}');
}
//list: entity to json
String fromJson=json.encode(personSerializer.toList(persons));
print(fromJson);
}
-
對象內(nèi)嵌對象與json相互轉(zhuǎn)換
在對象內(nèi)添加一個對象
class Person {
String name;
int age;
//new
Info info;
//new
}
//new
class Info{
String address;
String phoneNumber;
}
//edit
@GenSerializer( serializers: [ InfoSerializer])
//edit
class PersonSerializer extends Serializer<Person> with _$PersonSerializer {
}
//new
@GenSerializer()
class InfoSerializer extends Serializer<Info> with _$InfoSerializer {
}
//new
運行命令pub run build_runner build
轉(zhuǎn)換跟上面相同
String jsonString='{"name":"rhyme","age":18,"info":{"address":"廣州","phoneNumber":"159xxxxxxxx"}}';
PersonSerializer personSerializer=new PersonSerializer();
// json to entity
Person person=personSerializer.fromMap(json.decode(jsonString));
print('name:${person.name} ,age: ${person.age}');
//entity to json
String toJson= json.encode(personSerializer.toMap(person));
print(toJson);
-
對象內(nèi)嵌對象列
將上面的Info
改為List<Info>
重新運行命令pub run build_runner build
轉(zhuǎn)換跟上面一樣
String jsonString='[{"name":"rhyme","age":18,"info":[{"address":"廣州","phoneNumber":"159xxxxxxxx"},{"address":"北京","phoneNumber":"158xxxxxxxx"}]},{"name":"ben","age":21}]';
PersonSerializer personSerializer=new PersonSerializer();
// list: json to entity
//因為目前fromList不支持dynamic的值库快,所以,我們需要吧json.decode出來的List<dynamic>轉(zhuǎn)化為List<Map>钥顽,不然會報異常
List<Map> mapList=(json.decode(jsonString) as List<dynamic>).map((dy)=>dy as Map).toList();
List<Person> persons=personSerializer.fromList(mapList);
for(Person p in persons){
print('name:${p.name} ,age: ${p.age}');
}
//list: entity to json
String fromJson=json.encode(personSerializer.toList(persons));
print(fromJson);
-
對象內(nèi)嵌多個對象
class Person {
String name;
int age;
List<Info> info;
//new
Avatar avatar;
//new
}
class Info{
String address;
String phoneNumber;
}
//new
class Avatar{
String url;
String path;
}
//new
//edit
@GenSerializer( serializers: [ InfoSerializer,AvatarSerializer])
//edit
class PersonSerializer extends Serializer<Person> with _$PersonSerializer {
}
@GenSerializer()
class InfoSerializer extends Serializer<Info> with _$InfoSerializer {
}
//new
@GenSerializer()
class AvatarSerializer extends Serializer<Avatar> with _$AvatarSerializer {
}
//new
然后重新運行命令pub run build_runner build
使用也是跟上面相同
String jsonString='[{"name":"rhyme","age":18,"avatar":{"url":"http://www.baidu.com","path":"baidu"},"info":[{"address":"廣州","phoneNumber":"159xxxxxxxx"},{"address":"北京","phoneNumber":"158xxxxxxxx"}]},{"name":"ben","age":21}]';
PersonSerializer personSerializer=new PersonSerializer();
// list: json to entity
//因為目前fromList不支持dynamic的值缺谴,所以,我們需要吧json.decode出來的List<dynamic>轉(zhuǎn)化為List<Map>耳鸯,不然會報異常
List<Map> mapList=(json.decode(jsonString) as List<dynamic>).map((dy)=>dy as Map).toList();
List<Person> persons=personSerializer.fromList(mapList);
for(Person p in persons){
print('name:${p.name} ,age: ${p.age}');
}
//list: entity to json
String fromJson=json.encode(personSerializer.toList(persons));
print(fromJson);
-
別名
有時候湿蛔,我們的實體類成員變量名跟json
數(shù)據(jù)中對應(yīng)的字段不相同,可以使用@Alias('')
對需要的字段進行標記县爬,里面的值為json
對應(yīng)的字段
class Person {
@Alias('Name')
String name;
int age;
List<Info> info;
Avatar avatar;
}
要使上面生效阳啥,需要重新運行命令pub run build_runner build
-
Build命令
我們可以發(fā)現(xiàn),當我每次做一個修改财喳,都需要執(zhí)行pub run build_runner build
命令察迟,這樣是非常容易導(dǎo)致疏忽的,jaguar_serializer
已經(jīng)為我們考慮到了耳高,
我們可以使用命令
pub run build_runner watch
當該命令運行后扎瓶,我們的每次點擊保存,都會重新激活一次腳本泌枪,如下圖
如果你是Flutter開發(fā)概荷,可以使用下面的build命令
flutter packages pub run build_runner build
Flutter的監(jiān)聽命令
flutter packages pub run build_runner watch
ok,今天就到這里了碌燕,我們明天見!
如果想繼續(xù)學(xué)習(xí)DartVM服務(wù)器開發(fā)误证,請關(guān)注我,學(xué)習(xí)更多騷操作修壕!