# goccy/go-json architecture

> Architecture diagram of the GitHub repository goccy/go-json, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/goccy/go-json
- Repository: https://github.com/goccy/go-json

## Overview

go-json is a Go JSON library intended as a fast, encoding/json-compatible replacement. Its main workflow accepts Go values through the public API, encodes them to JSON bytes or decodes JSON into Go values, while the jsontext package also exposes lower-level streaming and token-oriented JSON operations. The repository includes a v2 API, configurable behavior, type-safe field filtering, and colored encoding. The graph reflects documented capabilities and indexed dependencies; some implementation paths and links are inferred only from package documentation and are marked dashed.

Main entry points: [`internal/encoder/context.go`](https://github.com/goccy/go-json/blob/master/internal/encoder/context.go) (Encoder engine), [`v2/arshal.go`](https://github.com/goccy/go-json/blob/master/v2/arshal.go) (V2 API), [`json.go`](https://github.com/goccy/go-json/blob/master/json.go) (JSON API), [`v2/marshal_to.go`](https://github.com/goccy/go-json/blob/master/v2/marshal_to.go) (Writer API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public APIs"]
  node_legacy_api["JSON API<br/>[json.go]"]
  node_encode_api["Encode API<br/>[encode.go]"]
  node_decode_api["Decode API<br/>[decode.go]"]
  node_v2_api["V2 API<br/>[arshal.go]"]
  node_marshal_to["Writer API<br/>[marshal_to.go]"]
  node_field_filter["Field filtering<br/>[query.go]"]
end

subgraph group_engine["Encode and Decode"]
  node_encoder["Encoder engine<br/>[context.go]"]
  node_encoder_vm["Encoder VM<br/>[vm.go]"]
  node_colored_vm["Colored encoding<br/>[vm.go]"]
  node_indent_vm["Indented encoding<br/>[vm.go]"]
  node_decoder["Decoder engine<br/>[context.go]"]
  node_decoder_string["String decoding<br/>[string.go]"]
end

subgraph group_text["JSON Text"]
  node_jsontext_decoder["Text decoder<br/>[decode.go]"]
  node_jsontext_encoder["Text encoder<br/>[encode.go]"]
  node_value_scanner["Value scanner<br/>[scan.go]"]
  node_text_tools["Text operations<br/>[text.go]"]
end

subgraph group_support["Shared Support"]
  node_field_rules["Struct field rules<br/>[fields.go]"]
  node_errors["Error handling<br/>[error.go]"]
  node_options["Options<br/>[options.go]"]
end

node_caller(("Go caller"))

node_caller -->|"calls"| node_legacy_api
node_legacy_api -->|"encodes with"| node_encoder
node_legacy_api -->|"decodes with"| node_decoder
node_caller -->|"calls"| node_encode_api
node_caller -->|"calls"| node_decode_api
node_encode_api -.->|"encodes with"| node_encoder
node_decode_api -.->|"decodes with"| node_decoder
node_encoder -.->|"executes"| node_encoder_vm
node_encoder -.->|"uses field rules"| node_field_rules
node_decoder -->|"decodes strings"| node_decoder_string
node_caller -->|"calls"| node_v2_api
node_v2_api -->|"encodes with"| node_encoder
node_v2_api -->|"uses text encoder"| node_jsontext_encoder
node_caller -->|"writes JSON"| node_marshal_to
node_marshal_to -->|"encodes with"| node_encoder
node_marshal_to -->|"writes through"| node_jsontext_encoder
node_caller -->|"streams JSON"| node_jsontext_decoder
node_caller -.->|"writes tokens"| node_jsontext_encoder
node_jsontext_decoder -.->|"scans values"| node_value_scanner
node_caller -.->|"uses text operations"| node_text_tools
node_legacy_api -.->|"reports errors"| node_errors
node_v2_api -->|"reports errors"| node_errors
node_caller -->|"configures filtering"| node_field_filter
node_field_filter -->|"sets field query"| node_encoder
node_caller -->|"configures behavior"| node_options
node_v2_api -->|"applies options"| node_options
node_encoder -.->|"supports coloring"| node_colored_vm
node_encoder -.->|"supports indentation"| node_indent_vm

click node_legacy_api "https://github.com/goccy/go-json/blob/master/json.go"
click node_encode_api "https://github.com/goccy/go-json/blob/master/encode.go"
click node_decode_api "https://github.com/goccy/go-json/blob/master/decode.go"
click node_v2_api "https://github.com/goccy/go-json/blob/master/v2/arshal.go"
click node_marshal_to "https://github.com/goccy/go-json/blob/master/v2/marshal_to.go"
click node_encoder "https://github.com/goccy/go-json/blob/master/internal/encoder/context.go"
click node_encoder_vm "https://github.com/goccy/go-json/blob/master/internal/encoder/vm/vm.go"
click node_colored_vm "https://github.com/goccy/go-json/blob/master/internal/encoder/vm_color/vm.go"
click node_indent_vm "https://github.com/goccy/go-json/blob/master/internal/encoder/vm_indent/vm.go"
click node_decoder "https://github.com/goccy/go-json/blob/master/internal/decoder/context.go"
click node_decoder_string "https://github.com/goccy/go-json/blob/master/internal/decoder/string.go"
click node_field_rules "https://github.com/goccy/go-json/blob/master/internal/jsonfields/fields.go"
click node_jsontext_decoder "https://github.com/goccy/go-json/blob/master/jsontext/decode.go"
click node_jsontext_encoder "https://github.com/goccy/go-json/blob/master/jsontext/encode.go"
click node_value_scanner "https://github.com/goccy/go-json/blob/master/jsontext/scan.go"
click node_text_tools "https://github.com/goccy/go-json/blob/master/jsontext/text.go"
click node_errors "https://github.com/goccy/go-json/blob/master/internal/errors/error.go"
click node_field_filter "https://github.com/goccy/go-json/blob/master/query.go"
click node_options "https://github.com/goccy/go-json/blob/master/internal/options/options.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_legacy_api,node_encode_api,node_decode_api,node_v2_api,node_marshal_to,node_field_filter toneBlue
class node_encoder,node_encoder_vm,node_colored_vm,node_indent_vm,node_decoder,node_decoder_string toneAmber
class node_jsontext_decoder,node_jsontext_encoder,node_value_scanner,node_text_tools toneMint
class node_field_rules,node_errors,node_options toneRose
class node_caller toneIndigo
```

## Components

### Public APIs

- [JSON API](https://github.com/goccy/go-json/blob/master/json.go): `json.go`
- [Encode API](https://github.com/goccy/go-json/blob/master/encode.go): `encode.go`
- [Decode API](https://github.com/goccy/go-json/blob/master/decode.go): `decode.go`
- [V2 API](https://github.com/goccy/go-json/blob/master/v2/arshal.go): `v2/arshal.go`
- [Writer API](https://github.com/goccy/go-json/blob/master/v2/marshal_to.go): `v2/marshal_to.go`
- [Field filtering](https://github.com/goccy/go-json/blob/master/query.go): `query.go`

### Encode and Decode

- [Encoder engine](https://github.com/goccy/go-json/blob/master/internal/encoder/context.go): `internal/encoder/context.go`
- [Encoder VM](https://github.com/goccy/go-json/blob/master/internal/encoder/vm/vm.go): `internal/encoder/vm/vm.go`
- [Colored encoding](https://github.com/goccy/go-json/blob/master/internal/encoder/vm_color/vm.go): `internal/encoder/vm_color/vm.go`
- [Indented encoding](https://github.com/goccy/go-json/blob/master/internal/encoder/vm_indent/vm.go): `internal/encoder/vm_indent/vm.go`
- [Decoder engine](https://github.com/goccy/go-json/blob/master/internal/decoder/context.go): `internal/decoder/context.go`
- [String decoding](https://github.com/goccy/go-json/blob/master/internal/decoder/string.go): `internal/decoder/string.go`

### JSON Text

- [Text decoder](https://github.com/goccy/go-json/blob/master/jsontext/decode.go): `jsontext/decode.go`
- [Text encoder](https://github.com/goccy/go-json/blob/master/jsontext/encode.go): `jsontext/encode.go`
- [Value scanner](https://github.com/goccy/go-json/blob/master/jsontext/scan.go): `jsontext/scan.go`
- [Text operations](https://github.com/goccy/go-json/blob/master/jsontext/text.go): `jsontext/text.go`

### Shared Support

- [Struct field rules](https://github.com/goccy/go-json/blob/master/internal/jsonfields/fields.go): `internal/jsonfields/fields.go`
- [Error handling](https://github.com/goccy/go-json/blob/master/internal/errors/error.go): `internal/errors/error.go`
- [Options](https://github.com/goccy/go-json/blob/master/internal/options/options.go): `internal/options/options.go`

### Other components

- **Go caller**

## Connections

- Go caller → JSON API: calls (evidence: `README.md`)
- JSON API → Encoder engine: encodes with (evidence: `json.go`)
- JSON API → Decoder engine: decodes with (evidence: `json.go`)
- Go caller → Encode API: calls (evidence: `README.md`)
- Go caller → Decode API: calls (evidence: `README.md`)
- Encode API → Encoder engine: encodes with
- Decode API → Decoder engine: decodes with
- Encoder engine → Encoder VM: executes
- Encoder engine → Struct field rules: uses field rules
- Decoder engine → String decoding: decodes strings (evidence: `internal/decoder/context.go`)
- Go caller → V2 API: calls (evidence: `README.md`)
- V2 API → Encoder engine: encodes with (evidence: `v2/arshal.go`)
- V2 API → Text encoder: uses text encoder (evidence: `v2/arshal.go`)
- Go caller → Writer API: writes JSON (evidence: `README.md`)
- Writer API → Encoder engine: encodes with (evidence: `v2/marshal_to.go`)
- Writer API → Text encoder: writes through (evidence: `v2/marshal_to.go`)
- Go caller → Text decoder: streams JSON (evidence: `jsontext/decode.go`)
- Go caller → Text encoder: writes tokens
- Text decoder → Value scanner: scans values
- Go caller → Text operations: uses text operations
- JSON API → Error handling: reports errors
- V2 API → Error handling: reports errors (evidence: `v2/arshal.go`)
- Go caller → Field filtering: configures filtering (evidence: `query.go`)
- Field filtering → Encoder engine: sets field query (evidence: `query.go`)
- Go caller → Options: configures behavior (evidence: `README.md`)
- V2 API → Options: applies options (evidence: `v2/arshal.go`)
- Encoder engine → Colored encoding: supports coloring
- Encoder engine → Indented encoding: supports indentation
