diff --git a/example/coaps_readproperty.dart b/example/coaps_readproperty.dart index 9f2bbb72..3083be29 100644 --- a/example/coaps_readproperty.dart +++ b/example/coaps_readproperty.dart @@ -61,7 +61,7 @@ Future main(List args) async { }, }; - final thingDescription = ThingDescription.fromJson(thingDescriptionJson); + final thingDescription = thingDescriptionJson.toThingDescription(); final consumedThing = await wot.consume(thingDescription); final status = await consumedThing.readProperty("status"); final value = await status.value(); diff --git a/example/complex_example.dart b/example/complex_example.dart index 1d3ea30a..76d017c6 100644 --- a/example/complex_example.dart +++ b/example/complex_example.dart @@ -103,7 +103,7 @@ Future main() async { ); final wot = await servient.start(); - final thingDescription = ThingDescription.fromJson(thingDescriptionJson); + final thingDescription = thingDescriptionJson.toThingDescription(); final consumedThing = await wot.consume(thingDescription); final status = await consumedThing.readProperty("status"); final value1 = await status.value(); diff --git a/example/http_basic_authentication.dart b/example/http_basic_authentication.dart index f2f96194..7b979d6f 100644 --- a/example/http_basic_authentication.dart +++ b/example/http_basic_authentication.dart @@ -67,7 +67,7 @@ Future main(List args) async { ); final wot = await servient.start(); - final thingDescription = ThingDescription.fromJson(thingDescriptionJson); + final thingDescription = thingDescriptionJson.toThingDescription(); final consumedThing = await wot.consume(thingDescription); final status = await consumedThing.readProperty("status"); diff --git a/example/mqtt_example.dart b/example/mqtt_example.dart index d77a9c3b..0ed4919a 100644 --- a/example/mqtt_example.dart +++ b/example/mqtt_example.dart @@ -67,7 +67,7 @@ Future main(List args) async { final wot = await servient.start(); - final thingDescription = ThingDescription.fromJson(thingDescriptionJson); + final thingDescription = thingDescriptionJson.toThingDescription(); final consumedThing = await wot.consume(thingDescription); await consumedThing.readAndPrintProperty("status"); diff --git a/lib/src/definitions/thing_description.dart b/lib/src/definitions/thing_description.dart index 5e2f8c10..3551fe69 100644 --- a/lib/src/definitions/thing_description.dart +++ b/lib/src/definitions/thing_description.dart @@ -259,3 +259,11 @@ class ThingDescription { return id ?? base?.toString() ?? title; } } + +/// Extension for generating [ThingDescription]s from [Map]s more easily. +extension ToThingDescription on Map { + /// Tries to generate a [ThingDescription] from this [Map] object. + ThingDescription toThingDescription() { + return ThingDescription.fromJson(this); + } +}