在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:netheril96/StaticJSON开源软件地址:https://github.com/netheril96/StaticJSON开源编程语言:C++ 97.9%开源软件介绍:StaticJSONFast, direct and static typed parsing of JSON with C++. OverviewJSON is a popular format for data exchange. Reading and writing JSON in C++, however, is nontrivial. Even with the help of libraries, one still needs to write lots of boilerplate code, and it is extremely hard to guard against all possible errors, since JSON is dynamically typed while C++ employs static typing. More importantly, manually writing such code is a violation of DRY principle. When manually written, the class definition, parsing and serialization code can easily become out of sync, leading to brittle code and subtle bugs.
Usage
Just drop the The cmake files are provided for building and running the integration test. Quick startBuiltin types#include <staticjson/staticjson.hpp>
int builtin_test() {
using namespace staticjson;
std::string a = to_json_string(std::vector<double>{1.0, 2.0, -3.1415});
std::string b = to_pretty_json_string(std::map<std::string, std::shared_ptr<std::list<bool>>>{});
std::vector<std::unordered_map<std::string, std::int64_t>> data;
const char* json_string = "[{\" hello \": 535353, \" world \": 849},"
" {\" k \": -548343}]";
assert(from_json_string(json_string, &data, nullptr));
assert(data.size() == 2);
assert(data[1][" k "] == -548343);
to_pretty_json_file(stdout, data);
return 0;
} Register custom class typesFor your own classes, you need to add some definitions first before you can use the Intrusive definitionThis way requires you to implement a special method in the class prototyped struct Date
{
int year, month, day;
void staticjson_init(ObjectHandler* h)
{
h->add_property("year", &year);
h->add_property("month", &month);
h->add_property("day", &day);
h->set_flags(Flags::DisallowUnknownKey);
}
};
struct BlockEvent
{
std::uint64_t serial_number, admin_ID = 255;
Date date;
std::string description, details;
void staticjson_init(ObjectHandler* h)
{
h->add_property("serial_number", &serial_number);
h->add_property("administrator ID", &admin_ID, Flags::Optional);
h->add_property("date", &date, Flags::Optional);
h->add_property("description", &description, Flags::Optional);
h->add_property("details", &details, Flags::Optional);
}
}; Non-intrusive definitionThis requires you to overload a special function for your custom class. For example, the namespace staticjson
{
void init(Date* d, ObjectHandler* h)
{
h->add_property("year", &d->year);
h->add_property("month", &d->month);
h->add_property("day", &d->day);
h->set_flags(Flags::DisallowUnknownKey);
}
} You may need to declare Register enumeration typesExample enum class CalendarType
{
Gregorian,
Chinese,
Jewish,
Islam
};
STATICJSON_DECLARE_ENUM(CalendarType,
{"Gregorian", CalendarType::Gregorian},
{"Chinese", CalendarType::Chinese},
{"Jewish", CalendarType::Jewish},
{"Islam", CalendarType::Islam}) This will convert the enum type to/from strings, and signal error if the string is not in the list. Note that this macro must not be instantiated inside a namespace. Custom conversionIf you want a type to be serialized in a different way, such as a custom namespace staticjson
{
template <>
struct Converter<Date>
{
typedef std::string shadow_type;
// This typedef is a must. The shadow type is a C++ type
// that can be directly converted to and from JSON values.
static std::unique_ptr<ErrorBase> from_shadow(const shadow_type& shadow, Date& value)
{
bool success = value.parseISO8601(shadow);
if (success)
return nullptr;
return std::make_unique<CustomError>("Invalid ISO 8601 string");
}
static void to_shadow(const Date& value, shadow_type& shadow)
{
shadow = value.toISO8601();
}
};
}
Error handling
The third parameter of all
List of builtin supported types
Dynamic typingIf you need occasional escape from the rigidity of C++'s static type system, but do not want complete dynamism, you can still find the middle ground in
Export as JSON SchemaFunction MiscThe project was originally named autojsoncxx and requires a code generator to run. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论