Giter VIP home page Giter VIP logo

jsonparser's Introduction

Minimal JSON Parser written in go

Purpose

This project is a solution for Write Your Own JSON Parser build for my personal educational purposes

Features

  • Parse json into interface{}
  • TODO: implement json stringify

Implementation Details

The implementation is based on the json specification Introducing JSON.

Lexical analysis

This step is responsible to create the tokens. A json scanner was implemented from scratch to accomplish this task.

Syntax analysis

This step is responsible to validate the correct structure that matches the formal grammar and create the syntax tree. The parser implemented in this project is a recursive descent parser based on the json context free grammar seen in the specification Introducing JSON.

Syntax tree

The json is parsed directly as an interface{}. Can be used exactly like go manipulates Generic JSON

Example Usage

package main

import (
	"JSONParser/JSONParser"
	"JSONParser/Util"
	"log"
	"os"
)

func main() {
	input, err := os.ReadFile("tests/step4/valid2.json")

	if err != nil {
		log.Fatal(err)
	}

	parsed, err := JSONParser.Parse(input)
	if err != nil {
		log.Fatal(err)
	}

	Util.Printify(parsed)
}

File tests/step4/valid2.json

{
  "key": "value",
  "y": "\u0020",
  "e": 12.5e-4,
  "e2": 12e+4,
  "e3": 12e-4,
  "key-n": 101000,
  "n" : -12445.1,
  "key-o": {
    "inner key": "inner value"
  },
  "key-l": [
    "list value"
  ],
  "l" : [1, 2, "dd", 3],
  "nested": {
    "n": {
      "attr": true
    }
  }
}

Output

{
  "key-n": 101000,
  "n": -12445.1,
  "key-l": ["list value"],
  "e": 0.00125,
  "e2": 120000,
  "e3": 0.0012,
  "key-o": {
    "inner key": "inner value"
  },
  "l": [1, 2, "dd", 3],
  "nested": {
    "n": {
      "attr": true
    }
  },
  "key": "value",
  "y": " "
}

How to run

  1. clone the repo git clone https://github.com/Leonardpepa/JSONParser
  2. build go build
  3. run on windows JSONParser.exe
  4. run on linux ./JSONParser
  5. test go test ./...

Tests

The parser is tested comparing the results against the native go json package. Run the tests go test ./...

jsonparser's People

Contributors

leonardpepa avatar

Stargazers

Elisavet  avatar Evita Dasyra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.