33 lines
497 B
Go
33 lines
497 B
Go
|
package event
|
||
|
|
||
|
import "encoding/json"
|
||
|
|
||
|
type (
|
||
|
Event struct {
|
||
|
Timestamp int64 `json:"timestamp"`
|
||
|
Type uint16 `json:"type"`
|
||
|
Value string `json:"payload"`
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func (e Event) Serialize() ([]byte, error) {
|
||
|
jsonData, err := json.Marshal(e)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return jsonData, err
|
||
|
}
|
||
|
|
||
|
func Deserialize(jsonData []byte) (Event, error) {
|
||
|
var (
|
||
|
event Event
|
||
|
)
|
||
|
|
||
|
if err := json.Unmarshal(jsonData, &event); err != nil {
|
||
|
return event, err
|
||
|
}
|
||
|
|
||
|
return event, nil
|
||
|
}
|