serialize

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const MinIndent = "  "

Variables

This section is empty.

Functions

func Keys added in v0.3.7

func Keys[V any](m map[string]V) []string

Keys returns the sorted list of keys in a map

func MarshalJSON

func MarshalJSON(s Serializable) ([]byte, error)

func Sorted added in v0.3.7

func Sorted(keys []string) []string

Sorted sorts a string slice

Types

type BufferedSerializer

type BufferedSerializer struct {
	JsonSerializer
	// We buffer the writer to prepend locals later
	Original Writer
	Buffer   bytes.Buffer
	Buffered *bufio.Writer
}

Serializer that collects output into a buffer

func (*BufferedSerializer) End

func (j *BufferedSerializer) End()

End implements Serializer

func (*BufferedSerializer) Setup

func (j *BufferedSerializer) Setup(w Writer, params map[string]string)

Setup implements Serializer

type JsonSerializer

type JsonSerializer struct {
	Writer        Writer            // Where to write
	SupportParams bool              // True to support parameter matching
	ReverseParams map[string]string // match strings and turn into parameters
	Matched       map[string]string // Which params were matched
	Depth         int               // indentation depth, if -1 then do not indent
	Err           error
	// contains filtered or unexported fields
}

Serializes a JSON object to a string.

func (*JsonSerializer) Begin

func (j *JsonSerializer) Begin()

Begin serializing a new object

func (*JsonSerializer) BeginBlock

func (j *JsonSerializer) BeginBlock(optionalTitle string)

BeginBlock opens a block with an optional key

func (*JsonSerializer) BeginList

func (j *JsonSerializer) BeginList(optionalTitle string)

BeginList opens a list with an optional key

func (*JsonSerializer) End

func (j *JsonSerializer) End()

End object serialization

func (*JsonSerializer) EndBlock

func (j *JsonSerializer) EndBlock()

Endblock closes a block

func (*JsonSerializer) EndList

func (j *JsonSerializer) EndList()

EndList closes a list

func (*JsonSerializer) Error

func (j *JsonSerializer) Error() error

Error accumulates errors while encoding to check at the end

func (*JsonSerializer) KeyBool

func (j *JsonSerializer) KeyBool(k string, v bool)

KeyBool dumps a key and value pair, value is bool

func (*JsonSerializer) KeyFloat

func (j *JsonSerializer) KeyFloat(k string, v float64)

KeyFloat dumps a key and value pair, value is float

func (*JsonSerializer) KeyInt

func (j *JsonSerializer) KeyInt(k string, v int)

KeyInt dumps a key and value pair, value is int

func (*JsonSerializer) KeyRaw

func (j *JsonSerializer) KeyRaw(k string, v json.RawMessage, compact bool)

KeyRaw dumps a key and value pair, value is json.RawMessage

func (*JsonSerializer) KeyString

func (j *JsonSerializer) KeyString(k, v string)

KeyString dumps a key and value pair, value is string

func (*JsonSerializer) Param

func (j *JsonSerializer) Param(s string)

Param called on every string, to check if it must be replaced

func (*JsonSerializer) Serialize

func (j *JsonSerializer) Serialize(s Serializable)

Serialize recursos into a Serializable object

func (*JsonSerializer) Setup

func (j *JsonSerializer) Setup(w Writer, params map[string]string)

Setup must be called before starting serializing

func (*JsonSerializer) String

func (j *JsonSerializer) String(v string, compact bool)

KeyString dumps a string

type OptionalBool added in v0.5.2

type OptionalBool struct {
	Defined bool
	Value   bool
}

Some bools in the IoTA APIs have different behaviour if they are undefined versus false. For instance, "timestamp === false" might not be the same as "timestamp === undefined", there is a global config parameter for that.

For this reason, all those bools that are marked as `omitempty` cannot be just omitted when set to 'false'. They must only be omitted if they are actually not defined.

I could achieve this using `*bool` instead of `bool` as the type for these settings, but then I'd have to support pointers in the code generation tool, and see how to handle them in cue too.

So in the end I preferred a custom type with custom marshaller, unmarshaller and a "SerializeHook" method that tells the code generation tool to generate the correct code.

func (OptionalBool) IsEmpty added in v0.5.2

func (o OptionalBool) IsEmpty() bool

IsEmpty implements the code generator isEmpty interface

func (*OptionalBool) MarshalJSON added in v0.5.2

func (o *OptionalBool) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (OptionalBool) SerializeHook added in v0.5.2

func (o OptionalBool) SerializeHook(property string, serializer Serializer)

SerializeHook Implements the code generator isHook interface

func (*OptionalBool) UnmarshalJSON added in v0.5.2

func (o *OptionalBool) UnmarshalJSON(data []byte) error

UnmarshalJson implements the json.Unmarshaler interface.

type Serializable

type Serializable interface {
	Serialize(Serializer)
}

type Serializer

type Serializer interface {
	KeyString(k, v string)                            // , "key": "val"
	String(v string, compact bool)                    // , "val"
	KeyInt(k string, v int)                           // , "key": val
	KeyFloat(k string, v float64)                     // , "key": val
	KeyBool(k string, v bool)                         // , "key": val
	KeyRaw(k string, v json.RawMessage, compact bool) // , "key": val
	Serialize(Serializable)
	BeginBlock(string) // { + omit first ","
	EndBlock()         // }
	BeginList(string)  // [ + omit first ","
	EndList()          // ]
	Error() error      // If it failed at any step
}

type Writer

type Writer interface {
	io.Writer
	io.StringWriter
}

Jump to

Keyboard shortcuts

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