I am trying to do a JSON Restful web service in C/C++. I have tried Axis2/C and Staff, which work great for XML serialization/deserialization but not for JSON.
-
6Nice question but take my advice: If you can do this web service in Java (or other framework-friendly oriented towards web service) - do it. Messing with low level stuff which C++ offers is great for learning, but it'll slow you down in most cases. – Poni Mar 20 '12 at 17:36
-
Poni has a point. Even if you "roll your own" Java service, (using, say, Jetty and Gson) you can get something simple in a few hours, I've found. There may be frameworks out there that do all of the servlet stuff for you, but nothing immediately comes to mind. – Tom Mar 22 '12 at 17:51
-
1@poni and Tom. What about speed, what if I wanted to write a json framework and wanted it to be bloody fast ? Wouldn't c\c++ be a good idea ? – gideon Nov 17 '14 at 23:44
-
@gideon yes if you can write it optimally and have lots of requests. otherwise you won't find any significant difference. – Sujay Phadke Feb 25 '16 at 21:02
10 Answers
You might want to take a look at Casablanca introduced in Herb Sutter's blog.
- 11,059
- 8
- 62
- 115
there are a small number of libraries that support creating rest services with c, e.g. restinio:
#include <restinio/all.hpp>
int main()
{
restinio::run(
restinio::on_this_thread()
.port(8080)
.address("localhost")
.request_handler([](auto req) {
return req->create_response().set_body("Hello, World!").done();
}));
return 0;
}
- 6,693
- 2
- 21
- 47
- 8,509
- 1
- 16
- 19
try https://github.com/babelouest/ulfius great library to build C/C++ Restful APIs. can support all platforms: Linux, FreeBSD, Windows and others
- 192
- 1
- 9
Try ngrest. It's a simple but fast C++ RESTful JSON Web Services framework. It can be deployed on top of Apache2, Nginx or own simple http server.
Regarding Axis2/C with JSON. It's seems that official Axis2/C no longer maintained. So Axis2/C become obsolete (but still works).
JSON support for Axis2/C is available in axis2c-unofficial project.
There are an installation manuals on how to install Axis2/C with JSON support under Linux, Windows using binary package, Windows from source code.
You can try it with WSF Staff using Customers (REST) example in JSON mode (which is available from staff/samples/rest/webclient directory of staff source code).
- 4,971
- 1
- 21
- 24
Take a look at Oat++
It has:
- URL routing with URL-parameters mapping
- Support for Swagger-UI endpoint annotations.
- Object-Mapping with JSON support.
Example endpoint:
ENDPOINT("GET", "users/{name}", getUserByName, PATH(String, name)) {
auto userDto = UserDto::createShared();
userDto->name = name;
return createDtoResponse(Status::CODE_200, userDto);
}
Curl:
$ curl http://localhost:8000/users/john
{"name":"john"}
- 458
- 3
- 8
For C++ web service, I am using the following stack:
- ipkn/crow C++ micro web framework
- nlohmann/json for json serialization/deserialization.
- 1,099
- 2
- 14
- 34
There is a JIRA project resolved the support of JSON in AXIS2/C .
I implemented in my project and I managed with the writer (Badgerfish convention) but still I am trying to manage with the reader.
It seems more complicated managing with the stack in the memory.
- 34,369
- 22
- 118
- 164
- 11
- 1
-
Really, it was not resolved. Resolution "Won't fix". That is because this patch: 1) for old Axis2/C-1.3, 2) very unstable and don't work at all, 3) For windows-only. – loentar Apr 05 '13 at 09:33
JSON and JSONPath are supported for both C and C++ in gsoap with a new code generator and a new JSON API to get you started quickly.
Several JSON, JSON-RPC and REST examples are included. Memory management is automatic.
The code generator can be useful. Take for example the json.org menu.json snippet:
{ "menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}
}
The gsoap command jsoncpp -M menu.json generates this code to populate a JSON value:
value x(ctx);
x["menu"]["id"] = "file";
x["menu"]["value"] = "File";
x["menu"]["popup"]["menuitem"][0]["value"] = "New";
x["menu"]["popup"]["menuitem"][0]["onclick"] = "CreateNewDoc()";
x["menu"]["popup"]["menuitem"][1]["value"] = "Open";
x["menu"]["popup"]["menuitem"][1]["onclick"] = "OpenDoc()";
x["menu"]["popup"]["menuitem"][2]["value"] = "Close";
x["menu"]["popup"]["menuitem"][2]["onclick"] = "CloseDoc()";
Also reading parsed JSON values and JSONPath code can be generated by this tool.
EDIT
To clarify, the jsoncpp command-line code generator shows the API code to read and write JSON data by using a .json file as a template, which I found is useful to save time to write the API code to populate and extract JSON data. JSONPath query code can also be generated with this tool.
- 1,589
- 1
- 10
- 22