lenientjson

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 8 Imported by: 0

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

This section is empty.

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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