lenientjson

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 9 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-lenientjson

Go


Turns the lenient JSON that Content Patcher and Json.NET accept (comments, trailing commas, loose numbers) into strict JSON for encoding/json. Package lenientjson, module github.com/Rethunk-Tech/go-lenientjson.

One function, Clean, takes the bytes a .NET program would hand to Json.NET (Newtonsoft.Json) and returns strict JSON that encoding/json reads to the same values. The target is Json.NET's reader, not JSONC or JSON5. A caller that needs the cleaned bytes (for json.Decoder, a line-numbered error) gets them; one that does not writes json.Unmarshal(lenientjson.Clean(b), &v).

Quick start

go get github.com/Rethunk-Tech/go-lenientjson

A worked example and the contracts: HUMANS.md.

Features

  • Reads the encoding File.ReadAllText picks from the BOM: UTF-8, UTF-16 LE/BE, UTF-32 LE/BE.
  • Strips // and /* */ comments and trailing commas; an elided array element or empty property value ([1,,2], {"o":,}) becomes null.
  • Accepts single-quoted strings, unquoted property names, .5 and 5., and raw control characters in strings.
  • NaN, Infinity, -Infinity and undefined become null, because encoding/json has no NaN.
  • Keeps every newline, so a *json.SyntaxError's line over the output is the line in the source. Columns are not kept.
  • Known gap: hex (0x1F) and octal (012) integers stay invalid; Json.NET reads them, but a rewrite depends on the target type.

Documentation

Doc Audience
HUMANS.md Running and using the package
AGENTS.md Internals, invariants, and why
CONTRIBUTING.md Process — commits, tests, docs
SECURITY.md Vulnerability disclosure
CHANGELOG.md Release notes

License

MIT — see LICENSE. © 2026 Rethunk.Tech, LLC.

Documentation

Overview

Package lenientjson turns the JSON that Json.NET (Newtonsoft.Json) reads into strict JSON for encoding/json: it decodes a UTF-8, UTF-16 or UTF-32 BOM and strips comments and trailing commas; accepts single-quoted strings, unquoted property names, bare decimal points and NaN/Infinity/undefined; and escapes control characters inside strings.

Index

Constants

This section is empty.

Variables

View Source
var ErrTruncated = errors.New("lenientjson: input ends inside a UTF-16 or UTF-32 code unit")

ErrTruncated reports a UTF-16 or UTF-32 input that ends inside a code unit.

Functions

func Clean

func Clean(b []byte) []byte

Clean rewrites b as strict JSON where Json.NET would read it. Text Json.NET rejects is left for encoding/json to reject; string contents are never rewritten beyond escaping. Newlines are kept, so a *json.SyntaxError's line counted over the result is the source line. Input that ends inside a UTF-16 or UTF-32 code unit is returned unchanged, which encoding/json rejects; TryClean reports it as an error.

func TryClean added in v0.2.0

func TryClean(b []byte) ([]byte, error)

TryClean is Clean that returns ErrTruncated for input ending inside a UTF-16 or UTF-32 code unit.

Types

This section is empty.

Jump to

Keyboard shortcuts

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