jsoncjson

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 3 Imported by: 1

README

JSONcJSON

Version Build Status Go Report Card Coverage Status PkgGoDev

The library provides a JSONC (json with comments) to JSON streamer. It supports multiline comments ( /* Comment */) and one-line comments ( // Comment ).

For example, it translates JSON with comments:

{
    /*
        JSONcJSON
        =^._.^= ∫
    */
    "Hello": "world" // In-line comments are also supported.
}

to a valid JSON:

{
    "Hello": "world"
}

Installing:

go get github.com/hedhyw/jsoncjson

Usage example:

More examples.

// Converting jsonc to json and decoding.

const in = `
{
    "Hello": "world"
    /* Perhaps the truth depends on a walk around the lake. */
}
`

// The reader can be anything.
// For example: file, strings.NewReader(), bytes.NewReader(), ...
var r = jsoncjson.NewReader(strings.NewReader(in))

var data map[string]interface{}
_, = json.NewDecoder(r).Decode(&data)

fmt.Printf("%+v\n", data) // map[Hello:world].

Documentation

Overview

Package jsoncjson provides JSONC (JSON with comment) reader that removes all comments from the input.

For example following input:

{ /* Comment. */
	"Hello": "World" // Comment.
}

Produces:

{
	"Hello": "World"
}
Example (JsoncDecode)

This example demonstrates decoding of JSON with comments input with a standart "encoding/json" decoder to a map[string]interface{} structure.

package main

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/hedhyw/jsoncjson"
)

func main() {
	const jsoncInStr = `
		{
			// A "Hello, World!" program generally is a computer program
			// that outputs or displays the message "Hello, World!".
			
			"Hello": "World"
		}
	`

	var r = jsoncjson.NewReader(strings.NewReader(jsoncInStr))

	var data map[string]interface{}
	_ = json.NewDecoder(r).Decode(&data)

	fmt.Printf("%+v", data)
}
Output:

map[Hello:World]
Example (JsoncFromBytes)

This example demonstrates reading JSON with comments from the bytes and printing then JSON without comments output.

package main

import (
	"bytes"
	"fmt"

	"github.com/hedhyw/jsoncjson"
)

func main() {
	var in = []byte(`
	{/*
		"If you cannot do great things, do small things in a great way."
		
		- Napoleon Hill

	 */ "Hello": "World" }
	`)

	var r = jsoncjson.NewReader(bytes.NewReader(in))

	var buf bytes.Buffer
	_, _ = buf.ReadFrom(r)

	fmt.Printf("%s", buf.String())
}
Output:

{ "Hello": "World" }
Example (JsoncFromFile)

This example demonstrates reading JSON with comments from the file and decoding with a standart "encoding/json" decoder to a map[string]interface{} structure.

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/hedhyw/jsoncjson"
)

func main() {
	const exampleFile = "./example.jsonc"

	var f, err = os.Open(exampleFile)
	if err != nil {
		log.Fatalf("openning: %s: %s", exampleFile, err)
	}
	defer f.Close()

	var r = jsoncjson.NewReader(f)

	var data map[string]interface{}
	err = json.NewDecoder(r).Decode(&data)
	if err != nil {
		log.Fatalf("decoding: %s", err)
	}

	fmt.Printf("%+v\n", data)
}
Output:

map[Hello:world]
Example (JsoncFromString)

This example demonstrates reading JSON with comments from the string and printing then JSON without comments output.

package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/hedhyw/jsoncjson"
)

func main() {
	var in = strings.ReplaceAll(`
		{
			// Between saying and doing, many a pair of shoes is worn out.
			"Hello": "World"
		}
	`, "\t", "")

	var r = jsoncjson.NewReader(strings.NewReader(in))

	var buf bytes.Buffer
	_, _ = buf.ReadFrom(r)

	fmt.Printf("%s", buf.String())
}
Output:

{
"Hello": "World"
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReader

func NewReader(r io.Reader) io.Reader

NewReader creates new reader that removes all comments from a jsonc input and returns pure json.

Types

This section is empty.

Jump to

Keyboard shortcuts

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