kotlinx_dart_converter 1.0.6

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 50

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

Map<String, String> 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<String, dynamic> 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:

{
  "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):

{
  "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"
    }
  ]
}

1.0.0

  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  kotlinx_dart_converter: ^1.0.6

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:kotlinx_dart_converter/kotlinx_dart_converter.dart';
  
Version Uploaded Documentation Archive
1.0.6 Oct 26, 2018 Go to the documentation of kotlinx_dart_converter 1.0.6 Download kotlinx_dart_converter 1.0.6 archive
1.0.5 Aug 8, 2018 Go to the documentation of kotlinx_dart_converter 1.0.5 Download kotlinx_dart_converter 1.0.5 archive
1.0.4 Aug 8, 2018 Go to the documentation of kotlinx_dart_converter 1.0.4 Download kotlinx_dart_converter 1.0.4 archive
1.0.3 Aug 8, 2018 Go to the documentation of kotlinx_dart_converter 1.0.3 Download kotlinx_dart_converter 1.0.3 archive
1.0.2 Aug 7, 2018 Go to the documentation of kotlinx_dart_converter 1.0.2 Download kotlinx_dart_converter 1.0.2 archive
1.0.1 Aug 6, 2018 Go to the documentation of kotlinx_dart_converter 1.0.1 Download kotlinx_dart_converter 1.0.1 archive
1.0.0 Aug 6, 2018 Go to the documentation of kotlinx_dart_converter 1.0.0 Download kotlinx_dart_converter 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:kotlinx_dart_converter/kotlinx_dart_converter.dart.

Health suggestions

Format lib/kotlinx_dart_converter.dart.

Run dartfmt to format lib/kotlinx_dart_converter.dart.

Format lib/src/kotlinx_dart_converter.dart.

Run dartfmt to format lib/src/kotlinx_dart_converter.dart.

Maintenance suggestions

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use kotlinx_dart_converter.dart. Packages with multiple examples should use example/readme.md.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
Dev dependencies
test ^1.0.0