[mapguide-commits] r7271 - sandbox/jng/streaming/Web/src/HttpHandler

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Fri Dec 14 21:02:54 PST 2012


Author: jng
Date: 2012-12-14 21:02:54 -0800 (Fri, 14 Dec 2012)
New Revision: 7271

Modified:
   sandbox/jng/streaming/Web/src/HttpHandler/HttpReaderStreamer.cpp
Log:
#2194: Fix the structure of the streamed JSON content. Turns out the MgXmlJsonConvert class was outputting our fragments as JSON objects whereas the original implementation outputs them as JSON 1-element arrays (???). So we have to do some string gymnastics on our converted JSON results before they can be outputted to the http response stream.

Modified: sandbox/jng/streaming/Web/src/HttpHandler/HttpReaderStreamer.cpp
===================================================================
--- sandbox/jng/streaming/Web/src/HttpHandler/HttpReaderStreamer.cpp	2012-12-14 17:41:16 UTC (rev 7270)
+++ sandbox/jng/streaming/Web/src/HttpHandler/HttpReaderStreamer.cpp	2012-12-15 05:02:54 UTC (rev 7271)
@@ -30,38 +30,48 @@
 
     if (m_format == MgMimeType::Json)
     {
-        //MgXmlJsonConvert jsonConvert;
         std::string buf;
         std::string jsonbuf;
 
-        //HACK-ish: Split the converted response. Since the XML bits in question are just
-        //start and end tags, this is relatively simple.
-        //
-        //The JSON conversion should look like this:
-        //
-        // <ElementName></ElementName>
-        //
-        //becomes:
-        //
-        // {"ElementName":{}}
-        //
-        //We want to split this so that we have:
-        //
-        // Start - "ElementName":{
-        // End -   }
-
         //How this looks:
         //
-        // {"ResponseElementName":
-        //    {<header json>},
-        //  "BodyElementName":[
+        // {                            //outer JSON start
+        //   "ResponseElementName":     //root element name
+        //     {                        //root JSON property start
+        //      <header JSON pair>,
+        //      "BodyElementName":[     //body JSON array start
         //
         jsonbuf = "{\"";
         jsonbuf += m_reader->GetResponseElementName();
-        jsonbuf += "\":";
+        jsonbuf += "\":{";
         m_reader->HeaderToStringUtf8(buf);
         std::string jsonbuf2;
         ToJson(buf, jsonbuf2);
+        //This will have redudant outer { }, so strip them
+        jsonbuf2.erase(0, 1);
+        jsonbuf2.erase(jsonbuf2.length() - 2, 1);
+        //HACK: To match the original output, we have to array-ify this object (crazy? yes!)
+        //
+        //We currently have something like this
+        //
+        // "HeaderElementName":{
+        //    <prop1>:<val1>,
+        //    <prop2>:<val2>
+        // }
+        //
+        //We have to change it to this
+        //
+        // "HeaderElementName":[{
+        //    <prop1>:<val1>,
+        //    <prop2>:<val2>
+        // }]
+
+        //Find first instance of ": and insert [ after it. We use ": because a feature
+        //reader puts out xs:schema as the header element name
+        jsonbuf2.insert(jsonbuf2.find("\":") + 2, "[");
+        //Append ] to the end
+        jsonbuf2.append("]");
+
         jsonbuf += jsonbuf2;
         jsonbuf += ",\"";
         jsonbuf += m_reader->GetBodyElementName();
@@ -78,6 +88,20 @@
             //The body is a valid full XML element, so no need for gymnastics like its
             //surrounding elements
             ToJson(buf, jsonbuf);
+
+            //Strip outer { }
+            jsonbuf.erase(0, 1);
+            jsonbuf.erase(jsonbuf.length() - 2, 1);
+            //HACK: Same as the header, this needs to be array-ified to match the old output
+            //
+            //Find first instance of ": and insert [ after it.
+            jsonbuf.insert(jsonbuf.find("\":") + 2, "[");
+            //Append ] to the end
+            jsonbuf.append("]");
+            //Put back in outer { }
+            jsonbuf = "{" + jsonbuf;
+            jsonbuf += "}";
+
             bNext = m_reader->ReadNext();
             if (bNext)
                 jsonbuf += ",";
@@ -88,8 +112,10 @@
         jsonbuf.clear();
 
         // How this looks:
-        // ]}
-        jsonbuf = "]}";
+        //      ]   //End of body JSON array
+        //    }     //End of root JSON property
+        // }        //End of outer JSON
+        jsonbuf = "]}}";
         WriteChunk(jsonbuf.c_str(), jsonbuf.length());
     }
     else if (m_format == MgMimeType::Xml)



More information about the mapguide-commits mailing list