1

I'm using Microsoft's cpprestsdk to send a JSON from client to server.

The problem is that the data I'm writing in the JSON isn't keeping it's original writing order, and it's being rearranged in alphabetical order.

This is the function that returns the JSON value object:

web::json::value returnJSON()
{
    json::value output;

    output[L"Outer_list"][L"valX"] = json::value::string(L"value1");
    output[L"Outer_list"][L"valY"] = json::value::string(L"value2");
    output[L"Outer_list"][L"valA"] = json::value::string(L"value3");
    output[L"Outer_list"][L"valZ"] = json::value::string(L"value4");

    output[L"Outer_list"][L"valXList"][0] = json::value::string(L"XValue1");
    output[L"Outer_list"][L"valXList"][1] = json::value::string(L"XValue2");
    output[L"Outer_list"][L"valXList"][2] = json::value::string(L"XValue3");

    output[L"Outer_list"][L"valYList"][0] = json::value::string(L"YValue1");
    output[L"Outer_list"][L"valYList"][1] = json::value::string(L"YValue2");
    output[L"Outer_list"][L"valYList"][2] = json::value::string(L"YValue3");

    std::wcout << "output = " << output.serialize() << std::endl << std::endl;

    return output;
}

And this is the function that sends the data:

void sendPOST()
{
    web::json::value myJson = returnJSON();
    http_client client(L"http://127.0.0.1:34568/");

    try {
        client.request(methods::POST, L"MY_path-query_fragment", myJson).then([](http_response response) {
            if (response.status_code() == status_codes::OK) {
                auto body = response.extract_string().get();
                std::wcout << "The response is = \n" << body << std::endl;
            }
            else
            {
                std::cout << "No response" << std::endl;
            }
            });
    }
    catch (const std::exception& e)
    {
        std::cout << "ERROR: " << e.what() << std::endl;
    }
}

The data should look like this:

{"Outer_list":{"valX":"value1","valY":"value2","valA":"value3","valZ":"value4","valXList":["XValue1","XValue2","XValue3"],"valYList":["YValue1","YValue2","YValue3"]}}

But on the client/server I see that the data that's being sent/received is:

{"Outer_list":{"valA":"value3","valX":"value1","valXList":["XValue1","XValue2","XValue3"],"valY":"value2","valYList":["YValue1","YValue2","YValue3"],"valZ":"value4"}}

As you can immediately see, the valA is the first one and the valZ is the last, becuase they have been reodered.

How can I turn off this alphabetical reoder, and keep the original writing order?

Destard20
  • 13
  • 1
  • 6
  • JSON objects are unordered, quite a lot of JSON libraries don't preserve the order of the keys. I don't imagine there's a way of changing this in cpprest as it's probably just using a `std::map` which will always sort its keys – Alan Birtles Feb 21 '22 at 14:10
  • So the order shouldn't matter? Yeah that would make sense, especially because a parser should find the value it's searching for despite the order of the data. Edit: I asked the question because keeping the original order would have been more convenient if someone were to read it raw, but it's not a big deal. – Destard20 Feb 21 '22 at 14:18

1 Answers1

0

It turns out you can turn off the sorting! Add this line right after you create 'output':

output[L"Outer_list"] = web::json::value::object(true);

There is a default parameter (bool keep_order = false) that you can override when you create your web::json::value::object. Once this is set, all values added will be kept in the original order.

I was also looking for a way to make the output easier to read by humans and stumbled upon this in the docs.

J Higs
  • 114
  • 5