package module
Version: v3.1.0+incompatible Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 11 Imported by: 38



Build Status Go Pkg Go Report Card coverage godoc

Hjson Intro

  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!

The Go implementation of Hjson is based on hjson-js. For other platforms see hjson.github.io.


Make sure you have a working Go environment. See the install instructions.

  1. Get the sources
$ go get -u github.com/hjson/hjson-go
  1. Build the hjson-cli commandline tool (optional)
$ cd $(go env GOPATH)/src/github.com/hjson/hjson-go/hjson-cli && go install
$ hjson-cli --version

Usage as command line tool

usage: hjson-cli [OPTIONS] [INPUT]
hjson can be used to convert JSON from/to Hjson.

hjson will read the given JSON/Hjson input file or read from stdin.

      Allow -0.
      Print braces on the same line.
  -c  Output as JSON.
  -h  Show this screen.
  -indentBy string
      The indent string. (default "  ")
  -j  Output as formatted JSON.
      Omit braces at the root.
      Always quote string values.


  • run hjson-cli test.json > test.hjson to convert to Hjson
  • run hjson-cli -j test.hjson > test.json to convert to JSON

Usage as a GO library

package main

import (

func main() {

    // Now let's look at decoding Hjson data into Go
    // values.
    sampleText := []byte(`
        # specify rate in requests/second
        rate: 1000

    // We need to provide a variable where Hjson
    // can put the decoded data.
    var dat map[string]interface{}

    // Decode and a check for errors.
    if err := hjson.Unmarshal(sampleText, &dat); err != nil {

    // In order to use the values in the decoded map,
    // we'll need to cast them to their appropriate type.

    rate := dat["rate"].(float64)

    array := dat["array"].([]interface{})
    str1 := array[0].(string)

    // To encode to Hjson with default options:
    sampleMap := map[string]int{"apple": 5, "lettuce": 7}
    hjson, _ := hjson.Marshal(sampleMap)
    // this is short for:
    // options := hjson.DefaultOptions()
    // hjson, _ := hjson.MarshalWithOptions(sampleMap, options)

If you prefer, you can also unmarshal to Go objects by converting to JSON:

package main

import (

type Sample struct {
    Rate  int
    Array []string

func main() {

    sampleText := []byte(`
        # specify rate in requests/second
        rate: 1000

    // read Hjson
    var dat map[string]interface{}
    hjson.Unmarshal(sampleText, &dat)

    // convert to JSON
    b, _ := json.Marshal(dat)

    // unmarshal
    var sample Sample
    json.Unmarshal(b, &sample)





see releases




This section is empty.


This section is empty.


func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the Hjson encoding of v using default options.

See MarshalWithOptions.

func MarshalWithOptions

func MarshalWithOptions(v interface{}, options EncoderOptions) ([]byte, error)

MarshalWithOptions returns the Hjson encoding of v.

Marshal traverses the value v recursively.

Boolean values encode as JSON booleans.

Floating point, integer, and Number values encode as JSON numbers.

String values encode as Hjson strings (quoteless, multiline or JSON).

Array and slice values encode as JSON arrays.

Map values encode as JSON objects. The map's key type must be a string. The map keys are sorted and used as JSON object keys.

Pointer values encode as the value pointed to. A nil pointer encodes as the null JSON value.

Interface values encode as the value contained in the interface. A nil interface value encodes as the null JSON value.

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.

func Unmarshal

func Unmarshal(data []byte, v interface{}) (err error)

Unmarshal parses the Hjson-encoded data and stores the result in the value pointed to by v.

Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary.


type EncoderOptions

type EncoderOptions struct {
	// End of line, should be either \n or \r\n
	Eol string
	// Place braces on the same line
	BracesSameLine bool
	// Deprecated: Hjson always emits braces
	EmitRootBraces bool
	// Always place string in quotes
	QuoteAlways bool
	// Indent string
	IndentBy string
	// Allow the -0 value (unlike ES6)
	AllowMinusZero bool
	// Encode unknown values as 'null'
	UnknownAsNull bool

EncoderOptions defines options for encoding to Hjson.

func DefaultOptions

func DefaultOptions() EncoderOptions

DefaultOptions returns the default encoding options.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL