Documentation
¶
Overview ¶
Package gen provides type safe generic types. They are type safe in that array and objects can only be constructed of other types in the package. The basic types are:
Bool Int Float String Time
The collection types are Array and Object. All the types implement the Node interface which is relatively simple interface defined primarily to restrict what can be in the collection types. The Node interface should not be used to define new generic types.
Also included in the package are a builder and parser that behave like the parser and builder in the oj package except for gen types.
Index ¶
- Constants
- Variables
- type Array
- type Big
- type Bool
- type Builder
- func (b *Builder) Array(key ...string) error
- func (b *Builder) MustArray(key ...string)
- func (b *Builder) MustObject(key ...string)
- func (b *Builder) MustValue(value Node, key ...string)
- func (b *Builder) Object(key ...string) error
- func (b *Builder) Pop()
- func (b *Builder) PopAll()
- func (b *Builder) Reset()
- func (b *Builder) Result() (result Node)
- func (b *Builder) Value(value Node, key ...string) error
- type Float
- type Int
- type Key
- type Node
- type Number
- type Object
- type ParseError
- type Parser
- type String
- type Time
Examples ¶
Constants ¶
const ( // BigLimit is the limit before a number is converted into a Big // instance. (9223372036854775807 / 10 = 922337203685477580) BigLimit = math.MaxInt64 / 10 // DivLimit is the divisor limit before a number is converted into a Big // instance. (nearest multiple of 10 below max int64) DivLimit = 1000000000000000000 )
Variables ¶
var EmptyArray = Array{}
EmptyArray is a array of nodes of zero length.
var False = Bool(false)
False is a false boolean value.
var Sort = false
Sort if true sorts Object keys on output.
var TimeFormat = ""
TimeFormat defines how time is encoded. Options are to use a time. layout string format such as time.RFC3339Nano, "second" for a decimal representation, "nano" for a an integer.
var TimeWrap = ""
TimeWrap if not empty encoded time as an object with a single member. For example if set to "@" then and TimeFormat is RFC3339Nano then the encoded time will look like '{"@":"2020-04-12T16:34:04.123456789Z"}'
var True = Bool(true)
True is a true boolean value.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array []Node
Array represents an array of nodes.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is assists in build a more complex Node.
Example ¶
package main
import (
"fmt"
"github.com/ohler55/ojg/gen"
"github.com/ohler55/ojg/sen"
)
func main() {
var b gen.Builder
b.MustObject()
b.MustArray("a")
b.MustValue(gen.True)
b.MustObject()
b.MustValue(gen.Int(123), "x")
b.Pop()
b.MustValue(nil)
b.PopAll()
v := b.Result()
fmt.Println(sen.String(v))
}
Output: {a:[true {x:123}null]}
func (*Builder) Array ¶
Array adds an array to the builder. A key is required if adding to a parent object.
func (*Builder) MustArray ¶ added in v1.11.0
MustArray adds an array to the builder. A key is required if adding to a parent object.
func (*Builder) MustObject ¶ added in v1.11.0
MustObject adds an object to the builder. A key is required if adding to a parent object.
func (*Builder) MustValue ¶ added in v1.11.0
MustValue adds a Node to the builder. A key is required if adding to a parent object.
func (*Builder) Object ¶
Object adds an object to the builder. A key is required if adding to a parent object.
func (*Builder) Reset ¶
func (b *Builder) Reset()
Reset clears the the Builder of previous built nodes.
type Float ¶
type Float float64
Float is a float64 Node.
type Int ¶
type Int int64
Int is a int64 Node.
type Key ¶
type Key string
Key use for parsing.
func (Key) Alter ¶
Alter converts the node into it's native type. Note this will modify Objects and Arrays in place making them no longer usable as the original type. Use with care!
type Node ¶
type Node interface {
fmt.Stringer
// Alter converts the node into it's native type. Note this will modify
// Objects and Arrays in place making them no longer usable as the
// original type. Use with care!
Alter() any
// Simplify makes a copy of the node but as simple types.
Simplify() any
// Dup returns a deep duplicate of the node.
Dup() Node
// Empty returns true if the node is empty.
Empty() bool
}
Node is the interface for typed generic data.
type Number ¶ added in v1.1.0
type Number struct {
I uint64
Frac uint64
Div uint64
Exp uint64
Neg bool
NegExp bool
BigBuf []byte
Conv ojg.NumConvMethod
ForceFloat bool
}
Number is used internally by parsers.
type Object ¶
Object is a map of Nodes with string keys.
type ParseError ¶
ParseError represents a parse error.
func (*ParseError) Error ¶
func (err *ParseError) Error() string
Error returns a string representation of the error.
type Parser ¶
type Parser struct {
// OnlyOne returns an error if more than one JSON is in the string or stream.
OnlyOne bool
// Reuse maps. Previously returned maps will no longer be valid or rather
// could be modified during parsing.
Reuse bool
// contains filtered or unexported fields
}
Parser is a reusable JSON parser. It can be reused for multiple parsings which allows buffer reuse for a performance advantage.
func (*Parser) Parse ¶
Parse a JSON string in to simple types. An error is returned if not valid JSON.
Example ¶
package main
import (
"fmt"
"github.com/ohler55/ojg/gen"
"github.com/ohler55/ojg/oj"
)
func main() {
// The parser can be reused for better performance by reusing buffers.
var p gen.Parser
v, err := p.Parse([]byte(`{"a": 1, "b":[2,3,4]}`))
if err == nil {
// Sorted output allows for consistent results.
fmt.Println(oj.JSON(v, &oj.Options{Sort: true}))
fmt.Printf("type: %T\n", v)
} else {
fmt.Println(err.Error())
}
}
Output: {"a":1,"b":[2,3,4]} type: gen.Object
type String ¶
type String string
String is a string Node.