-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshall.go
126 lines (105 loc) · 3 KB
/
marshall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Generic Data Record - marshall.go
* Copyright (c) 2019 - 2024 TQ-Systems GmbH <[email protected]>, D-82229 Seefeld, Germany. All rights reserved.
* Author: Alexander Poegelt and the Energy Manager development team
*
* This software code contained herein is licensed under the terms and conditions of
* the TQ-Systems Product Software License Agreement Version 1.0.1 or any later version.
* You will find the corresponding license text in the LICENSE file.
* In case of any license issues please contact [email protected].
*/
package gdr
import (
"encoding/json"
"errors"
)
// MarshalJSON is the custom marshalling implementation for DeviceType
func (d DeviceType) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON is the custom unmarshalling implementation for DeviceType
func (d *DeviceType) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
val, ok := DeviceType_value[str]
if !ok {
return errors.New("invalid device type: " + str)
}
*d = DeviceType(val)
return nil
}
// MarshalJSON is the custom marshalling implementation for Class
func (c Class) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
// UnmarshalJSON is the custom unmarshalling implementation for Class
func (c *Class) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
val, ok := Class_value[str]
if !ok {
return errors.New("invalid class: " + str)
}
*c = Class(val)
return nil
}
// MarshalJSON is the custom marshalling implementation for Status
func (s Status) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// UnmarshalJSON is the custom unmarshalling implementation for Status
func (s *Status) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
val, ok := Status_value[str]
if !ok {
return errors.New("invalid status: " + str)
}
*s = Status(val)
return nil
}
// MarshalJSON is the custom marshalling implementation for Unit
func (s Unit) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// UnmarshalJSON is the custom unmarshalling implementation for Unit
func (s *Unit) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
val, ok := Unit_value[str]
if !ok {
return errors.New("invalid unit: " + str)
}
*s = Unit(val)
return nil
}
// MarshalJSON is the custom marshalling implementation for FlexValueType
func (s FlexValueType) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// UnmarshalJSON is the custom unmarshalling implementation for FlexValueType
func (s *FlexValueType) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
val, ok := FlexValueType_value[str]
if !ok {
return errors.New("invalid flexible value type: " + str)
}
*s = FlexValueType(val)
return nil
}