Documentation
¶
Index ¶
- Constants
- func Keys[V any](m map[string]V) []string
- func MarshalJSON(s Serializable) ([]byte, error)
- func Sorted(keys []string) []string
- type BufferedSerializer
- type JsonSerializer
- func (j *JsonSerializer) Begin()
- func (j *JsonSerializer) BeginBlock(optionalTitle string)
- func (j *JsonSerializer) BeginList(optionalTitle string)
- func (j *JsonSerializer) End()
- func (j *JsonSerializer) EndBlock()
- func (j *JsonSerializer) EndList()
- func (j *JsonSerializer) Error() error
- func (j *JsonSerializer) KeyBool(k string, v bool)
- func (j *JsonSerializer) KeyFloat(k string, v float64)
- func (j *JsonSerializer) KeyInt(k string, v int)
- func (j *JsonSerializer) KeyRaw(k string, v json.RawMessage, compact bool)
- func (j *JsonSerializer) KeyString(k, v string)
- func (j *JsonSerializer) Param(s string)
- func (j *JsonSerializer) Serialize(s Serializable)
- func (j *JsonSerializer) Setup(w Writer, params map[string]string)
- func (j *JsonSerializer) String(v string, compact bool)
- type OptionalBool
- type Serializable
- type Serializer
- type Writer
Constants ¶
const MinIndent = " "
Variables ¶
This section is empty.
Functions ¶
func MarshalJSON ¶
func MarshalJSON(s Serializable) ([]byte, error)
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
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) 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) 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
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 }