codec

package
v2.0.0-alpha.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

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

func DecodeCBOR(b []byte) (senml.Pack, error)

DecodeCBOR takes a SenML pack in CBOR bytes and decodes it into a Pack

func DecodeCSV

func DecodeCSV(b []byte, options ...Option) (senml.Pack, error)

DecodeCSV takes a SenML pack in CSV bytes and decodes it into a Pack

func DecodeJSON

func DecodeJSON(b []byte) (senml.Pack, error)

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 DecodeXML

func DecodeXML(b []byte) (senml.Pack, error)

DecodeXML takes a SenML pack in XML bytes and decodes it into a Pack

func EncodeCBOR

func EncodeCBOR(p senml.Pack) ([]byte, error)

EncodeCBOR serializes the SenML pack into CBOR bytes

func EncodeCSV

func EncodeCSV(p senml.Pack, options ...Option) ([]byte, error)

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

func EncodeJSON(p senml.Pack, options ...Option) ([]byte, error)

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

func EncodeXML(p senml.Pack, options ...Option) ([]byte, error)

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

func ReadCSV

func ReadCSV(r io.Reader, options ...Option) (senml.Pack, error)

func WithHeader

func WithHeader(o *codecOptions)

WithHeader enables CSV header

func WriteCSV

func WriteCSV(p senml.Pack, w io.Writer, options ...Option) error

Types

type Option

type Option func(*codecOptions)

Option is the function type for setting codec options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL