Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

jujson

jujson is a lightweight, header-only C++11 library designed for parsing JSON. It provides a simple and efficient way to parse JSON strings into structured json_value objects, supporting various JSON data types including null, boolean, string, number, array, and object.

Features

  • Header-Only: No need to compile separate libraries; just include the header file.
  • C++11 Compatible: Works with C++11 and later standards.
  • Efficient Parsing: Designed to be fast and lightweight.
  • Customizable: Easily extendable with custom traits and value types.
  • JSON Value Types

jujson supports the following JSON value types:

  • Null: JUSJON_JSON_VALUE_NULL
  • Boolean: JUSJON_JSON_VALUE_TRUE, JUSJON_JSON_VALUE_FALSE
  • String: JUSJON_JSON_VALUE_STRING
  • Number: JUSJON_JSON_VALUE_NUMBER
  • Array: JUSJON_JSON_VALUE_ARRAY
  • Object: JUSJON_JSON_VALUE_OBJECT

JSON Traits

You can customize the JSON traits by defining your own traits class. The default traits are provided for char and wchar_t.

structcustom_json_traits {
typedefchar char_type;
staticconstexpr char_type get_string_litteral_begin() noexcept { return'>'; } // default - "staticconstexpr char_type get_string_litteral_end() noexcept { return'<'; } // default - "staticboolis_string_litteral_begin(char_type c) noexcept { return c == get_string_litteral_begin(); }
staticboolis_string_litteral_end(char_type c) noexcept { return c == get_string_litteral_end(); }
// Define other traits as needed...
};

Error Handling

jujson provides error codes to help with debugging:

  • jujson_ERROR_CODE_SUCCESS: Successful parsing.
  • jujson_ERROR_CODE_UNEXPECTED: Unexpected character encountered.
  • jujson_ERROR_CODE_EOF: End of file reached unexpectedly.

You can check the last error code using parser.get_last_error().

Installation

To use jujson, simply include the header file in your project:

#include"jujson.hpp"

Usage

jujson is designed to be a simple and (maybe)efficient JSON parsing library. It can be used in various applications where JSON data needs to be parsed and manipulated.

Examples

Basic

Here is a basic example of how to use jujson to parse a JSON string:

#include<iostream>
#include"jujson.hpp"intmain() {
std::string jsonStr = R"({"name": "John", "age": 30, "isStudent": false, "courses": ["Math", "Science"]})";
jujson::json_parser<std::string> parser(jsonStr);
jujson::json_value<std::string> jsonObj = parser.parse_json_object_expected();
if (jsonObj.is_valid()) {
std::cout << "Parsed JSON: " << jsonObj.to_string() << '\n';
return0;
} else {
std::cerr << "Error parsing JSON: " << parser.get_last_error() << '\n';
return1;
}
return0;
}

Accessing

#include<iostream>
#include"jujson.hpp"intmain() {
std::string jsonStr = R"({"name": "John", "age": 30, "isStudent": false, "courses": ["Math", "Science"]})";
jujson::json_parser<std::string> parser(jsonStr);
jujson::json_value<std::string> jsonObj = parser.parse_json_object_expected();
if (jsonObj.is_valid()) {
// Accessing string valueauto nameKV = jsonObj.find("name");
if (nameKV != jsonObj.get_childs().end()) {
std::cout << "Name: " << nameKV->value().data() << '\n';
}
// Accessing number valueauto ageKV = jsonObj.find("age");
if (ageKV != jsonObj.get_childs().end()) {
std::cout << "Age: " << ageKV->value().data() << '\n';
}
// Accessing boolean valueauto isStudentKV = jsonObj.find("isStudent");
if (isStudentKV != jsonObj.get_childs().end()) {
std::cout << "Is Student: " << isStudentKV->value().data() << '\n';
}
// Accessing array valueauto coursesKV = jsonObj.find("courses");
if (coursesKV != jsonObj.get_childs().end()) {
for (constauto& courseKVs : coursesKV->value().get_childs()) {
std::cout << "Course: " << courseKVs.data() << '\n';
}
}
return0;
} else {
std::cerr << parser.line() << ':' << parser.column() << ':' << " error parsing JSON: " << parser.get_last_error() << '\n';
return1;
}
return0;
}

Custom Trait Type

#include<iostream>
#include"jujson.hpp"structcustom_trait {
public:typedefchar char_type;
public:[[nodiscard]]constexprstatic char_type get_string_litteral_begin() noexcept {return'<';}
[[nodiscard]]constexprstatic char_type get_string_litteral_end() noexcept {return'>';}
[[nodiscard]]staticboolis_string_litteral_begin(char_type c) noexcept {return c == get_string_litteral_begin();}
[[nodiscard]]staticboolis_string_litteral_end(char_type c) noexcept {return c == get_string_litteral_end();}
[[nodiscard]]constexprstatic char_type get_array_begin() noexcept {return'[';}
[[nodiscard]]constexprstatic char_type get_array_end() noexcept {return']';}
// . . . - copy from default traits
};
intmain() {
std::string jsonStr = R"({<name>: <John>, <age>: 30, <isStudent>: false, <courses>: [<Math>, <Science>]})";
jujson::json_parser<std::string> parser(jsonStr);
jujson::json_value<std::string> jsonObj = parser.parse_json_object_expected();
if (jsonObj.is_valid()) {
std::cout << "Parsed JSON: " << jsonObj.to_string() << '\n';
return0;
} else {
std::cerr << "Error parsing JSON: " << parser.get_last_error() << '\n';
return1;
}
return0;
}

Acknowledgments

License

This library is licensed under the MIT License. See the LICENSE file for more details.

About

A C++11 header-only library for json parsing

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages