kotlinx_dart_converter
所属分类:Kotlin编程
开发工具:Dart
文件大小:11KB
下载次数:0
上传日期:2018-10-26 08:54:03
上 传 者:
sh-1993
说明: 用于将Kotlinx JSON转换为Dart JSON的转换器。支持多态类。
(A converter for converting the Kotlinx JSON to Dart JSON. Supports polymorphic classes.)
文件列表:
.DS_Store (10244, 2018-10-26)
CHANGELOG.md (50, 2018-10-26)
LICENSE (1069, 2018-10-26)
analysis_options.yaml (376, 2018-10-26)
example (0, 2018-10-26)
example\long_example.dart (2209, 2018-10-26)
example\new_example.dart (851, 2018-10-26)
example\tensor_example.dart (772, 2018-10-26)
lib (0, 2018-10-26)
lib\.DS_Store (8196, 2018-10-26)
lib\kotlinx_dart_converter.dart (146, 2018-10-26)
lib\src (0, 2018-10-26)
lib\src\kotlinx_dart_converter.dart (3648, 2018-10-26)
pubspec.yaml (356, 2018-10-26)
test (0, 2018-10-26)
test\mubs_test.dart (521, 2018-10-26)
# KotlinxDartConverter
#### What?
A package for converting from the Kotlin library `Kotlinx` JSON format (which supports polymorphism),
to the Dart library `built_value`.
#### Why?
`Kotlinx` has a different representation of objects than standard JSON, and is incompatible with
the most robust serialization library for Dart which also supports polymorphism (`built_value`).
#### Small Example
Specifically, `Kotlinx` will represent a polymorph object as:
```
[
"ClassName",
{
"field1" : "value1",
"field2" : "value2",
...
}
]
```
Whereas `built_value` for Dart expects the following format:
```
{
"$" : "ClassName",
"field1" : "value1",
"field2" : "value2",
...
}
```
#### Usage Example
```dart
Map nonPolymorphicClasses = {
"triggers": "dk.cachet.carp.protocols.domain.triggers.TriggerWithId",
"triggeredTasks": "dk.cachet.carp.protocols.domain.triggers.TriggeredTask",
};
AdvancedKotlinxDartConverter jsonConverter = new AdvancedKotlinxDartConverter();
jsonConverter.nonPolymorphicClasses = nonPolymorphicClasses;
Map result = jsonConverter.convert(kotlinJson);
// Print the resulting JSON string
print(json.encode(result));
```
#### Elaborate Example (updated August 8th 2018)
Given the following Study protocol, containing both polymorphic- as well ans non-polymorphic classes:
```json
{
"ownerId": "9c3aff68-b7f0-491a-9f1a-e2ca88bf01cf",
"name": "MUBS",
"masterDevices": [
[
"dk.cachet.carp.protocols.domain.devices.Smartphone",
{ "isMasterDevice": true, "roleName": "Patient phone" }
]
],
"connectedDevices": [],
"connections": [],
"tasks": [
[
"dk.cachet.carp.protocols.domain.tasks.IndefiniteTask",
{
"name": "Start measures",
"measures": [
[
"dk.cachet.carp.protocols.domain.tasks.measures.GpsMeasure",
{
"type": [
"dk.cachet.carp.protocols.domain.data.GpsDataType",
{ "category": "Location" }
]
}
],
[
"dk.cachet.carp.protocols.domain.tasks.measures.StepCountMeasure",
{
"type": [
"dk.cachet.carp.protocols.domain.data.StepCountDataType",
{ "category": "Movement" }
]
}
]
]
}
]
],
"triggers": [
{
"id": 0,
"trigger": [
"dk.cachet.carp.protocols.domain.triggers.StartOfStudyTrigger",
{
"sourceDeviceRoleName": "Patient phone",
"requiresMasterDevice": true
}
]
}
],
"triggeredTasks": [
{
"triggerId": 0,
"taskName": "Start measures",
"targetDeviceRoleName": "Patient phone"
}
]
}
```
The converter will output the following JSON (when encoded as a string):
```json
{
"ownerId": "9c3aff68-b7f0-491a-9f1a-e2ca88bf01cf",
"name": "MUBS",
"masterDevices": [
{
"$": "dk.cachet.carp.protocols.domain.devices.Smartphone",
"isMasterDevice": true,
"roleName": "Patient phone"
}
],
"connectedDevices": [],
"connections": [],
"tasks": [
{
"$": "dk.cachet.carp.protocols.domain.tasks.IndefiniteTask",
"name": "Start measures",
"measures": [
{
"$": "dk.cachet.carp.protocols.domain.tasks.measures.GpsMeasure",
"type": [
{
"$": "dk.cachet.carp.protocols.domain.data.GpsDataType",
"category": "Location"
}
]
},
{
"$":
"dk.cachet.carp.protocols.domain.tasks.measures.StepCountMeasure",
"type": [
{
"$": "dk.cachet.carp.protocols.domain.data.StepCountDataType",
"category": "Movement"
}
]
}
]
}
],
"triggers": [
{
"$": "dk.cachet.carp.protocols.domain.triggers.TriggerWithId",
"id": 0,
"trigger": [
{
"$": "dk.cachet.carp.protocols.domain.triggers.StartOfStudyTrigger",
"sourceDeviceRoleName": "Patient phone",
"requiresMasterDevice": true
}
]
}
],
"triggeredTasks": [
{
"$": "dk.cachet.carp.protocols.domain.triggers.TriggeredTask",
"triggerId": 0,
"taskName": "Start measures",
"targetDeviceRoleName": "Patient phone"
}
]
}
```
近期下载者:
相关文件:
收藏者: