Fork me on GitHub

go-ruby-json

Ruby's JSON parser & generator in pure Go — MRI-compatible, no cgo.

MRI-compatibleJSON.parseJSON.generatepretty_generatesymbolize_namesParserErrorNestingErrorMRI byte-exact100% coverage6 arches
Documentation (MkDocs Material + mike) Go 1.26.4+ Coverage 100%
zsh
$ go get github.com/go-ruby-json/json@latest

go-ruby-json is a pure-Go (no cgo) reimplementation of Ruby's JSON library — a JSON.parse parser and a JSON.generate / JSON.pretty_generate generator that match reference Ruby's parsing and serialization byte-for-byte. It round-trips Ruby values through JSON the way MRI does: the full value graph (Hash/Array/String/Integer/Float/true/false/nil), symbolize_names, depth-limited nesting, and the complete JSON::ParserError / JSON::NestingError / JSON::GeneratorError taxonomy. It was extracted from rbgo's prelude/internals into a reusable standalone library: no dependency on the Ruby runtime, the dependency runs the other way. It is the JSON backend for go-embedded-ruby, bound by rbgo as a native module just like go-ruby-regexp and go-ruby-erb — differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Parser ready

JSON.parse(s) parses JSON text into the Ruby value graph (Hash/Array/String/Integer/Float/true/false/nil), with symbolize_names to key hashes by Symbol, exactly as MRI’s parser does.

Generator ready

JSON.generate(v) emits compact JSON and JSON.pretty_generate(v) the indented form, matching reference Ruby’s spacing and escaping byte-for-byte.

Nesting & limits ready

Depth tracking that raises JSON::NestingError past the configured limit, mirroring MRI’s recursion guard on both parse and generate.

Error taxonomy ready

The JSON::ParserError / JSON::NestingError / JSON::GeneratorError hierarchy, raised on the same inputs and with the same messages as reference Ruby.

Number & string fidelity ready

Integers, floats, and escapes (including \uXXXX surrogate pairs) parsed and emitted exactly as MRI, including the float formatting edge cases.

Differential oracle & coverage ready

A wide corpus round-tripped here and by the system ruby/json, compared byte-for-byte; 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.

A faithful port of Ruby's JSON parser and generator in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It parses JSON into the Ruby value graph with symbolize_names, emits compact and pretty_generate forms, tracks nesting depth, and raises the JSON::ParserError / JSON::NestingError / JSON::GeneratorError taxonomy. Validated differentially against the system ruby binary — parsed and generated values compared against MRI's json. It is a standalone, reusable module extracted from rbgo's internals, and the JSON backend for the sibling org github.com/go-embedded-ruby.