Documentation ¶
Overview ¶
Package json contains JSON transformer.
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") // 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" smqjson "github.com/absmach/supermq/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 := smqjson.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" smqjson "github.com/absmach/supermq/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 := smqjson.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 { Channel string `json:"channel,omitempty" db:"channel" bson:"channel"` 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"` }
Message represents a JSON messages.
Click to show internal directories.
Click to hide internal directories.