Used StringBuffer and Writer to make json route request 53/79553/1
authorDuane Gearhart <duane@mapzen.com>
Mon, 27 Jun 2016 17:25:20 +0000 (13:25 -0400)
committerBaldur Gudbjornsson <baldur@mapzen.com>
Fri, 1 Jul 2016 18:10:46 +0000 (14:10 -0400)
Change-Id: Ifbeefb54ae9d490d33166ef14b4cdef5e63c027e

src/mapzen/mapzen_route.cpp

index 7eb820e..1d1195d 100644 (file)
@@ -54,77 +54,91 @@ std::string url_encode(const std::string& unencoded) {
 int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data)
 {
        MAP_DEBUG("**********IN QUERY ROUTE!!**************");
-       rapidjson::Document document;
-       rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
 
-       rapidjson::Value directions_options(rapidjson::kObjectType);
-       rapidjson::Value costing(rapidjson::kStringType);
-       rapidjson::Value locations(rapidjson::kArrayType);
-       rapidjson::Value location(rapidjson::kObjectType);
-       rapidjson::Value startloc(rapidjson::kObjectType);
-       rapidjson::Value endloc(rapidjson::kObjectType);
-       rapidjson::Value costing_options(rapidjson::kObjectType);
-
-       switch (type) {
-       case COSTING_AUTO:
-               costing.SetString("auto");
-               break;
-       case COSTING_AUTO_SHORTER:
-               costing.SetString("auto_shorter");
-               break;
-       case COSTING_PEDESTRIAN:
-               costing.SetString("pedestrian");
-               break;
-       case COSTING_MULTIMODAL:
-               costing.SetString("multimodal");
-               break;
-       case COSTING_BICYCLE:
-               costing.SetString("bicycle");
-               break;
-       default:
-               costing.SetString("auto");
-       }
-
-       document.AddMember("costing", costing, allocator);
-       MAP_DEBUG("**********SET COSTING: %f", costing.GetString());
+       rapidjson::StringBuffer buffer;
+       rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
 
-       directions_options.AddMember("unit","mi",allocator);
-       document.AddMember("directions_options", directions_options, allocator);
-       MAP_DEBUG("**********SET DIRECTIONS_OPTIONS: %s", directions_options.GetString());
+       // start json request object
+    writer.StartObject();
 
+       //{"locations":[{"lat":42.358528,"lon":-83.271400,"street":"Appleton"},{"lat":42.996613,"lon":-78.749855,"street":"Ranch Trail"}]
        int length = g_list_length(waypoints);
        if (length != 0) {
                int index = 0;
                GList *waypoints_list = NULL;
                waypoints_list = g_list_first(waypoints);
+           writer.Key("locations");
+           writer.StartArray();
+
                while (waypoints_list) {
                        coords_s *data = (coords_s *) waypoints_list->data;
                        MAP_DEBUG("**********IN WAYPOINT LIST******************");
-
-                       location.SetObject();
                        for (unsigned i = 0; i < sizeof(data); i++) {
-                               location.AddMember<double>("lat", data[i].latitude, allocator);
-                               location.AddMember<double>("lon", data[i].longitude, allocator);
+                               writer.StartObject();
+                               writer.Key("lat");
+                               writer.Double(data[i].latitude);
+                               writer.Key("lon");
+                               writer.Double(data[i].longitude);
+                               writer.EndObject();
                        }
                        waypoints_list = g_list_next(waypoints_list);
                        index++;
                }
-               locations.PushBack(location, allocator);
-               document.AddMember("locations", locations, allocator);
+           writer.EndArray();
        } else {
-               startloc.SetObject();
-               startloc.AddMember<double>("lat", startPoint.latitude, allocator);
-               startloc.AddMember<double>("lon", startPoint.longitude, allocator);
-               endloc.SetObject();
-               endloc.AddMember<double>("lat", endPoint.latitude, allocator);
-               endloc.AddMember<double>("lon", endPoint.longitude, allocator);
-
-               locations.PushBack(startloc, allocator);
-               locations.PushBack(endloc, allocator);
-               document.AddMember("locations", locations, allocator);
+           writer.Key("locations");
+           writer.StartArray();
+
+           writer.StartObject();
+           writer.Key("lat");
+           writer.Double(startPoint.latitude);
+           writer.Key("lon");
+           writer.Double(startPoint.longitude);
+           writer.EndObject();
+
+           writer.StartObject();
+           writer.Key("lat");
+           writer.Double(endPoint.latitude);
+           writer.Key("lon");
+           writer.Double(endPoint.longitude);
+           writer.EndObject();
+
+           writer.EndArray();
        }
 
+       std::string costing_str;
+       switch (type) {
+       case COSTING_AUTO:
+               costing_str = "auto";
+               break;
+       case COSTING_AUTO_SHORTER:
+               costing_str = "auto_shorter";
+               break;
+       case COSTING_PEDESTRIAN:
+               costing_str = "pedestrian";
+               break;
+       case COSTING_MULTIMODAL:
+               costing_str = "multimodal";
+               break;
+       case COSTING_BICYCLE:
+               costing_str = "bicycle";
+               break;
+       default:
+               costing_str = "auto";
+       }
+
+    writer.Key("costing");
+    writer.String(costing_str.c_str());
+
+    writer.Key("directions_options");
+    writer.StartObject();
+    writer.Key("units");
+    writer.String("mi");
+    writer.EndObject();
+
        if (avoids) {
+               writer.Key("costing_options");
+               writer.StartObject();
                switch (avoids) {
                case PENALTY_NONE:
                        //TODO
@@ -133,10 +147,12 @@ int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_t
                        //TODO
                        break;
                case PENALTY_TOLL_ROADS:
-                       costing_options.AddMember("toll_booth_penalty","1", allocator);
+                       writer.Key("toll_booth_penalty");
+                       writer.String("1");
                        break;
                case PENALTY_USE_FERRY:
-                       costing_options.AddMember("use_ferry","0", allocator);
+                       writer.Key("use_ferry");
+                       writer.String("0");
                        break;
                case PENALTY_USE_UNPAVED:
                        //TODO
@@ -145,32 +161,18 @@ int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_t
                        //TODO
                        break;
                case PENALTY_COUNTRY_CROSSING:
-                       costing_options.AddMember("country_crossing_penalty","1", allocator);
+                       writer.Key("country_crossing_penalty");
+                       writer.String("1");
                        break;
                }
-               document.AddMember("costing_options", costing_options, allocator);
-               MAP_DEBUG("**********SET COSTING_OPTIONS: %f", costing_options.GetString());
-
+               writer.EndObject();
        }
 
-       /*rapidjson::Value& drive_style;
-       switch (style) {
-       case DRIVING_STYLE_NORMAL:
-               //TODO
-               break;
-       case DRIVING_STYLE_CAUTIOUS:
-               //TODO
-               break;
-       case DRIVING_STYLE_AGGRESSIVE:
-               //TODO
-               break;
-       }
-       document.AddMember("driving_style", driving_style, allocator);*/
+       // end json request object
+    writer.EndObject();
+
+       MAP_DEBUG("********** query_route: buffer=%s", buffer.GetString());
 
-       //Stringify JSON
-       rapidjson::StringBuffer buffer;
-       rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
-       document.Accept(writer);
        const char* jsonString = buffer.GetString();
 
        //we think that this is already being encoded somewhere else