Documentation
¶
Overview ¶
Package pretty contains functions and types to support encoding to JSON and SEN formats is a more pleasing arrangement that either flat or simply indented formats. Options are available to controlling the suggested maximum width to stay within and the maximum nested depth on a single line. An alignment option is also available.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSON ¶
JSON encoded output. Arguments can be used to set the writer options. An int sets the width while a float64 is separated into a width as the integer portion of the float and the 10ths sets the maximum depth per line. A bool sets the align option and a *ojg.Options replaces the options portion of the writer.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg/pretty" "github.com/ohler55/ojg/sen" ) func main() { val := sen.MustParse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) // Pretty JSON format with a edge of 80 characters and a max depth of 2 per line. s := pretty.JSON(val, 80.2) fmt.Println(s) }
Output: [ true, false, [3, 2, 1], { "a": 1, "b": 2, "c": 3, "d": ["x", "y", "z", []] } ]
func SEN ¶
SEN encoded output. Arguments can be used to set the writer options. An int sets the width while a float64 is separated into a width as the integer portion of the float and the 10ths sets the maximum depth per line. A bool sets the align option and a *ojg.Options replaces the options portion of the writer.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg/pretty" "github.com/ohler55/ojg/sen" ) func main() { val := sen.MustParse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) // Pretty SEN format with a edge of 80 characters and a max depth of 2 per line. s := pretty.SEN(val, 80.2) fmt.Println(s) }
Output: [ true false [3 2 1] { a: 1 b: 2 c: 3 d: [x y z []] } ]
func WriteJSON ¶
WriteJSON encoded output written to the provided io.Writer. Arguments can be used to set the writer options. An int sets the width while a float64 is separated into a width as the integer portion of the float and the 10ths sets the maximum depth per line. A bool sets the align option and a *ojg.Options replaces the options portion of the writer.
Example ¶
package main import ( "fmt" "strings" "github.com/ohler55/ojg/pretty" "github.com/ohler55/ojg/sen" ) func main() { val := sen.MustParse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) var buf strings.Builder // Pretty JSON format with a edge of 80 characters and a max depth of 2 per line. if err := pretty.WriteJSON(&buf, val, 80.2); err != nil { panic(err) } fmt.Println(buf.String()) }
Output: [ true, false, [3, 2, 1], { "a": 1, "b": 2, "c": 3, "d": ["x", "y", "z", []] } ]
func WriteSEN ¶
WriteSEN encoded output written to the provided io.Writer. Arguments can be used to set the writer options. An int sets the width while a float64 is separated into a width as the integer portion of the float and the 10ths sets the maximum depth per line. A bool sets the align option and a *ojg.Options replaces the options portion of the writer.
Example ¶
package main import ( "fmt" "strings" "github.com/ohler55/ojg/pretty" "github.com/ohler55/ojg/sen" ) func main() { val := sen.MustParse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) var buf strings.Builder // Pretty SEN format with a edge of 80 characters and a max depth of 2 per line. if err := pretty.WriteSEN(&buf, val, 80.2); err != nil { panic(err) } fmt.Println(buf.String()) }
Output: [ true false [3 2 1] { a: 1 b: 2 c: 3 d: [x y z []] } ]
Types ¶
type Writer ¶ added in v1.8.0
type Writer struct { ojg.Options // Width is the suggested maximum width. In some cases it may not be // possible to stay within the specified width. Width int // MaxDepth is the maximum depth of an element on a single line. MaxDepth int // Align if true attempts to align elements of children in list. Align bool // SEN format if true otherwise JSON encoding. SEN bool // contains filtered or unexported fields }
Writer writes data in either JSON or SEN format using setting to determine the output.
func (*Writer) Encode ¶ added in v1.8.0
Encode data. Any panics during encoding will cause an empty return but will not fail.The returned buffer is the Writer buffer and is reused on the next call to write. If returned value is to be preserved past a second invocation then the buffer should be copied.
func (*Writer) Marshal ¶ added in v1.8.0
Marshal data. The same as Encode but a panics during encoding will result in an error return.