An open API service indexing awesome lists of open source software.

https://github.com/xirzo/jsonlexer

JSON Lexer that can be used to parse json files
https://github.com/xirzo/jsonlexer

json lexer parsing

Last synced: 2 months ago
JSON representation

JSON Lexer that can be used to parse json files

Awesome Lists containing this project

README

          

# JSON Lexer πŸ“„πŸ”

This is a simple lexer written in C that tokenizes JSON files. It breaks down JSON input into tokens, making it easier to parse and process data. πŸš€

---

## How It Works πŸ› οΈ

The lexer reads a JSON string character by character and identifies tokens based on the JSON syntax. It uses a `Lexer` struct to keep track of the current state and a `Token` struct to represent each token.

### Key Functions

- `init_lexer`: Initializes the lexer with the input JSON string.
- `next_token`: Returns the next token in the input.

---

## Token Types 🏷️

The lexer recognizes the following token types:

| Token Type | Example | Description |
|--------------|---------------|--------------------------------------|
| `TOKEN_LBR` | `{` | Left brace |
| `TOKEN_RBR` | `}` | Right brace |
| `TOKEN_COLON`| `:` | Colon |
| `TOKEN_COMMA`| `,` | Comma |
| `TOKEN_STRING`| `"key"` | String literal |
| `TOKEN_NUMBER`| `123` | Number literal |
| `TOKEN_BOOLEAN`| `true`/`false` | Boolean literal |
| `TOKEN_NULL` | `null` | Null literal |
| `TOKEN_EOF` | N/A | End of file/input |
| `TOKEN_ILLEGAL`| N/A | Illegal/unknown token |

---

## Example Usage πŸ–₯️

Here’s how you can use the lexer in your code:

```c
#include "lexer.h"

int main() {
const char* json = "{\"key\": \"value\"}";
struct Lexer lexer;
init_lexer(&lexer, json);

Token token;

do {
token = next_token(&lexer);
printf("Token: %s, Literal: %s\n", token_type_to_string(token.type), token.literal);
} while (token.type != TOKEN_EOF);

return 0;
}
```

---

## Running Tests πŸ§ͺ

The project includes a suite of tests to verify the lexer's functionality. Run the tests to ensure everything works as expected:

### Test Cases

1. **Empty Object** `{}` βœ…
2. **Key-Number Pair** `{"key": 123}` βœ…
3. **Key-String Pair** `{"key": "value"}` βœ…
4. **Key-Boolean Pair** `{"key": true}` βœ…
5. **Key-Null Pair** `{"key": null}` βœ…

---

## Project Structure πŸ“‚

```
json-lexer/
β”œβ”€β”€ lexer.h # Lexer header file
β”œβ”€β”€ lexer.c # Lexer implementation
β”œβ”€β”€ token.h # Token header file
β”œβ”€β”€ token.c # Token implementation
β”œβ”€β”€ main.c # Main program and tests
```