Documentation
ΒΆ
Overview ΒΆ
There are 3 functions for generating JSON:
- EncodeString serializes data into a string.
- EncodeBytes serializes data into a slice of bytes.
- AppendBytes is like EncodeBytes but allows you to provide a buffer to use.
Primitive types are constructed by:
- Null
- Bool
- Int
- Int8
- Int16
- Int32
- Int64
- UInt
- UInt8
- UInt16
- UInt32
- UInt64
- UIntPtr
- Float32
- Float64
- String
- SafeString
Each of the above has a nullable version that serializes to null if the value is nil:
- PBool
- PInt
- PInt8
- PInt16
- PInt32
- PInt64
- PUInt
- PUInt8
- PUInt16
- PUInt32
- PUInt64
- PUIntPtr
- PFloat32
- PFloat64
- PString
Collections are constructed by:
Lastly, there are 2 bridges between jsony and stdlib encoding/json:
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"name", jsony.String("aragorn")}, jsony.Field{"age", jsony.Int(87)}, jsony.Field{"admin", jsony.Bool(true)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"name":"aragorn","age":87,"admin":true}
Index ΒΆ
- Constants
- func AppendBytes(buf []byte, e Encoder) []byte
- func Comment(s comment) comment
- func EncodeBytes(e Encoder) []byte
- func EncodeString(e Encoder) string
- func SafeString(s safeString) safeString
- type A
- type Array
- type B
- type Bool
- type Bytes
- type Encoder
- func FromMarshaller(m marshaller) Encoder
- func PBool(v *bool) Encoder
- func PFloat32(v *float32) Encoder
- func PFloat64(v *float64) Encoder
- func PInt(v *int) Encoder
- func PInt16(v *int16) Encoder
- func PInt32(v *int32) Encoder
- func PInt64(v *int64) Encoder
- func PInt8(v *int8) Encoder
- func PString(v *string) Encoder
- func PUInt(v *uint) Encoder
- func PUInt16(v *uint16) Encoder
- func PUInt32(v *uint32) Encoder
- func PUInt64(v *uint64) Encoder
- func PUInt8(v *uint8) Encoder
- func PUIntPtr(v *uintptr) Encoder
- type F
- type Field
- type Float32
- type Float64
- type I
- type Int
- type Int16
- type Int32
- type Int64
- type Int8
- type M
- type Map
- type Marshaller
- type MixedArray
- type O
- type Object
- type S
- type SA
- type SafeArray
- type String
- type U
- type UInt
- type UInt16
- type UInt32
- type UInt64
- type UInt8
- type UIntPtr
- type UO
- type UnsafeField
- type UnsafeObject
Examples ΒΆ
Constants ΒΆ
const ( True = Bool(true) False = Bool(false) )
const Null null = false
Variables ΒΆ
This section is empty.
Functions ΒΆ
func AppendBytes ΒΆ
Marshal JSON into the end of the given slice of bytes.
Useful if you want to reduce allocations by reusing an existing buffer.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { buf := make([]byte, 0, 1024) res := jsony.AppendBytes(buf, jsony.Object{}) fmt.Printf("%v\n", res) }
Output: [123 125]
func EncodeBytes ΒΆ
Marshal JSON as a slice of bytes.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { res := jsony.EncodeBytes(jsony.Object{}) fmt.Printf("%v\n", res) }
Output: [123 125]
func EncodeString ΒΆ
Marshal JSON as a string.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { res := jsony.EncodeString(jsony.Object{}) fmt.Printf("%v\n", res) }
Output: {}
func SafeString ΒΆ
func SafeString(s safeString) safeString
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"name", jsony.SafeString("johny")}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"name":"johny"}
Types ΒΆ
type Array ΒΆ
type Array[T Encoder] []T
Array of elements of the same type.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Array[jsony.Int]{ jsony.Int(13), jsony.Int(14), jsony.Int(15), } res := jsony.EncodeString(user) fmt.Println(res) }
Output: [13,14,15]
type Bool ΒΆ
type Bool bool
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"admin", jsony.Bool(true)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"admin":true}
type Bytes ΒΆ
type Bytes struct {
// contains filtered or unexported fields
}
Bytes is a slice of bytes that wraps [append] to require no assignment.
type Encoder ΒΆ
type Encoder interface {
EncodeJSON(*Bytes)
}
Encoder is an interface describing objects that can be serialized to JSON.
func FromMarshaller ΒΆ
func FromMarshaller(m marshaller) Encoder
FromMarshaller wraps an [json.Marshaller] to make it Encoder.
func PBool ΒΆ
PBool is a pointer version of Bool.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Bool.
func PFloat32 ΒΆ
PFloat32 is a pointer version of Float32.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Float32.
func PFloat64 ΒΆ
PFloat64 is a pointer version of Float64.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Float64.
func PInt ΒΆ
PInt is a pointer version of Int.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Int.
func PInt16 ΒΆ
PInt16 is a pointer version of Int16.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Int16.
func PInt32 ΒΆ
PInt32 is a pointer version of Int32.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Int32.
func PInt64 ΒΆ
PInt64 is a pointer version of Int64.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Int64.
func PInt8 ΒΆ
PInt8 is a pointer version of Int8.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as Int8.
func PString ΒΆ
PString is a pointer version of String.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as String.
func PUInt ΒΆ
PUInt is a pointer version of UInt.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as UInt.
func PUInt16 ΒΆ
PUInt16 is a pointer version of UInt16.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as UInt16.
func PUInt32 ΒΆ
PUInt32 is a pointer version of UInt32.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as UInt32.
func PUInt64 ΒΆ
PUInt64 is a pointer version of UInt64.
If the given value is nil, it will be serialized to null. Otherwise, behaves exactly as UInt64.
type Float32 ΒΆ
type Float32 float32
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"weight", jsony.Float32(62.3)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"weight":62.3}
type Float64 ΒΆ
type Float64 float64
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"weight", jsony.Float64(62.3)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"weight":62.3}
type Int ΒΆ
type Int int
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.Int(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type Int16 ΒΆ
type Int16 int16
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.Int16(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type Int32 ΒΆ
type Int32 int32
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.Int32(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type Int64 ΒΆ
type Int64 int64
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.Int64(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type Int8 ΒΆ
type Int8 int8
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.Int8(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type Map ΒΆ
An object with dynamically set elements.
The order of items is non-deterministic. If the order is important, use UnsafeObject.
Unlike UnsafeObject, Map guarantees that there is only one element for each key.
Make sure the keys are either String or SafeString. JSON doesn't support non-string keys.
type Marshaller ΒΆ
type Marshaller struct {
Encoder
}
Marshaller wraps an Encoder to make it [json.Marshaller].
Might be useful if you, for some reason, want to mix json with stdlib json.
func (Marshaller) MarshalJSON ΒΆ
func (m Marshaller) MarshalJSON() ([]byte, error)
type MixedArray ΒΆ
Array where every element can be of different type.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.MixedArray{ jsony.String("johny"), jsony.Int(14), jsony.Null, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: ["johny",14,null]
type Object ΒΆ
type Object []Field
An object with fixed keys.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"name", jsony.String("aragorn")}, jsony.Field{"age", jsony.Int(87)}, jsony.Field{"admin", jsony.Bool(true)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"name":"aragorn","age":87,"admin":true}
type String ΒΆ
type String string
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"name", jsony.String("johny")}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"name":"johny"}
type UInt ΒΆ
type UInt uint
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UInt(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UInt16 ΒΆ
type UInt16 uint16
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UInt16(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UInt32 ΒΆ
type UInt32 uint32
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UInt32(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UInt64 ΒΆ
type UInt64 uint64
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UInt64(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UInt8 ΒΆ
type UInt8 uint8
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UInt8(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UIntPtr ΒΆ
type UIntPtr uintptr
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/jsony" ) func main() { user := jsony.Object{ jsony.Field{"age", jsony.UIntPtr(123)}, } res := jsony.EncodeString(user) fmt.Println(res) }
Output: {"age":123}
type UnsafeField ΒΆ added in v1.1.0
A key-value pair, an UnsafeObject's field.
type UnsafeObject ΒΆ added in v1.1.0
type UnsafeObject []UnsafeField
An object with dynamically set elements.
Unlike Object, kays don't have to be a string literal. And unlike Map, the order of elements is preserved.
Make sure the keys are either String or SafeString. JSON doesn't support non-string keys.
func (UnsafeObject) EncodeJSON ΒΆ added in v1.1.0
func (v UnsafeObject) EncodeJSON(w *Bytes)
EncodeJSON implements Encoder.
func (UnsafeObject) With ΒΆ added in v1.1.0
func (v UnsafeObject) With(fs ...UnsafeField) UnsafeObject
Create a copy of the object with the given fields added to the end.