Skip to content

Migrating from the Standard Library

cybergodev/json is 100% compatible with the standard library encoding/json — simply replace the import path and your existing code compiles and runs without any changes. This page walks you through the migration and the additional capabilities you gain.

Migration in Three Steps

  1. Install:

    bash
    go get github.com/cybergodev/json
  2. Replace the import: swap "encoding/json" for "github.com/cybergodev/json".

    go
    // Before
    import "encoding/json"
    
    // After
    import "github.com/cybergodev/json"
  3. Done: it compiles, and all your existing code works unchanged.

Fully Compatible API

The table below maps encoding/json to cybergodev/json:

encoding/jsoncybergodev/jsonNotes
Marshal(v)Marshal(v, cfg...)Compatible signature, optional cfg param
Unmarshal(data, &v)Unmarshal(data, &v, cfg...)Same as above
MarshalIndent(v, prefix, indent)Same nameFully compatible
Compact(dst, src)Same nameFully compatible
Indent(dst, src, prefix, indent)Same nameFully compatible
HTMLEscape(dst, src)Same nameFully compatible
Valid(data)Valid(data, cfg...)Compatible signature
NewEncoder(w)NewEncoder(w, cfg...)Compatible signature
NewDecoder(r)NewDecoder(r, cfg...)Compatible signature
NumberNumberType compatible
DelimDelimType compatible
TokenTokenType compatible

Optional cfg parameter

All extra cfg ...Config parameters are optional (variadic). When omitted, behavior is identical to the standard library; pass them only when you want to enable enhanced capabilities such as security mode or caching.

Code Example: Just Swap the Import

The example below shows the "swap the import only" effect — encoding, decoding, and struct tags work exactly as with encoding/json:

go
package main

import (
    "fmt"

    "github.com/cybergodev/json"
)

func main() {
    type User struct {
        Name string   `json:"name"`
        Age  int      `json:"age"`
        Tags []string `json:"tags"`
    }

    // Encode — exactly the same as encoding/json
    user := User{Name: "Alice", Age: 30, Tags: []string{"go", "json"}}
    b, err := json.Marshal(user)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(b))
    // Output: {"name":"Alice","age":30,"tags":["go","json"]}

    // Decode — exactly the same as encoding/json
    var u User
    if err := json.Unmarshal(b, &u); err != nil {
        panic(err)
    }
    fmt.Printf("%+v\n", u)
    // Output: {Name:Alice Age:30 Tags:[go json]}
}

Additional Capabilities

After migrating, while keeping standard-library compatibility, you can opt into the following capabilities that the standard library does not offer:

CapabilityExampleLearn more
Path queriesjson.GetString(data, "user.name")Path Expression Syntax
Get with defaultjson.GetInt(data, "timeout", 30)Query Functions
Generic getjson.GetTyped[User](data, "user")Generics
Path modificationjson.Set(data, "user.name", "Bob")Modify Operations
Schema validationjson.ValidateSchema(data, schema)Validator
Streaming JSONLjson.StreamLinesInto[T](r, fn)JSONL Processing
High-performance processorp, _ := json.New()Processor Guide

Behavioral Differences

Under the default configuration, cybergodev/json behaves identically to encoding/json. All extra capabilities (security mode, path queries, schema validation, etc.) are opt-in — enabled explicitly via the Config parameter, with no impact on existing code.

In other words: migration is zero-cost, and you gain a superset of "standard library + additional capabilities".

Next Steps