Documentation ¶
Index ¶
Examples ¶
Constants ¶
const F = "false"
const T = "true"
Variables ¶
var Bools = map[bo]by{ // contains filtered or unexported fields }
Functions ¶
This section is empty.
Types ¶
type Base64 ¶
type Base64 struct{ V by }
Base64 is a string representing binary data encoded as base64.
func (*Base64) Marshal ¶
func (b2 *Base64) Marshal(dst by) (b by)
Example ¶
const ( hexVal = "deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef00" ) bin, err := hex.Dec(hexVal) if err != nil { return } b1 := &Base64{bin} var b by b = b1.Marshal(nil) fmt.Printf("%s\n", b) b2 := &Base64{} var rem by rem, err = b2.Unmarshal(b) if chk.E(err) || len(rem) != 0 { fmt.Printf("%s\n%s", err.Error(), rem) return } fmt.Printf("data: %0x\n", b2.V) fmt.Printf("%v\n", equals(bin, b2.V))
Output: "3q2+78r+ASNFZ4mrze8A3q2+78r+ASNFZ4mrze8A" data: deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef00 true
type Bech32 ¶
type Bech32 struct{ HRP, V by }
Bech32 is a string encoded in bech32 format including a human-readable prefix and base32 encoded binary data. A key difference is the HRP prefix.
To create a new Bech32, load the HRP variable with the intended HRP for encoding.
When decoding, point this variable at the expected HRP, if it doesn't match in what is encoded, it returns an error.
func (*Bech32) Marshal ¶
func (b2 *Bech32) Marshal(dst by) (b by)
Example ¶
const ( hrp = "herp" hexVal = "00deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef" ) bin, err := hex.Dec(hexVal) if err != nil { return } b32 := &Bech32{by(hrp), bin} b := b32.Marshal(nil) fmt.Printf("%s\n", b) b33 := &Bech32{HRP: by(hrp)} var rem by rem, err = b33.Unmarshal(b) if chk.E(err) || len(rem) != 0 { return } fmt.Printf("hrp: %s\ndata: %0x\n", b33.HRP, b33.V) fmt.Printf("%v\n", equals(bin, b33.V))
Output: "herp1qr02m0h0etlqzg69v7y6hn00qr02m0h0etlqzg69v7y6hn00jujvlj" hrp: herp data: 00deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef true
type Bool ¶
type Bool struct{ V bo }
Bool can be either `true` or `false` and only lower case. Initialize these values as follows:
truthValue := &json.Bool{}
to get a default value which is false.
func (*Bool) Marshal ¶
func (b2 *Bool) Marshal(dst by) (b by)
Example ¶
var b by bt := &Bool{true} b = bt.Marshal(b) fmt.Printf("%s\n", b) bt2 := &Bool{} rem, err := bt2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", bt2.V == true) b = b[:0] bf := &Bool{} // implicit initialized bool is false b = bf.Marshal(b) fmt.Printf("%s\n", b) fmt.Printf("%v\n", bf.V == false)
Output: true true false true
type Hex ¶
type Hex struct{ V by }
Hex is a string representing binary data encoded as hexadecimal.
func (*Hex) Marshal ¶
func (h *Hex) Marshal(dst by) (b by)
Example ¶
const ( hexVal = "deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef" ) bin, err := hex.Dec(hexVal) if err != nil { return } h := &Hex{bin} b := h.Marshal(nil) fmt.Printf("%s\n", b) h2 := &Hex{} var rem by rem, err = h2.Unmarshal(b) if chk.E(err) || len(rem) != 0 { fmt.Printf("%s\n%s", err.Error(), rem) return } fmt.Printf("data: %0x\n", h2.V) fmt.Printf("%v\n", equals(bin, h2.V))
Output: "deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef" data: deadbeefcafe0123456789abcdef00deadbeefcafe0123456789abcdef true
type KeyValue ¶
type KeyValue struct { Key by Value I }
A KeyValue is a field in an Object.
func (*KeyValue) Marshal ¶
func (k *KeyValue) Marshal(dst by) (b by)
Example ¶
const ( // deliberately put whitespace here to make sure it parses. even garbage will parse, but // we aren't going to bother, mainly whitespace needs to be allowed. keyVal = `"key" : "value"` ) kv := &KeyValue{Value: &String{}} rem, err := kv.Unmarshal(by(keyVal)) if chk.E(err) || len(rem) != 0 { fmt.Printf("%s\n'%s'", err.Error(), rem) return } kv2 := &KeyValue{by("key"), &String{by("value")}} var b, b2 by b = kv.Marshal(b) b2 = kv2.Marshal(b2) fmt.Printf("%s\n%s\n%v\n", b, b2, equals(b, b2))
Output: "key":"value" "key":"value" true
type Object ¶
type Object struct{ V []KeyValue }
An Object is an (not necessarily) ordered list of KeyValue.
type Signed ¶
type Signed struct{ V int64 }
Signed integers can be negative and thus a `-` prefix.
Initialize these values as follows:
num := &json.Signed{}
to get a default value which is zero.
Technically we are supporting negative numbers here but in fact in nostr message encodings there is no such thing as any negative integers, nor floating point, but this ensures if there is ever need for negative values they are supported. For the most part this will be used for timestamps.
There is a NewSigned function that accepts any type of generic signed integer value and automatically converts it to the biggest type that is used in runtime.
func NewUnsigned ¶
func (*Signed) Marshal ¶
func (s *Signed) Marshal(dst by) (b by)
Example ¶
var b by s := &Signed{} b = s.Marshal(b) fmt.Printf("%s\n", b) s2 := &Signed{} rem, err := s2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", s2.V == 0) s.V = 69420 b = b[:0] b = s.Marshal(b) fmt.Printf("%s\n", b) rem, err = s2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", s2.V == s.V) s.V *= -69420 b = b[:0] b = s.Marshal(b) fmt.Printf("%s\n", b) rem, err = s2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", s2.V == s.V)
Output: 0 true 69420 true -4819136400 true
type String ¶
type String struct{ V by }
String is a regular string. Must be escaped as per nip-01. Bytes stored in it are not escaped, only must be escaped to output JSON.
Again like the other types, create a new String with:
str := json.String{}
There is also a convenience NewString function that generically automatically converts actual golang strings to save the caller from doing so.
func (*String) Marshal ¶
func (s *String) Marshal(dst by) (b by)
Example ¶
var b by const ex = `test with newlines and hidden tab and spaces at the end ` s := NewString(ex) b = s.Marshal(b) fmt.Printf("%s\n", b) s2 := &String{} rem, err := s2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", equals(s2.V, by(ex)))
Output: "test with\n\t\nnewlines and hidden tab and spaces at the end " true
type Unsigned ¶
type Unsigned struct{ V uint64 }
Unsigned integers have no possible `-` prefix nor a decimal place.
Initialize these values as follows:
num := &json.Unsigned{}
to get a default value which is zero.
There is a generic NewUnsigned function which will automatically convert any integer type to the internal uint64 type, saving the caller from needing to cast it in their code.
func (*Unsigned) Marshal ¶
func (u *Unsigned) Marshal(dst by) (b by)
Example ¶
var b by u := &Unsigned{} b = u.Marshal(b) fmt.Printf("%s\n", b) u2 := &Unsigned{} rem, err := u2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", u2.V == 0) u.V = 69420 b = b[:0] b = u.Marshal(b) fmt.Printf("%s\n", b) rem, err = u2.Unmarshal(b) if err != nil || len(rem) != 0 { return } fmt.Printf("%v\n", u2.V == 69420)
Output: 0 true 69420 true