Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrTransform represents an error during parsing message. ErrTransform = errors.New("unable to parse JSON object") // ErrFormatPayload represents an error during formatting message payload. ErrFormatPayload = errors.New("unable to format payload") // ErrInvalidKey represents the use of a reserved message field. ErrInvalidKey = errors.New("invalid object key") // ErrInvalidTimeField represents the use an invalid time field. ErrInvalidTimeField = errors.New("invalid time field") )
Functions ¶
func Flatten ¶
Flatten makes nested maps flat using composite keys created by concatenation of the nested keys.
Example ¶
package main
import (
"encoding/json"
"fmt"
mfjson "github.com/MainfluxLabs/mainflux/pkg/transformers/json"
)
func main() {
in := map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key5": map[string]interface{}{
"nested1": map[string]interface{}{
"nested2": "value3",
"nested3": "value4",
},
"nested2": map[string]interface{}{
"nested4": "value5",
},
},
}
out, err := mfjson.Flatten(in)
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(out, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
Output: { "key1": "value1", "key2": "value2", "key5/nested1/nested2": "value3", "key5/nested1/nested3": "value4", "key5/nested2/nested4": "value5" }
func ParseFlat ¶
func ParseFlat(flat interface{}) interface{}
ParseFlat receives flat map that represents complex JSON objects and returns the corresponding complex JSON object with nested maps. It's the opposite of the Flatten function.
Example ¶
package main
import (
"encoding/json"
"fmt"
mfjson "github.com/MainfluxLabs/mainflux/pkg/transformers/json"
)
func main() {
in := map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key5/nested1/nested2": "value3",
"key5/nested1/nested3": "value4",
"key5/nested2/nested4": "value5",
}
out := mfjson.ParseFlat(in)
b, err := json.MarshalIndent(out, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
Output: { "key1": "value1", "key2": "value2", "key5": { "nested1": { "nested2": "value3", "nested3": "value4" }, "nested2": { "nested4": "value5" } } }
Types ¶
type Message ¶
type Message struct {
Created int64 `json:"created,omitempty" db:"created" bson:"created"`
Subtopic string `json:"subtopic,omitempty" db:"subtopic" bson:"subtopic,omitempty"`
Publisher string `json:"publisher,omitempty" db:"publisher" bson:"publisher"`
Protocol string `json:"protocol,omitempty" db:"protocol" bson:"protocol"`
Payload Payload `json:"payload,omitempty" db:"payload" bson:"payload,omitempty"`
Profile Profile `json:"profile,omitempty" db:"profile" bson:"profile,omitempty"`
}
Message represents a JSON messages.
Click to show internal directories.
Click to hide internal directories.