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.

4

10 Answers

You might want to take a look at Casablanca introduced in Herb Sutter's blog.

2

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; } 

try great library to build C/C++ Restful APIs. can support all platforms: Linux, FreeBSD, Windows and others

You could look at ffead-cpp. Apart from providing support for json and restfull web services it also includes more features. This framework may be too heavy weight for your situation though.

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).

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 {"name":"john"} 

For C++ web service, I am using the following stack:

You may want to take a look at webcc.

It's a lightweight C++ HTTP client and server library for embedding purpose based on Boost.Asio (1.66+).

It's quite promising and actively being developed.

It includes a lot of examples to demonstrate how to create a server and client.

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.

1

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy