Serializer
Description
The Serializer object can be used to serialize properties of the source object. This can be used to replicate a object in a variant map or human readable in a JSON format.
Note
Be carefully when mode is set to Serializer.SerializeOnChange. This can update data multiple times. For example updating the data property of an DataObject will also update its timestamp and hence serialize twice. This is also noted in the example at the bottom. If this behaviour is a problem for your application use Serializer.SerializeManually and connect update() to a unique signal.
- › Inherits:
Overview
Properties
Methods
Signals
Enumerations
Properties
data
This property holds the output of the serialization.
- › Type:
Variant
- › Signal:
dataChanged()
- › Attributes:
Writable
excludedProperties
This property holds a list of source property names as strings which should not be serialized.
- › Type:
StringList
- › Signal:
excludedPropertiesChanged()
- › Attributes:
Writable
format
This property holds the format which is used for the serialization.
- › Type:
- › Default:
- › Signal:
formatChanged()
- › Attributes:
Writable
interval
This property holds the interval of the periodically serialization. This property does not have a effect when mode is not set to Serializer.SerializePeriodically.
- › Type:
SignedInteger
- › Default:
0- › Signal:
intervalChanged()
- › Attributes:
Writable
mode
This property holds the mode which defined the moment when the source should be serialized.
- › Type:
- › Default:
- › Signal:
modeChanged()
- › Attributes:
Writable
source
This property holds the object which should be serialized.
- › Type:
- › Signal:
sourceChanged()
- › Attributes:
Writable
Methods
update()
This method updates data with the serialized duplicate of source.
Enumerations
Format
This enumeration describes which format should be used to output the serialized data.
Name |
Value |
Description |
|---|---|---|
|
|
defines human readable output with indention and line breaks after each key value pair. |
|
|
defines human readable output in one line without spaces. |
|
|
the data is left unformatted as a map. |
Mode
This enumeration describes all available modes when the source object should be serialized.
Name |
Value |
Description |
|---|---|---|
|
|
serialize only when update() is called. |
|
|
serialize periodically with the given interval. |
|
|
serialize properties whenever a property was changed. |
Example
import InCore.Foundation 2.5
Application {
// counts uptime in seconds
Counter {
// default interval is 1 s
id: counter
increment: 1
startValue: 0
}
// measurement to display uptime
MeasurementGroup {
id: measurementGroup
Measurement {
id: measurement
data: counter.value
name: "uptime"
}
}
// pack data to a string
Serializer {
source: measurementGroup
mode: Serializer.SerializeOnChange
format: Serializer.CompactJsonString
excludedProperties: ["displayString", "siPrefix"]
onDataChanged: console.log( "serialized data changed", data )
}
}