31

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,348
  • 1
  • 14
  • 22
  • 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

16

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

Philipp
  • 11,549
  • 8
  • 66
  • 126
9

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
  • 7,811
  • 2
  • 23
  • 51
Shaaban Ebrahim
  • 9,766
  • 1
  • 18
  • 20
6

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

Rand0m
  • 342
  • 1
  • 4
  • 10
3

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

georgeliatsos
  • 1,168
  • 3
  • 15
  • 34
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
  • 558
  • 3
  • 10
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
  • 861
  • 1
  • 10
  • 22
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
  • 35,521
  • 22
  • 122
  • 171
  • 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,772
  • 1
  • 15
  • 23
1

For web service in C, you can leverage library like ulfius, civetweb:

https://github.com/babelouest/ulfius

https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md

For web service in C++, you can leverage library like libhv, restbed:

https://github.com/ithewei/libhv

https://github.com/Corvusoft/restbed

david euler
  • 714
  • 8
  • 12