Git Product home page Git Product logo

francispatron / betterjson Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 20 KB

Code without comments is like a detective novel without clues—frustrating and often indecipherable. With betterjson, you can finally annotate your JSON files with all the witty remarks and crucial explanations they deserve. After all, clarity is king, and who doesn't love a well-commented piece of code?

License: MIT License

Go 100.00%
comments hjson json json5 jsonc readable-code

betterjson's Introduction

Better Json

Go CI Release

Better Json is a Go library that enhances JSON parsing by supporting comments within JSON files.

Features

  • Comment Support: Easily parse JSON files with single-line and multi-line comments.
  • Simple API: Uses a familiar API similar to the standard encoding/json package.

Usage

Here's a basic example of how to use betterjson:

package main

import (
	"fmt"
	"log"
	"os"
	"github.com/FrancisPatron/betterjson"
)

// ... [Your struct definitions here]

func main() {
	// Read example.json
	data, err := os.ReadFile("example.json")
	if err != nil {
		log.Fatalf("Failed to read example.json: %v", err)
	}

	var example ExampleData
	err = betterjson.Unmarshal(data, &example)
	if err != nil {
		log.Fatalf("Failed to parse example.json: %v", err)
	}

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

for a complete example checkout this example code

How It Works

betterjson offers a streamlined approach to parsing JSON files with comments. At its core, it employs a single, two-step function. First, the uncommenter strips away comments from the JSON content. Instead of simply removing comments, it replaces them with whitespaces, ensuring that the original structure and line count remain intact. This replacement is crucial, as it preserves the accuracy of line and column numbers. Once the comments are replaced, the standard json.Unmarshal function takes over, parsing the now-comment-free JSON. This approach ensures that any errors reported by json.Unmarshal accurately reflect the correct line and column numbers from the original file, providing precise feedback even in the presence of comments.

Instalation

To install betterjson, use go get:

 go get github.com/FrancisPatron/betterjson

betterjson's People

Contributors

francispatron avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

betterjson's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.