JSON with comments for Go!

Overview

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!

Issues
  • 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"
          }
        ]
      }
    }
    
    
    opened by jekkay 5
  • 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"}
    
    opened by jekkay 2
  • 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.

    opened by stirante 1
  • 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.

    enhancement 
    opened by fivitti 1
  • 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.

    bug 
    opened by TravisTX 1
  • 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.
    opened by akshaybharambe14 1
  • 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:
    
    bug 
    opened by blackLearning 1
Releases(v1)
  • v1(Mar 25, 2022)

    The Why

    A number of fixes and enhancements have been introduced since inception so I felt the need to formally release the package under systematic versioning to make it manageable. So, this is the first formal release of the jsonc package for golang.

    A big thanks to the contributors!

    What's Changed

    • fix issue with commented escape characters by @TravisTX in https://github.com/muhammadmuzzammil1998/jsonc/pull/11
    • Add support for hash style comments by @fivitti in https://github.com/muhammadmuzzammil1998/jsonc/pull/12
    • Shift to GitHub Actions by @muhammadmuzzammil1998 in https://github.com/muhammadmuzzammil1998/jsonc/pull/13
    • Fix issue when single line comment is ended by block comment suffix by @stirante in https://github.com/muhammadmuzzammil1998/jsonc/pull/14
    • Restructure the repository by @muhammadmuzzammil1998 in https://github.com/muhammadmuzzammil1998/jsonc/pull/15

    What's Fixed

    • #1 by @blackLearning
    • #5 by @nojus297
    • #6 and #7 by @jekkay
    • #10 by @TravisTX

    Enhancements and Requests

    • #4 by @akshaybharambe14
    • #9 by @arulrajnet

    New Contributors

    • @TravisTX made their contribution in https://github.com/muhammadmuzzammil1998/jsonc/pull/11 for #10
    • @fivitti made their contribution in https://github.com/muhammadmuzzammil1998/jsonc/pull/12
    • @stirante made their contribution in https://github.com/muhammadmuzzammil1998/jsonc/pull/14

    Full Changelog: https://github.com/muhammadmuzzammil1998/jsonc/commits/v1

    Source code(tar.gz)
    Source code(zip)
Owner
Muhammad Muzzammil
I code stuff.
Muhammad Muzzammil
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

Josh Baker 10.5k Jun 30, 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

William Poussier 172 Jun 23, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go

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

Masaaki Goshima 1.6k Jun 26, 2022
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

High Performance, Kubernetes Native Object Storage 2 Jun 26, 2022
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

Samit Ghimire 5 Mar 3, 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

null 3 Jun 30, 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

Stepan Pyzhov 119 Jun 24, 2022
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

Aliaksandr Valialkin 1.6k Jun 21, 2022
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 $

skanehira 108 Apr 27, 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

Nick Law 47 May 18, 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

Qian Chen 182 Apr 27, 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

Aditya Mukerjee 2.5k Jun 23, 2022
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

Guillaume de Sagazan 87 Jun 23, 2022
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

William Poussier 128 Jun 1, 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

Maciek Zając 100 May 10, 2022
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

Artem Andreenko 63 Jan 23, 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

Ricardo Longa 102 May 22, 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

Qntfy 230 Jun 17, 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

swaraj18 25 Nov 12, 2021