Skip to content

nukilabs/js

Repository files navigation

Javascript Object Decoding

A small fork of Go's encoding/json (Go 1.25) with minimal tweaks to accept JavaScript-style object keys during decoding:

  • Unquoted identifier keys are allowed: {a:1, b:2}
  • Single-quoted keys are allowed: {'a':1, 'b':2}
  • Trailing commas are allowed: {a:1, b:2,} and [1, 2, 3,]

Everything else behaves like encoding/json, with these differences:

  • String values may be single-quoted or double-quoted. Numbers/booleans/null unchanged.
  • The JavaScript literal undefined is accepted on decode and treated like null.
  • Unknown identifier values (e.g., myVar, someValue, window.location) are accepted on decode and treated like null.
  • Numbers 0 and 1 can be unmarshaled into bool fields (as false and true respectively).
  • Struct field tags use js:"..." instead of json:"...".
  • Marshal/Encoder output remains standard JSON.

Install

go get github.com/nukilabs/js

Usage

package main

import (
    "fmt"
    "github.com/nukilabs/js"
)

func main() {
    var m map[string]any
    _ = js.Unmarshal([]byte("{a:1, 'b':2, \"c\":3}"), &m)
    fmt.Println(m) // map[a:1 b:2 c:3]
    
    // Unknown identifiers and member expressions are treated as null
    _ = js.Unmarshal([]byte("{x: someUnknownValue, y: window.location, z: 42}"), &m)
    fmt.Println(m) // map[x:<nil> y:<nil> z:42]
}

Notes

  • Token streaming API also supports single-quoted and bare identifier object keys.

  • This project copies Go's encoding/json under its BSD license (see LICENSE) and applies minimal changes in the scanner and decoder.

About

Javascript object unmarshal and marshal

Resources

License

Stars

Watchers

Forks

Languages