package dinetrpc import ( "fmt" "encoding/json" ) func (msg *Msg) UnmarshalJSON(b []byte) error { hdr := &HeaderMsg{} if err := json.Unmarshal(b, hdr); err != nil { return err } msg.HeaderMsg = hdr var classMethod string var msgType MsgType if msg.Pub != "" { classMethod = msg.Pub msgType = MsgType_Publish } else if msg.Req != "" { classMethod = msg.Req msgType = MsgType_Request } else if msg.Rep != "" { classMethod = msg.Rep msgType = MsgType_Reply // TODO attach error //if err := json.Unmarshal(b, msg.Error); err != nil { // return err //} } else { return fmt.Errorf("unknown") } msg.Type = msgType msg.ClassMethod = ClassMethodFromJSONString(classMethod) // No need to decode result for requests if msg.Type == MsgType_Request { return nil } if msg.ClassMethod == ClassMethod_SensorData { sensorDataResult := &SensorDataResult{} if err := json.Unmarshal(b, sensorDataResult); err != nil { return err } msg.Result = &Msg_SensorDataResult{SensorDataResult: sensorDataResult} } else if msg.ClassMethod == ClassMethod_DeviceData { deviceDataResult := &DeviceDataResult{} if err := json.Unmarshal(b, deviceDataResult); err != nil { return err } msg.Result = &Msg_DeviceDataResult{DeviceDataResult: deviceDataResult} } else if msg.ClassMethod == ClassMethod_DeviceInfo { deviceInfoResult := &DeviceInfoResult{} if err := json.Unmarshal(b, deviceInfoResult); err != nil { return err } msg.Result = &Msg_DeviceInfoResult{DeviceInfoResult: deviceInfoResult} } else if msg.ClassMethod == ClassMethod_SensorInfo { sensorInfoResult := &SensorInfoResult{} if err := json.Unmarshal(b, sensorInfoResult); err != nil { return err } msg.Result = &Msg_SensorInfoResult{SensorInfoResult: sensorInfoResult} } else if msg.ClassMethod == ClassMethod_ConfigInfo { configInfoResult := &ConfigInfoResult{} if err := json.Unmarshal(b, configInfoResult); err != nil { return err } msg.Result = &Msg_ConfigInfoResult{ConfigInfoResult: configInfoResult} } else if msg.ClassMethod == ClassMethod_ActionInfo { actionInfoResult := &ActionInfoResult{} if err := json.Unmarshal(b, actionInfoResult); err != nil { return err } msg.Result = &Msg_ActionInfoResult{ActionInfoResult: actionInfoResult} } else if msg.ClassMethod == ClassMethod_ConnectionInfo { connectionInfoResult := &ConnectionInfoResult{} if err := json.Unmarshal(b, connectionInfoResult); err != nil { return err } msg.Result = &Msg_ConnectionInfoResult{ConnectionInfoResult: connectionInfoResult} } return nil } func (cm ClassMethod) MarshalJSON() ([]byte, error) { name, ok := ClassMethod_JSON_name[int32(cm)] if !ok { name = ClassMethod_JSON_name[int32(ClassMethod_UnknownUnknown)] } return json.Marshal(name) } func (cm *ClassMethod) UnmarshalJSON(b []byte) error { var name string if err := json.Unmarshal(b, &name); err != nil { return err } *cm = ClassMethodFromJSONString(name) return nil } func (v *Value) UnmarshalJSON(b []byte) error { var jv interface{} if err := json.Unmarshal(b, &jv); err != nil { return err } switch value := jv.(type) { case string: v.Value = &Value_String_{String_: value} case int, int8, int16, int32, int64: v.Value = &Value_Number{Number: float64(value.(int64))} case uint, uint8, uint16, uint32, uint64: v.Value = &Value_Number{Number: float64(value.(uint64))} case float32, float64: v.Value = &Value_Number{Number: float64(value.(float64))} case bool: v.Value = &Value_Bool{Bool: value} case map[string]interface{}: gps := &GPSValue{} if err := json.Unmarshal(b, &gps); err != nil { return err } v.Value = &Value_GPS{GPS: gps} return nil default: return fmt.Errorf("unsupported value type") } return nil } func (v *Value) MarshalJSON() ([]byte, error) { var value interface{} if x, ok := v.GetValue().(*Value_Bool); ok { value = x.Bool } else if x, ok := v.GetValue().(*Value_Number); ok { value = x.Number } else if x, ok := v.GetValue().(*Value_String_); ok { value = x.String_ } return json.Marshal(value) }