fixjson

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 3 Imported by: 1

README

fixjson

GoDoc

fixjson is a Go package that converts broken, nonstandard or nonofficial format (e.g. jsonc) to standard json. As an author of this package, I kindly ask you, stop doing that shit and use standard json whenever possible.

The features of the package are:

  • remove comments, single line (// text) or multiline (/* text */)
  • remove trailing commas, from arrays and objects
  • add missing commas after string literals at the end of a line in arrays and objects
  • remove unnecessary and invalid escape character of single quotes (')
  • escape quotes " in string literals, if they are placed not at the end of a line
  • fixes html encoded quotes in json
  • replace new line (\n) characters in string literals with \\ following by n
  • replace tabs (\t) characters with \\ following by t
{
  /* Example block                                  // multiline comment
   comment */
  "propertyName": {
    "name": "No, you can\'t do that in json",       // invalid escape character
    "value": 5432, // counter                       // inline comment           
    "username": "josh"                              // missing comma
    "password": "pass123",                          // trailing comma
  },

  "title": "Hello                                   // new line in string literal
    World",
}

Getting Started

Installing
$ go get -u github.com/astappiev/fixjson

This will retrieve the library.

Example

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

data := `
{
  /* Example block
   comment */
  "propertyName": {
    "name": "No, you can\'t do that in json",
    "value": 5432, // counter   
    "username": "josh"
    "password": "pass123",
  },

  "title": "Hello
    World",
}
`

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

Documentation

Index

Constants

View Source
const (
	TAB          = 9   // \t
	NEWLINE      = 10  // \n
	RETURN       = 13  // \r
	SPACE        = 32  //
	QUOTE        = 34  // "
	HASH         = 35  // #
	APOSTROPHE   = 39  // "
	AMPERSAND    = 38  // &
	ASTERISK     = 42  // *
	COMMA        = 44  // ,
	SLASH        = 47  // /
	COLON        = 58  // :
	ESCAPE       = 92  // \
	LEFT_SQUARE  = 91  // [
	RIGHT_SQUARE = 93  // ]
	LEFT_CURLY   = 123 // {
	RIGHT_CURLY  = 125 // }
)

Variables

This section is empty.

Functions

func FallbackUnmarshal

func FallbackUnmarshal(data []byte, v any) error

FallbackUnmarshal tries to unmarshal the data as valid JSON, if that fails, it tries to fix it and unmarshal fixed

func ToJSON

func ToJSON(src []byte) []byte

ToJSON fixes the input to a valid JSON

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal tries to fix the JSON and then unmarshal it

Types

This section is empty.

Jump to

Keyboard shortcuts

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