Movatterモバイル変換


[0]ホーム

URL:


stripjsoncomments

packagemodule
v1.0.11Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License:MITImports:1Imported by:5

Details

Repository

github.com/trapcodeio/go-strip-json-comments

Links

README

go-strip-json-comments

github |pkg.go.dev

Note:

This is an exact port of Sindresorhus Nodejsstrip-json-comments toGoLang.

Strip comments from JSON. Lets you use comments in your JSON files!

This is now possible:

{    // Rainbows    "unicorn": /* ❤ */    "cake"}

It will replace single-line comments// and multi-line comments/**/ with whitespace. This allows JSON errorpositions to remain as close as possible to the original source.

Also available as aGulp/Grunt/Broccoli plugin.

Install

go get github.com/trapcodeio/go-strip-json-comments

Usage

package mainimport ("fmt""github.com/trapcodeio/go-strip-json-comments")func main() {json := `{        // Rainbows        "unicorn": /* ❤ */ "cake"    }`fmt.Println(stripjsoncomments.Strip(json))//=>{ "unicorn": "cake" }// with optionsoptions := stripjsoncomments.Options{Whitespace:     false,TrailingCommas: true,}fmt.Println(stripjsoncomments.StripWithOptions(json, &options))}
Test

Clone repo and run

go test -v

you can also view them in this repo actions tab.

Benchmark
go test -bench=. -count 5 -run=^#
goos: darwingoarch: amd64pkg: github.com/trapcodeio/go-strip-json-commentscpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHzBenchmarkWithFiles/strip_JSON_comments-16                    332           3304452 ns/opBenchmarkWithFiles/strip_JSON_comments-16                    408           3759578 ns/opBenchmarkWithFiles/strip_JSON_comments-16                    289           3634032 ns/opBenchmarkWithFiles/strip_JSON_comments-16                    297           3826366 ns/opBenchmarkWithFiles/strip_JSON_comments-16                    338           3652408 ns/opBenchmarkWithFiles/strip_JSON_comments_without_whitespace-16                 621           1747448 ns/opBenchmarkWithFiles/strip_JSON_comments_without_whitespace-16                 639           1899337 ns/opBenchmarkWithFiles/strip_JSON_comments_without_whitespace-16                 652           1891341 ns/opBenchmarkWithFiles/strip_JSON_comments_without_whitespace-16                 766           1799857 ns/opBenchmarkWithFiles/strip_JSON_comments_without_whitespace-16                 699           1867128 ns/opBenchmarkWithFiles/strip_Big_JSON_comments-16                                  9         131861237 ns/opBenchmarkWithFiles/strip_Big_JSON_comments-16                                  8         138128030 ns/opBenchmarkWithFiles/strip_Big_JSON_comments-16                                  9         140734501 ns/opBenchmarkWithFiles/strip_Big_JSON_comments-16                                  8         126000649 ns/opBenchmarkWithFiles/strip_Big_JSON_comments-16                                  8         133652308 ns/opPASSok      github.com/trapcodeio/go-strip-json-comments    24.843s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcStrip

func Strip(jsonStringstring)string

Strip - strips comments from JSON string

funcStripWithOptions

func StripWithOptions(jsonStringstring, options *Options)string

StripWithOptions - strips comments from JSON string with options

Types

typeOptions

type Options struct {WhitespaceboolTrailingCommasbool}

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp