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

https://github.com/battlegrounds/minjson

Minimalistic JSON library for modern C++20
https://github.com/battlegrounds/minjson

cpp20 json minimal modern-cpp

Last synced: about 1 year ago
JSON representation

Minimalistic JSON library for modern C++20

Awesome Lists containing this project

README

          

# 🧩 MinJSON – минималистичная JSON-Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° для C++20

**MinJSON** β€” это лСгковСсная, Π²Ρ‹ΡΠΎΠΊΠΎΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΠ½Π°Ρ JSON-Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° с ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΎΠΉ:

- βœ… C++20-ΠΊΠΎΠ½Ρ†Π΅ΠΏΡ‚ΠΎΠ² (`MinJSONValueType`)
- βœ… ΠŸΠΎΠ»Π½ΠΎΡ†Π΅Π½Π½ΠΎΠ³ΠΎ парсинга ΠΈ сСриализации
- βœ… ВипобСзопасного доступа Ρ‡Π΅Ρ€Π΅Π· ΠΏΡƒΡ‚ΠΈ (`"user.name[0].value"`)
- βœ… ΠœΠ°ΠΊΡ€ΠΎΡΠ½ΠΎΠΉ рСфлСксии (Π² стилС `nlohmann::json`)
- βœ… ΠšΡΡˆΠΈΡ€ΠΎΠ²Π°Π½ΠΈΡ ΠΏΡƒΡ‚Π΅ΠΉ (`thread_local`)
- βœ… БовмСстима с STL: `std::string`, `std::vector`, `std::optional`

## πŸš€ ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования

### πŸ“Œ ΠŸΠ°Ρ€ΡΠΈΠ½Π³ ΠΈ доступ

```cpp
#include "MinJSON.hpp"

int main() {
MinJSON json;
auto result = json.parse(R"({"user":{"name":"Alice","age":30}})");

if (std::holds_alternative(result)) {
std::cerr << "Error: " << std::get(result) << "\n";
return 1;
}

const auto& data = std::get(result);
std::string name = json.get(data, "user.name");
int age = json.get(data, "user.age");

std::cout << name << " is " << age << " years old\n";
}
```

### πŸ“Œ ΠœΠ°ΠΊΡ€ΠΎΡΠ½Π°Ρ рСфлСксия

```cpp
#include "MinJSON.hpp"

struct User {
int id;
std::string name;
};

MINJSON_REGISTER_TYPE(User,
MINJSON_FIELD(id)
MINJSON_FIELD(name)
)

int main() {
MinJSON json;
MinJSON_register_User(json);

User user{42, "Bob"};
auto value = json.to_json(user);
std::cout << json.stringify(value, true) << "\n";

auto user2 = json.from_json(value);
std::cout << user2.name << "\n";
}
```

## πŸ”§ Π‘Π±ΠΎΡ€ΠΊΠ° Ρ‡Π΅Ρ€Π΅Π· CMake

```bash
mkdir build && cd build
cmake ..
make
```

## πŸ“„ ЛицСнзия

MIT License. Π‘Π²ΠΎΠ±ΠΎΠ΄Π½ΠΎ для использования Π² Π»ΡŽΠ±Ρ‹Ρ… цСлях.