Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cph-cachet/kotlinx_dart_converter
A converter for converting the Kotlinx JSON to Dart JSON. Supports polymorphic classes.
https://github.com/cph-cachet/kotlinx_dart_converter
Last synced: 4 days ago
JSON representation
A converter for converting the Kotlinx JSON to Dart JSON. Supports polymorphic classes.
- Host: GitHub
- URL: https://github.com/cph-cachet/kotlinx_dart_converter
- Owner: cph-cachet
- License: mit
- Created: 2018-08-03T22:10:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-26T08:54:03.000Z (about 6 years ago)
- Last Synced: 2024-04-16T10:58:26.999Z (7 months ago)
- Language: Dart
- Size: 19.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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"
}
]
}
```