JSON with comments for Go!

jsonc

JSON with comments for Go!
travisci

JSONC is a superset of JSON which supports comments. JSON formatted files are readable to humans but the lack of comments decreases readability. With JSONC, you can use block (/* */) and single line (//) comments to describe the functionality. Microsoft VS Code also uses this format in their configuration files like settings.json, keybindings.json, launch.json, etc.

jsonc

What this package offers

JSONC for Go offers ability to convert and unmarshal JSONC to pure JSON. It also provides functionality to read JSONC file from disk and return JSONC and corresponding JSON encoding to operate on. However, it only provides a one way conversion. That is, you can not generate JSONC from JSON. Read documentation for detailed examples.

Usage

go get it

Run go get command to install the package.

$ go get muzzammil.xyz/jsonc

Import jsonc

Import muzzammil.xyz/jsonc to your source file.

package main

import (
  "fmt"

  "muzzammil.xyz/jsonc"
)

Test it

Now test it!

func main() {
  j := []byte(`{"foo": /*comment*/ "bar"}`)
  jc := jsonc.ToJSON(j) // Calling jsonc.ToJSON() to convert JSONC to JSON
  if jsonc.Valid(jc) {
    fmt.Println(string(jc))
  } else {
    fmt.Println("Invalid JSONC")
  }
}
$ go run app.go
{"foo":"bar"}

Contributions

Contributions are welcome but kindly follow the Code of Conduct and guidelines. Please don't make Pull Requests for typographical errors, grammatical mistakes, "sane way" of doing it, etc. Open an issue for it. Thanks!

Owner
Muhammad Muzzammil
I code stuff.
Muhammad Muzzammil
Comments
  • fail to parse json file which contains character   \

    fail to parse json file which contains character \"

    
    // written by mastiff automatically, please don't edit this file
    
    {
      "pluginId": "osquery",
      "enable": true,
      "type": "pluginConfig",
      "hash": "6c3a3f6a22161912facd77a75d0e6346",
      "config": {
        "rules": [
          {
            "content": "{\n  \"query\": \"SELECT p.pid, p.name, p.cmdline, p.path FROM processes p JOIN process_open_files pf USING(pid) WHERE pf.path = '/dev/tty' AND p.cmdline LIKE '%sh -i';\",\n  \"interval\": 600,\n  \"descirption\": \"通过 fifo 管道实现反弹 shell(间接重定向)\"\n}",
            "ruleId": "reverse_shell_with_fifo_pipe_tty"
          },
          {
            "content": "{\n  \"query\": \"SELECT pid, name, cmdline, path as pipepath FROM (SELECT p.pid, p.name, p.cmdline, pf.path FROM process_open_files pf JOIN processes p USING(pid) WHERE p.name IN ('bash', 'sh')) bf JOIN file USING(path) WHERE type = 'fifo';\",\n  \"interval\": 600,\n  \"descirption\": \"通过 fifo 管道实现反弹 shell(直接重定向)\"\n}",
            "ruleId": "reverse_shell_with_fifo_pipe"
          },
          {
            "content": "{\n    \"query\":\"SELECT p.pid, p.name, p.cmdline, p.path FROM processes p JOIN process_open_sockets s ON p.pid = s.pid WHERE p.pid = (SELECT parent FROM processes WHERE name = 'sh') AND s.state = 'ESTABLISHED' AND p.cmdline LIKE 'python -c exec(%';\",\n    \"interval\":600,\n    \"descirption\":\"通过 socket 实现反弹 shell(Python)\"\n}",
            "ruleId": "reverse_shell_with_socket_python"
          }
        ]
      }
    }
    
    
  • fail to parse json content

    fail to parse json content

    func TestJsonParse(t *testing.T) {
    	s := "{                                  " +
    		"/**                                 " +
    		"* environment: dev/test/online      " +
    		"*/                                  " +
    		"\"env\": \"dev\"                    " +
    		"}                                   "
    //	fmt.Println(s)
    	pure := string(jsonc.ToJSON([]byte(s)))
    	fmt.Println(pure)
    }
    

    output is

    {testonline"env":"dev"}
    

    the correct result should be

    {"env":"dev"}
    
  • Fix issue when single line comment is ended by block comment suffix

    Fix issue when single line comment is ended by block comment suffix

    While using the library I encountered an issue, where single line comment can be ended with */. Example:

    {
      // I commented a glob pattern like this "**/*"
      "test" : "Hello"
    }
    

    The error I got was invalid character '*' looking for beginning of value.

    I added a simple check to not try and end the comment with */ if the comment is single line.

  • Add support for hash style comments

    Add support for hash style comments

    C-style comments (// and /* */ are the most popular way to add the annotations to JSON files. Another popular style is shell comments (single-line started with hash #). This style is used in shell scripting languages (bash, zsh, etc.), Python and, Perl.

    I noticed that the users use both comment styles, but none Go library supports all of them simultaneously.

    This MR adds the possibility to handle the shell comments in your excellent library.

  • Commented escape characters end up in the json

    Commented escape characters end up in the json

    Commented escape characters are leaking into the output JSON, resulting in invalid JSON. For example, if you pass this input JSONC into ToJSON:

    {
      "test1": "string with \" escape char"
      //"test2": "string with \" escape char"
    }
    

    The output is:

    {"test1":"string with \" escape char"\"}
    

    Note the erroneous \" towards the end, which makes this invalid JSON.

  • Update: Better benchmark results for speed and allocations

    Update: Better benchmark results for speed and allocations

    1. Update: Better benchmark results for speed and allocations. Replaced append operation with index assignment, reducing allocations. Also used single sate to manage operations.
  • should not remove the comments in texts with quotes

    should not remove the comments in texts with quotes

    ToJSON([]byte(`"url": "https://github.com"}`))
    

    expected:

    {"url":"https://github.com"}
    

    actual:

    {"url":"https:
    
  • Mode for replacing comments with spaces and leaving original whitespace in

    Mode for replacing comments with spaces and leaving original whitespace in

    While working with this library I noticed an issue when loading user-created JSON. The offsets of the errors in JSON is completely broken in case of syntax errors.

    Example (from my memory)

    Input JSON:

    {
      //this is a comment
      "field": "value",
    }
    

    In this case, the translation will remove comment and all unneeded whitespace and will result in something like this (not actual output)

    {"field":"value",}
    

    When unmarshalling it, it will throw an error about trailing comma (actually about expecting a property, but it's caused by trailing comma) with offset of around 16. When applying this offset to the original JSON, the offset is completely wrong and the user cannot easily find it.

    To fix it I left all whitespace in + replaced all comments with spaces in my fork of this library. https://github.com/stirante/jsonc/commit/c4ec34866fdd58b94c38db497febe30286f99e51

    I understand that not many people might want this behavior which is why I propose to add it as a separate setting/mode.

  • Ignore quotes inside comments

    Ignore quotes inside comments

    Added a small fix, that will ignore quotes inside comments. Previously, this JSON would break:

    {
      // This one quote "
      "makes": "everything else broken"
    }
    

    Additionally added to tests my previous fix, that was ending single line comments with **/ and current issue with quotes.

Get JSON values quickly - JSON parser for Go
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Dec 28, 2022
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

Dec 4, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Jan 6, 2023
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

Jun 26, 2022
Json-go - CLI to convert JSON to go and vice versa
Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Jul 29, 2022
JSON Spanner - A Go package that provides a fast and simple way to filter or transform a json document

JSON SPANNER JSON Spanner is a Go package that provides a fast and simple way to

Sep 14, 2022
Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Jan 5, 2023
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Jan 5, 2023
Small utility to create JSON objects
Small utility to create JSON objects

gjo Small utility to create JSON objects. This was inspired by jpmens/jo. Support OS Mac Linux Windows Requirements Go 1.1.14~ Git Installtion Build $

Dec 8, 2022
A Go package for handling common HTTP JSON responses.

go-respond A Go package for handling common HTTP JSON responses. Installation go get github.com/nicklaw5/go-respond Usage The goal of go-respond is to

Sep 26, 2022
JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Dec 28, 2022
Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Jan 1, 2023
A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Dec 11, 2022
Fast and flexible JSON encoder for Go
Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

Dec 21, 2022
Create go type representation from json

json2go Package json2go provides utilities for creating go type representation from json inputs. Json2go can be used in various ways: CLI tool Web pag

Dec 26, 2022
Console JSON formatter with query feature
Console JSON formatter with query feature

Console JSON formatter with query feature. Install: $ go get github.com/miolini/jsonf Usage: Usage of jsonf: -c=true: colorize output -d=false: de

Dec 4, 2022
Fluent API to make it easier to create Json objects.

Jsongo Fluent API to make it easier to create Json objects. Install go get github.com/ricardolonga/jsongo Usage To create this: { "name":"Ricar

Nov 7, 2022
Arbitrary transformations of JSON in Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Dec 18, 2022
Parsing JSON is a hassle in golang

GoJSON Parsing JSON is a hassle in golang. This package will allow you to parse and search elements in a json without structs. Install gojson go get g

Nov 12, 2021