29

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.

jllodra
  • 1,238
  • 1
  • 14
  • 21
  • 6
    Nice 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 Answers10

15

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

Philipp
  • 11,059
  • 8
  • 62
  • 115
7

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;
}
vlp
  • 6,693
  • 2
  • 21
  • 47
Shaaban Ebrahim
  • 8,509
  • 1
  • 16
  • 19
4

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

Rand0m
  • 192
  • 1
  • 9
3

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

loentar
  • 4,971
  • 1
  • 21
  • 24
3

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"}
lganzzzo
  • 458
  • 3
  • 8
3

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.

Rutix
  • 851
  • 1
  • 10
  • 22
2

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

georgeliatsos
  • 1,099
  • 2
  • 14
  • 34
2

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.

Adam Gu
  • 166
  • 2
  • 5
1

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.

Alessandro Minoccheri
  • 34,369
  • 22
  • 118
  • 164
  • 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
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.

Dr. Alex RE
  • 1,589
  • 1
  • 10
  • 22