37 lines
573 B
Go
37 lines
573 B
Go
|
package gophx
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
type message struct {
|
||
|
joinRef int64
|
||
|
ref int64
|
||
|
topic string
|
||
|
event string
|
||
|
payload any
|
||
|
}
|
||
|
|
||
|
func (m message) serializeJSON() ([]byte, error) {
|
||
|
data, err := json.Marshal([]any{m.joinRef, m.ref, m.topic, m.event, m.payload})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
func deserializeJSON(data []byte, msg *message) error {
|
||
|
m := message{}
|
||
|
slice := []any{&m.joinRef, &m.ref, &m.topic, &m.event, &m.payload}
|
||
|
|
||
|
err := json.Unmarshal(data, &slice)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
*msg = m
|
||
|
|
||
|
return nil
|
||
|
}
|