英文:
Dynamic Type Casting in Dart/Flutter
问题
以下是翻译好的部分:
我正在编写一个库,用于在Dart/Flutter中动态序列化/反序列化任何对象(与Python的Pydantic类似的想法)。然而,我发现实现最后一个组件——动态类型转换——几乎不可能。这是为了将从对象中使用反射检索的类型从JSON转换为列表<dynamic>到列表<string>(或类似的类型)。以下是期望的实现(尽我所知,这在Dart中不可能)。
Map<String, Type> dynamicTypes = {"key": int };
// 常规转换将是 "1" 作为 int
int value = "1" as dynamicTypes["key"];
是否有某种解决方法使这个实现成为可能?或者我是否已经陷入了死胡同(因此不存在其他动态序列化/反序列化包)?
英文:
I am in the middle of writing a library to dynamically serialise/deserialise any object in Dart/Flutter (Similar in idea to Pydantic for Python). However, I am finding it impossible to implement the last component, dynamic type casting. This is required in order convert types from JSON, such as List<dynamic> to List<string> (or similar). The types are retrieved from objects using reflection.
The below is the desired implementation (though as far as I understand this is not possible in Dart).
Map<String, Type> dynamicTypes = {"key": int };
// Regular casting would be "1" as int
int value = "1" as dynamicTypes["key"];
Is there some workaround which makes this possible to implement? Or have I reached a dead end with this (hence no other dynamic serialisation/deserialisation package already exists).
答案1
得分: 1
Conducting more research into this issue, it seems in Dart's current implementation this is impossible due to runtime reflection being disabled as referenced here in the official docs.
在深入研究这个问题时,似乎在Dart的当前实现中,由于禁用了运行时反射,这是不可能的,如官方文档中所引用的这里。
There are ongoing discussions about the support for this and the associated package dart:mirrors here on GitHub, but so far though there is some desire for such functionality it is highly unlikely to ever be implemented.
关于此问题和相关包dart:mirrors的支持,正在GitHub上进行着持续的讨论,但迄今为止,尽管有一些对此功能的渴望,但很不可能被实现。
As a result, the only options are:
- Use code generation libraries to generate methods.
- Manual serialisation/deserialisation methods.
- Implement classes with complex types such as lists and maps to be dynamic, enabling (all be it limited) automatic serialisation/deserialisation.
因此,唯一的选择是:
- 使用代码生成库生成方法。
- 手动序列化/反序列化方法。
- 实现具有复杂类型的类,如列表和映射,使其成为动态的,从而实现(尽管有限)自动序列化/反序列化。
英文:
Conducting more research into this issue, it seems in Dart's current implementation this is impossible due to runtime reflection being disabled as referenced here in the official docs.
There are ongoing discussions about the support for this and the associated package dart:mirrors here on GitHub, but so far though there is some desire for such functionality it is highly unlikely to ever be implemented.
As a result, the only options are:
- Use code generation libraries to generate methods.
- Manual serialisation/deserialisation methods.
- Implement classes with complex types such as lists and maps to be dynamic, enabling (all be it limited) automatic serialisation/deserialisation.
答案2
得分: 0
以下是翻译好的部分:
你的问题没有具体说明 `dynamicTypes` 是如何构建的,或者它的键是如何派生的。所以对于我来说,可能有一些细节不太清楚。
但是像这样的做法如何?
如果你的值是动态的而不是一个字符串:
甚至更简洁的是:
英文:
Your question does not specify how exactly dynamicTypes
is built, or how its key is derived. So there is perhaps a detail to this that is not clear to me.
But what about something like this?
class Caster<T> {
final T Function(String) fromString;
Caster(this.fromString);
}
void main() {
Map<String, Caster> dynamicTypes = { "key": Caster<int>((s) => int.parse(s)) };
int v = dynamicTypes['key']!.fromString('1');
print(v);
}
Or, if you have the value as a dynamic
rather than a string:
class Caster<T> {
T cast(dynamic value) => value as T;
}
void main() {
Map<String, Caster> dynamicTypes = { "key": Caster<int>() };
dynamic a = 1;
int v = dynamicTypes['key']!.cast(a);
print(v);
}
Or even more succinctly:
void main() {
dynamic a = 1;
int v = a;
print(v);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论