trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.08k stars 251 forks source link

如何处理自定义解析json #172

Closed JasonHezz closed 4 years ago

JasonHezz commented 4 years ago

我使用dart-json-mapper做json解析 避免重复代码 在生成代码中我想替换如下代码

final value = T.fromJson(_result.data) ;

final value = JsonMapper.deserialize<T>(_result.data);

应该如何处理比较好

trevorwang commented 4 years ago

目前还没有很好的插件机制来实现 这个需要修改generator的代码

https://github.com/trevorwang/retrofit.dart/blob/master/generator/lib/src/generator.dart 搜索 fromJ'son

欢迎PR

JasonHezz commented 4 years ago

我的方案是在RestApi 增加String parser

parser 为JsonSerializable则是生成旧模式

parse为DartJsonMapper则生成JsonMapper.deserialize

默认值为JsonSerializable

不知道这个方案可行吗 如果可行 我会尝试在周末提交PR

trevorwang commented 4 years ago

OK. parser考虑使用枚举类型

On Apr 17, 2020, at 10:55 AM, JasonHezz notifications@github.com wrote:

我的方案是在RestApi 增加String parser

parser 为JsonSerializable则是生成旧模式

parse为DartJsonMapper则生成JsonMapper.deserialize

默认值为JsonSerializable

不知道这个方案可行吗 如果可行 我会尝试在周末提交PRc

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/trevorwang/retrofit.dart/issues/172#issuecomment-615009947, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA5Y3V6RVSDMV2GLJH47ULRM7AKTANCNFSM4MJDZKBQ.

vanlooverenkoen commented 4 years ago

Can someone explain me what this ticket exactly does? Or is there documentation available

JasonHezz commented 4 years ago

@vanlooverenkoen I'm sorry for that .

This is a discussion about how to custom deserialize json. I use dart json mapper and I prefer generate code

final value = JsonMapper.deserialize<T>(_result.data);

rather than

final value = T.fromJson(_result.data) ;

That's the PR address. If you want to use dart json mapper too. Try code blow

@RestApi(
  baseUrl: 'http://xx/',
  parser: Parser.DartJsonMapper, 
)
abstract class RestClient {
}

But remember this only support deserialize , and @Body is not working because I have few time now. Hopefully I can finish that in next weeks.