Skip to content

glamboyosa/json-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Go JSON Parser

This project implements a basic JSON parser in Go using a recursive descent strategy. It tokenizes a JSON string and then parses the tokens into a nested Go map[string]any or []any structure.

Features

  • Parses basic JSON structures: objects ({}), arrays ([])
  • Parses JSON value types: strings ("..."), numbers, booleans (true, false), null
  • Uses recursive descent for parsing logic.
  • Includes basic logging for tracing the tokenization and parsing process.
  • Provides a Makefile for easy building and testing.
  • Includes unit tests using the testify library.

Usage

Make sure you have Go installed (version 1.18+ recommended).

  1. Get dependencies:

    go mod tidy
  2. Run the parser with the example JSON:

    make run
    # or
    go run main.go
  3. Run tests:

    make test
  4. Run tests with coverage:

    make test-coverage

    This will generate an HTML coverage report (coverage.out) and open it in your browser.

How it Works

  1. Tokenization: The ParseJSON method first iterates through the input string, identifying and creating tokens (like {, }, "name", :, 30, true, etc.).
  2. Parsing: After tokenization, the parse method is called. It looks at the current token and dispatches to parseObject or parseArray based on the token type ({ or [). These functions recursively call parse to handle nested structures and values.
  3. Recursive Descent: The parser descends through the token stream, building up the Go data structure (map[string]any for objects, []any for arrays) as it goes.

About

A simple Go JSON parser using recursive descent parsing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published