eteran / cpp-json Goto Github PK
View Code? Open in Web Editor NEWA fast & modern C++17 JSON library
License: Other
A fast & modern C++17 JSON library
License: Other
Hello, I am using the array/iterator facilities to navigate each object of a certain array, but some fields/attributes may be missing in some of the objects, the library is throwing an error if I try to obtain a value of a non-existent field:
terminate called after throwing an instance of 'json::invalid_index'
I tried is_null but it does not work because I have to retrieve the value first and the exception gets triggered.
Any help will be greatly appreciated.
Thank you.
ps: Very nice library, I started a hello world right away, I am parsing mostly, the iterator is great, I just hit this aparent limitation in the API, I cannot find a contains() or has_attribute() function
Hello,
depending on the system locale (at least on linux) floating point numbers may be rendered with a comma instead of a dot, resulting in a malformed JSON.
This happen ad example with Italian, Russian, French or German locales.
Hello, not a problem or bug per-se, but in any case I report it, I have a 2021 version, single-header file of this nice parser, I use it a lot for my github projects, working just fine. Today I run cppcheck using latest version on Ubuntu 22.04 and this warning was printed:
cppcheck --version
Cppcheck 2.7
The warning:
src/json.h:1259:18: error: Exception thrown in function declared not to throw exceptions. [throwInNoexceptFunction]
return has_key(as_object(v), key);
^
I see that this parser is divided into several .h files, and wonder how I have a single-header version? I don't recall creating a single file myself, whatever.
I will try to use the current Master version of the parser and run again cppcheck, nevertheless I checked the latest code and has_key keeps marked as noexcept and contains pretty much the same code as my old version.
Regards,
CPPServer
I have this fragment, could you please provide a sample code to read the "validator" field, which may or may not be present.
{
"uri": "/ms/categ/delete",
"sql": "execute sp_categ_delete ${categ_id}",
"function": "dbexec",
"fields": [
{"name": "categ_id", "type": "integer", "required": "true"}
],
"validator": { "function": "dbexist", "sql": "sp_categ_inuse ${categ_id}" }
}
Thank you very much!
Martin
When i try to parse this json
{ "test" : { "x" : 2 }, "RobotConfig" : [ 1, 3, { "AngularVel" : 2, "LinearVel" : 20 }, 2, { "AngularVel" : 2, "LinearVel" : 20 } ] }
Gives me this error msg
"terminate called after throwing an instance of 'json:invalid_type_cast'
if i remove this part
2, { "AngularVel" : 2, "LinearVel" : 20 }
working properly.
I have noticed that value
only accepts signed 32-bit integer numbers, no unsigned or 64-bit. Is there any reasoning behind?
The only reason I can think of is Javascript's limitation of integer numbers being confined in the range of [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]
.
However, a JSON library shouldn't be limited by Javascript's limits, so I propose a template constructor accepting all arithmetic types. We could even choose to make 64-bit integer constructors explicit, as JSON is often used from Javascript.
The interface looks very good. :)
Any chance I could skip using Boost?
I am searching the web all day today and I don't understand why on earth there is not a single easy to use library that does not depend on some secondary gigantic project.
:(
Any chance of string_view
support?
And, though it would be a ton of work on your part, I'd love to be able to (somehow) get a constexpr std::string_view
out of a json::object
. I prefer using json::object
over plain string literals but std::string
does malloc
after (I think) 15 bytes on MSVC so there's occasionally a lot of unnecessary overhead.
For example, I'm communicating with a server with JSON and I have something like this:
std::string json_get_thing(std::string const& guid,
std::string const& data,
int const an_int) {
json::object to_send = {
{ "command", "PUT" },
{ "guid", guid },
{ "data", data },
{ "an_int", an_int },
};
return json::stringify(to_send);
}
At least some of the json::value
s (e.g., command
) are just string literals and, at times, all three function params are known at compile time. I'd like to be able to do this:
constexpr std::string_view json_get_thing(std::string_view guid,
std::string_view data,
int const an_int) {
return json::string_viewify( {
{ "command", "PUT" },
{ "guid", guid },
{ "data", data },
{ "an_int", an_int },
});
}
value::as_array
and value::as_object
aren't defined inline in value.tcc, which then results in linker error fatal error LNK1169: one or more multiply defined symbols found with Visual C++ 2015.
The example on the front page with this comment is incorrect. it should be this:
// construct programatically
obj1.insert("test1", "hello world")
.insert("test2", 10)
.insert("test3", json::object().insert("x", 123.456))
.insert("test4", json::array().append(1).append(2).append(3).append(4));
Original issue reported on code.google.com by [email protected]
on 25 Aug 2013 at 12:20
I'm trying to compile cpp-json with Visual Studio 2013
My boost version is boost_1_53
I'm getting error in parser.tcc
It's look like error occurs because to std::isdigit passed one parameter
I get the following error
'bool std::isxdigit(_Elem,const std::locale &)' : expects 2 arguments
if(!std::isxdigit(hex[0])) throw invalid_unicode_character();
if(!std::isxdigit(hex[1])) throw invalid_unicode_character();
if(!std::isxdigit(hex[2])) throw invalid_unicode_character();
if(!std::isxdigit(hex[3])) throw invalid_unicode_character();
How I can solve this issue ?
Original issue reported on code.google.com by [email protected]
on 5 Mar 2015 at 11:13
This is not an issue, I just want to express my gratitude for releasing this component, it worked right away with my simple JSON file where other more famous parsers did throw exceptions right away on initialization and I could not resolve my simple case. Support was also great, the C++ promise of efficient high-level abstractions keeps standing!
The file is not big, it takes about 180 microseconds to parse it and fill some structures (vectors, hashmap), but since it only happens on thread creation (it's a thread_local object) I can live with it.
Regards!
[
{
"uri": "/ms/gasto/view",
"sql": "execute sp_gasto_view",
"function": "dbget"
},
{
"uri": "/ms/gasto/get",
"sql": "execute sp_gasto_get $gasto_id",
"function": "dbget",
"fields": [
{"name": "gasto_id", "type": "int", "required": "true"}
]
},
{
"uri": "/ms/gasto/delete",
"sql": "execute sp_gasto_delete $gasto_id",
"function": "dbexec",
"fields": [
{"name": "gasto_id", "type": "int", "required": "true"}
]
},
{
"uri": "/ms/gasto/add",
"sql": "execute sp_gasto_insert $fecha, $categ_id, $monto, $motivo",
"function": "dbexec",
"fields": [
{"name": "monto", "type": "double", "required": "true"},
{"name": "fecha", "type": "date", "required": "true"},
{"name": "motivo", "type": "string", "required": "true"},
{"name": "categ_id", "type": "int", "required": "true"}
]
},
{
"uri": "/ms/gasto/update",
"sql": "execute sp_gasto_update $gasto_id, $fecha, $categ_id, $monto, $motivo",
"function": "dbexec",
"fields": [
{"name": "gasto_id", "type": "int", "required": "true"},
{"name": "monto", "type": "double", "required": "true"},
{"name": "fecha", "type": "date", "required": "true"},
{"name": "motivo", "type": "string", "required": "true"},
{"name": "categ_id", "type": "int", "required": "true"}
]
},
{
"uri": "/ms/customer/info",
"sql": "execute sp_getCustomerInfo $customerid",
"function": "dbgetm",
"tags": [ {"tag": "customer"}, {"tag": "orders"} ],
"fields": [
{"name": "customerid", "type": "string", "required": "true"}
]
},
{
"uri": "/ms/products/view",
"sql": "execute sp_products_view",
"function": "dbget"
},
{
"uri": "/ms/shippers/view",
"sql": "execute sp_shippers_view",
"function": "dbget"
},
{
"uri": "/ms/customers/view",
"sql": "SELECT customerid, contactname, companyname, phone, country FROM customers ORDER BY companyname",
"function": "dbget"
},
{
"uri": "/ms/categ/view",
"sql": "execute sp_categ_view",
"function": "dbget"
},
{
"uri": "/ms/categ/get",
"sql": "execute sp_categ_get $categ_id",
"function": "dbget",
"fields": [
{"name": "categ_id", "type": "integer", "required": "true"}
]
},
{
"uri": "/ms/categ/add",
"sql": "insert into categ (descrip) values ($descrip)",
"function": "dbexec",
"fields": [
{"name": "descrip", "type": "string", "required": "true"}
]
},
{
"uri": "/ms/categ/update",
"sql": "update categ set descrip = $descrip where categ_id = $categ_id",
"function": "dbexec",
"fields": [
{"name": "categ_id", "type": "integer", "required": "true"},
{"name": "descrip", "type": "string", "required": "true"}
]
},
{
"uri": "/ms/categ/delete",
"sql": "delete from categ where categ_id = $categ_id",
"function": "dbexec",
"fields": [
{"name": "categ_id", "type": "integer", "required": "true"}
],
"validator": { "function": "dbfind", "sql": "SELECT top 1 categ_id FROM gasto where categ_id = $categ_id", "id": "custom", "description": "This record cannot be deleted, it's in use." }
},
{
"uri": "/ms/query/search",
"sql": "execute sp_getSalesByCategory $date1, $date2",
"function": "dbget",
"fields": [
{"name": "date1", "type": "date", "required": "true"},
{"name": "date2", "type": "date", "required": "true"}
]
},
{
"uri": "/ms/status",
"function": "getServerInfo"
},
{
"uri": "/ms/login",
"function": "login",
"fields": [
{"name": "login", "type": "string", "required": "true"},
{"name": "password", "type": "string", "required": "true"}
]
},
{
"uri": "/ms/logout",
"function": "logout"
}
]
Hello, I am trying to use has_key to test if an attribute is present before reading it, but the exception mentioned in the tittle is being thrown.
The JSON:
[
{
"uri": "/ms/query/search",
"sql": "execute sp_getSalesByCategory $date1, $date2",
"function": "dbget",
"fields": [
{"name": "date1", "type": "date", "required": "true"},
{"name": "date2", "type": "date", "required": "true"}
]
},
{
"uri": "/ms/status",
"function": "getServerInfo"
}
]
I am using an array and on each iteration I am testing the existence of certain attributes, like "sql" which may be optional.
std::ifstream file("./service.json");
if (file.is_open()) {
json::value json = json::parse(file);
const json::array &a = as_array(json);
for(auto it = a.begin(); it != a.end(); ++it) {
const json::value &v = *it;
std::cout << json::stringify(v) << std::endl;
std::string uri{ json::to_string(v["uri"]) };
std::cout << " reading ... " << uri << std::endl;
std::string func{ json::to_string(v["function"]) };
std::string sql{ "" };
if(json::has_key(v, "sql")) {
std::cout << " reading sql..." << std::endl;
sql = json::to_string(v["sql"]);
}
//more code follow here....
As soon as this is executed if(json::has_key(v, "sql")) {
the program terminates with this message:
terminate called after throwing an instance of json::invalid_index
What I am doing wrong?
Thank you,
Martín
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.