Documentation
¶
Index ¶
- Constants
- func DecodeCBOR(b []byte) (senml.Pack, error)
- func DecodeCSV(b []byte, options ...Option) (senml.Pack, error)
- func DecodeJSON(b []byte) (senml.Pack, error)
- func DecodeXML(b []byte) (senml.Pack, error)
- func EncodeCBOR(p senml.Pack) ([]byte, error)
- func EncodeCSV(p senml.Pack, options ...Option) ([]byte, error)
- func EncodeJSON(p senml.Pack, options ...Option) ([]byte, error)
- func EncodeXML(p senml.Pack, options ...Option) ([]byte, error)
- func PrettyPrint(o *codecOptions)
- func ReadCSV(r io.Reader, options ...Option) (senml.Pack, error)
- func WithHeader(o *codecOptions)
- func WriteCSV(p senml.Pack, w io.Writer, options ...Option) error
- type Option
Examples ¶
Constants ¶
View Source
const CSVHeader = "Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum"
CSVHeader is the fixed CSV header
Variables ¶
This section is empty.
Functions ¶
func DecodeCBOR ¶
DecodeCBOR takes a SenML pack in CBOR bytes and decodes it into a Pack
func DecodeJSON ¶
DecodeJSON takes a SenML pack in JSON bytes and decodes it into a Pack
Example ¶
package main
import (
"github.com/farshidtz/senml/v2/codec"
)
func main() {
input := `[{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},{"u":"Cel","t":1276020091.305,"v":23.6}]`
// decode JSON
pack, err := codec.DecodeJSON([]byte(input))
if err != nil {
panic(err) // handle the error
}
// validate the SenML Pack
err = pack.Validate()
if err != nil {
panic(err) // handle the error
}
}
func EncodeCBOR ¶
EncodeCBOR serializes the SenML pack into CBOR bytes
func EncodeCSV ¶
EncodeCSV serializes the SenML pack into CSV bytes
Example ¶
var pack senml.Pack = []senml.Record{
{Time: 1276020000, Name: "room1/temp_label", StringValue: "hot"},
{Time: 1276020100, Name: "room1/temp_label", StringValue: "cool"},
}
// encode to CSV (format: name,excel-time,value,unit)
csvBytes, err := codec.EncodeCSV(pack, codec.WithHeader)
if err != nil {
panic(err) // handle the error
}
fmt.Printf("%s\n", csvBytes)
Output: Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum 1276020000,0,room1/temp_label,,,hot,,, 1276020100,0,room1/temp_label,,,cool,,,
func EncodeJSON ¶
EncodeJSON serializes the SenML pack into JSON bytes
Example ¶
v := 23.1
var p senml.Pack = []senml.Record{
{Value: &v, Unit: "Cel", Name: "urn:dev:ow:10e2073a01080063"},
}
dataOut, err := codec.EncodeJSON(p)
if err != nil {
panic(err) // handle the error
}
fmt.Printf("%s", dataOut)
Output: [{"n":"urn:dev:ow:10e2073a01080063","u":"Cel","v":23.1}]
func EncodeXML ¶
EncodeXML serializes the SenML pack into XML bytes
Example ¶
var pack senml.Pack = []senml.Record{
{Time: 1276020000, Name: "room1/temp_label", StringValue: "hot"},
{Time: 1276020100, Name: "room1/temp_label", StringValue: "cool"},
}
// encode to Pretty XML
xmlBytes, err := codec.EncodeXML(pack, codec.PrettyPrint)
if err != nil {
panic(err) // handle the error
}
fmt.Printf("%s\n", xmlBytes)
Output: <sensml xmlns="urn:ietf:params:xml:ns:senml"> <senml n="room1/temp_label" t="1.27602e+09" vs="hot"></senml> <senml n="room1/temp_label" t="1.2760201e+09" vs="cool"></senml> </sensml>
func PrettyPrint ¶
func PrettyPrint(o *codecOptions)
PrettyPrint enables indentation of JSON and XML outputs
Types ¶
Click to show internal directories.
Click to hide internal directories.