Skip to content

kanand003/Json_Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Parser

A robust JSON parser implementation in C# that can parse, manipulate, and serialize JSON data. This implementation follows JSON specification standards and provides comprehensive error handling.

Features

  • Lexical Analysis: Converts JSON text into tokens
  • Parsing: Builds an abstract syntax tree from tokens
  • Serialization: Converts JSON objects back to string format
  • Support for all JSON data types:
    • Objects
    • Arrays
    • Strings (with escape sequences)
    • Numbers
    • Booleans
    • Null values

Building and Running

Prerequisites

  • .NET 8.0 SDK or later
  • A terminal/command prompt

Build Instructions

  1. Clone the repository
  2. Navigate to the project directory
  3. Run the following command to build:
dotnet build

Running the Program

After building, you can run the program using:

dotnet run

The program includes a sample JSON string that will be parsed and serialized to demonstrate the functionality.

Running Tests

The project includes test files that can be run using:

dotnet test

Components

1. Lexer (JsonLexer.cs)

  • Tokenizes input JSON string
  • Handles whitespace and special characters
  • Supports escape sequences in strings
  • Validates numeric formats
  • Recognizes keywords (true, false, null)

2. Parser (JsonParser.cs)

  • Constructs JSON object model from tokens
  • Implements recursive descent parsing
  • Handles nested structures
  • Provides detailed error messages for invalid JSON

3. Value Types

  • JsonValue: Base abstract class for all JSON values
  • JsonObject: Represents JSON objects with key-value pairs
  • JsonArray: Represents JSON arrays
  • JsonPrimitive: Represents primitive values (strings, numbers, booleans, null)

4. Serializer (JsonSerializer.cs)

  • Converts JSON objects back to string format
  • Properly escapes special characters
  • Maintains formatting consistency

Usage Example

string json = "{ \"name\": \"John\", \"age\": 30, \"isStudent\": false }";

// Tokenize
var lexer = new JsonLexer(json);
var tokens = lexer.Tokenize();

// Parse
var parser = new JsonParser(tokens);
var jsonObject = parser.Parse();

// Serialize back to string
var serializer = new JsonSerializer();
string jsonString = serializer.Serialize(jsonObject);

Error Handling

The parser includes comprehensive error handling for:

  • Malformed JSON
  • Invalid tokens
  • Unexpected characters
  • Unterminated strings
  • Invalid escape sequences
  • Improper nesting of objects and arrays

Testing

The project includes test files for:

  • Lexer testing (JsonLexerTests.cs)
  • Parser testing (JsonParserTests.cs)
  • Serializer testing (JsonSerializerTests.cs)

Implementation Details

The parser is implemented using a recursive descent approach, which makes it easy to understand and maintain. It processes JSON input in three main stages:

  1. Lexical Analysis: Converts the input string into a sequence of tokens
  2. Parsing: Converts the tokens into a tree structure
  3. Serialization: Converts the tree structure back into a JSON string

The implementation follows best practices for error handling and provides detailed error messages to help with debugging.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages