Convert Figma logo to code with AI

json-iterator logogo

A high-performance 100% compatible drop-in replacement of "encoding/json"

13,330
1,023
13,330
259

Top Related Projects

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

One of the fastest alternative JSON parser for Go that does not require schema

14,239

Get JSON values quickly - JSON parser for Go

Fast JSON serializer for golang.

2,961

faster JSON serialization for Go

3,445

For parsing, creating and editing unknown or dynamic JSON in Go

Quick Overview

json-iterator/go is a high-performance 100% compatible drop-in replacement for "encoding/json" in Go. It aims to provide faster JSON encoding and decoding while maintaining full compatibility with the standard library's JSON package.

Pros

  • Significantly faster performance compared to the standard library's JSON package
  • 100% compatible API with "encoding/json", allowing for easy integration into existing projects
  • Supports custom extensions and configurations for advanced use cases
  • Actively maintained and regularly updated

Cons

  • May introduce an additional dependency to your project
  • Some advanced features might require learning a slightly different API
  • Potential for slight differences in behavior in edge cases compared to the standard library

Code Examples

  1. Basic JSON encoding:
import "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary

data := map[string]interface{}{
    "name": "John Doe",
    "age":  30,
}
jsonBytes, _ := json.Marshal(data)
  1. Basic JSON decoding:
import "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary

jsonStr := `{"name": "John Doe", "age": 30}`
var result map[string]interface{}
json.Unmarshal([]byte(jsonStr), &result)
  1. Using a custom configuration:
import "github.com/json-iterator/go"

json := jsoniter.Config{
    EscapeHTML:             false,
    SortMapKeys:            true,
    ValidateJsonRawMessage: true,
}.Froze()

data := map[string]interface{}{"b": 2, "a": 1}
jsonBytes, _ := json.Marshal(data)

Getting Started

To start using json-iterator/go in your project:

  1. Install the package:

    go get github.com/json-iterator/go
    
  2. Import and use in your code:

    import "github.com/json-iterator/go"
    
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    
    // Use json.Marshal and json.Unmarshal as you would with encoding/json
    
  3. For more advanced usage, refer to the project's documentation and examples on GitHub.

Competitor Comparisons

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

Pros of fastjson

  • Faster parsing and serialization for certain use cases
  • Lower memory allocation and garbage collection overhead
  • Supports in-place value modifications without full re-encoding

Cons of fastjson

  • Less feature-rich compared to json-iterator/go
  • Not as widely adopted or maintained
  • May require more manual handling for complex JSON structures

Code Comparison

fastjson:

var p fastjson.Parser
v, err := p.Parse(jsonStr)
if err != nil {
    // handle error
}
name := v.GetStringBytes("name")

json-iterator:

var json = jsoniter.ConfigCompatibleWithStandardLibrary
var result map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &result)
if err != nil {
    // handle error
}
name := result["name"].(string)

Both libraries aim to provide high-performance JSON parsing and serialization for Go applications. fastjson focuses on speed and low memory usage, making it suitable for scenarios where performance is critical. However, json-iterator offers a more comprehensive feature set and better compatibility with the standard library, making it a more versatile choice for general use cases.

The code comparison demonstrates that fastjson requires slightly more manual handling but allows for direct access to JSON values without full unmarshaling. json-iterator, on the other hand, provides a more familiar interface similar to the standard library's encoding/json package.

One of the fastest alternative JSON parser for Go that does not require schema

Pros of jsonparser

  • Extremely fast for parsing specific fields without unmarshaling the entire JSON
  • Low memory usage due to zero-allocation design
  • Simple API for basic JSON operations

Cons of jsonparser

  • Limited functionality compared to full-featured JSON libraries
  • Requires manual type handling and conversion
  • Less suitable for complex JSON structures or when full object mapping is needed

Code Comparison

jsonparser:

value, err := jsonparser.GetString(data, "user", "name")
if err != nil {
    // Handle error
}

json-iterator/go:

var result struct {
    User struct {
        Name string `json:"name"`
    } `json:"user"`
}
iter := jsoniter.ConfigCompatibleWithStandardLibrary.BorrowIterator(data)
defer jsoniter.ConfigCompatibleWithStandardLibrary.ReturnIterator(iter)
iter.ReadVal(&result)

Summary

jsonparser is ideal for high-performance scenarios where only specific JSON fields are needed, offering speed and low memory usage. However, it sacrifices some convenience and flexibility compared to json-iterator/go, which provides a more feature-rich and familiar API similar to the standard library's encoding/json. json-iterator/go is better suited for complex JSON handling and full object mapping, while jsonparser excels in simple, targeted parsing tasks.

14,239

Get JSON values quickly - JSON parser for Go

Pros of gjson

  • Simpler API for quick JSON parsing and value retrieval
  • No need for struct definitions or unmarshaling
  • Lightweight with no external dependencies

Cons of gjson

  • Limited to read-only operations; no JSON modification capabilities
  • May be less performant for complex JSON structures or large datasets
  • Lacks advanced features like custom unmarshalers or stream processing

Code Comparison

gjson:

value := gjson.Get(json, "name.last")
println(value.String())

json-iterator:

var result struct { Name struct { Last string } }
json.Unmarshal([]byte(jsonStr), &result)
println(result.Name.Last)

Summary

gjson is ideal for simple JSON parsing tasks, offering a straightforward API for quick value retrieval without struct definitions. It's lightweight but limited to read-only operations. json-iterator provides a more comprehensive JSON handling solution with better performance for complex structures and additional features like custom unmarshalers. The choice between them depends on the specific requirements of your project, balancing simplicity against advanced functionality and performance needs.

Fast JSON serializer for golang.

Pros of easyjson

  • Generates code for faster JSON encoding/decoding, potentially outperforming json-iterator in some cases
  • Allows for more fine-grained control over JSON marshaling and unmarshaling
  • Supports custom MarshalJSON/UnmarshalJSON methods for complex types

Cons of easyjson

  • Requires code generation step, which can complicate build processes
  • Less flexible for dynamic JSON structures compared to json-iterator
  • May have a steeper learning curve due to its code generation approach

Code Comparison

easyjson:

//easyjson:json
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

json-iterator:

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}
// No additional code required for basic usage

Both libraries aim to improve JSON handling performance in Go, but they take different approaches. easyjson focuses on compile-time code generation for specific structs, while json-iterator provides a more flexible runtime solution that can be used as a drop-in replacement for the standard library's encoding/json package. The choice between them depends on specific project requirements, performance needs, and development workflow preferences.

2,961

faster JSON serialization for Go

Pros of ffjson

  • Generates static code for faster performance
  • No runtime reflection, reducing CPU and memory usage
  • Supports custom MarshalJSON/UnmarshalJSON methods

Cons of ffjson

  • Requires code generation step, increasing build complexity
  • Less flexible for dynamic JSON structures
  • Not actively maintained (last commit in 2018)

Code Comparison

ffjson:

//go:generate ffjson $GOFILE
type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

json-iterator:

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {
    json.Marshal(&data)
}

Key Differences

  1. Performance: Both libraries aim for high performance, but ffjson achieves this through code generation, while json-iterator uses optimized runtime techniques.

  2. Ease of Use: json-iterator is a drop-in replacement for the standard library, making it easier to integrate. ffjson requires additional build steps and code generation.

  3. Flexibility: json-iterator is more flexible for handling dynamic JSON structures, while ffjson excels with static, known structures.

  4. Maintenance: json-iterator is actively maintained, whereas ffjson has not seen updates in recent years.

  5. Memory Usage: ffjson may have lower memory usage due to its static nature, while json-iterator's performance optimizations might consume more memory in some cases.

3,445

For parsing, creating and editing unknown or dynamic JSON in Go

Pros of gabs

  • Simpler API for parsing and manipulating JSON
  • Easier to use for dynamic JSON structures
  • Better suited for quick prototyping and small projects

Cons of gabs

  • Generally slower performance compared to json-iterator
  • Less feature-rich for advanced JSON operations
  • Not as well-suited for large-scale, high-performance applications

Code Comparison

gabs:

jsonParsed, _ := gabs.ParseJSON([]byte(`{"name":"John","age":30}`))
name := jsonParsed.Path("name").Data().(string)
jsonParsed.Set(31, "age")

json-iterator:

var json = jsoniter.ConfigCompatibleWithStandardLibrary
var data map[string]interface{}
json.Unmarshal([]byte(`{"name":"John","age":30}`), &data)
name := data["name"].(string)
data["age"] = 31

Both libraries offer JSON parsing and manipulation capabilities, but gabs provides a more straightforward API for working with dynamic JSON structures. json-iterator, on the other hand, focuses on high-performance JSON processing and is more suitable for large-scale applications where speed is crucial. The choice between the two depends on the specific requirements of your project, balancing ease of use with performance needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Sourcegraph GoDoc Build Status codecov rcard License Gitter chat

A high-performance 100% compatible drop-in replacement of "encoding/json"

Benchmark

benchmark

Source code: https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_medium_payload_test.go

Raw Result (easyjson requires static code generation)

ns/opallocation bytesallocation times
std decode35510 ns/op1960 B/op99 allocs/op
easyjson decode8499 ns/op160 B/op4 allocs/op
jsoniter decode5623 ns/op160 B/op3 allocs/op
std encode2213 ns/op712 B/op5 allocs/op
easyjson encode883 ns/op576 B/op3 allocs/op
jsoniter encode837 ns/op384 B/op4 allocs/op

Always benchmark with your own workload. The result depends heavily on the data input.

Usage

100% compatibility with standard lib

Replace

import "encoding/json"
json.Marshal(&data)

with

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)

Replace

import "encoding/json"
json.Unmarshal(input, &data)

with

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)

More documentation

How to get

go get github.com/json-iterator/go

Contribution Welcomed !

Contributors

Report issue or pull request, or email taowen@gmail.com, or Gitter chat