jsonc

package
v0.0.0-...-642df0c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause, MIT Imports: 0 Imported by: 0

README

jsonc

GoDoc

jsonc is a Go package that converts the jsonc format to standard json.

The jsonc format is like standard json but allows for comments and trailing commas, such as:

{

  /* Dev Machine */
  "dbInfo": {
    "host": "localhost",
    "port": 5432,          
    "username": "josh",
    "password": "pass123", // please use a hashed password
  },

  /* Only SMTP Allowed */
  "emailInfo": {
    "email": "[email protected]", // use full email address
    "password": "pass123",
    "smtp": "smpt.example.com",
  }

}

There's a provided function jsonc.ToJSON, which does the conversion.

The resulting JSON will always be the same length as the input and it will include all of the same line breaks at matching offsets. This is to ensure the result can be later processed by a external parser and that that parser will report messages or errors with the correct offsets.

Getting Started

Installing

To start using jsonc, install Go and run go get:

$ go get -u github.com/tidwall/jsonc

This will retrieve the library.

Example

The following example uses a JSON document that has comments and trailing commas and converts it just prior to unmarshalling with the standard Go JSON library.


data := `
{
  /* Dev Machine */
  "dbInfo": {
    "host": "localhost",
    "port": 5432,          // use full email address
    "username": "josh",
    "password": "pass123", // use a hashed password
  },
  /* Only SMTP Allowed */
  "emailInfo": {
    "email": "[email protected]",
    "password": "pass123",
    "smtp": "smpt.example.com",
  }
}
`

err := json.Unmarshal(jsonc.ToJSON(data), &config)

Performance

It's fast and can convert GB/s of jsonc to json.

Contact

Josh Baker @tidwall

License

jsonc source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToJSON

func ToJSON(src []byte) []byte

ToJSON strips out comments and trailing commas and convert the input to a valid JSON per the official spec: https://tools.ietf.org/html/rfc8259

The resulting JSON will always be the same length as the input and it will include all of the same line breaks at matching offsets. This is to ensure the result can be later processed by a external parser and that that parser will report messages or errors with the correct offsets.

func ToJSONInPlace

func ToJSONInPlace(src []byte) []byte

ToJSONInPlace is the same as ToJSON, but this method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.

Types

This section is empty.

Jump to

Keyboard shortcuts

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