Browse Source

Merge pull request #6274 from afishbeck/roxie-json-scalar-data

Roxie json output of scalar data should be quoted

Reviewed-by: Gavin Halliday <ghalliday@hpccsystems.com>
Gavin Halliday 11 years ago
parent
commit
4b647d33c0
3 changed files with 40 additions and 21 deletions
  1. 33 6
      common/thorhelper/roxiehelper.cpp
  2. 4 2
      common/thorhelper/roxiehelper.hpp
  3. 3 13
      roxie/ccd/ccdcontext.cpp

+ 33 - 6
common/thorhelper/roxiehelper.cpp

@@ -743,7 +743,7 @@ void CSafeSocket::sendException(const char *source, unsigned code, const char *m
         response.startDataset("Exception", NULL, (unsigned) -1);
         response.appendf("<Source>%s</Source><Code>%d</Code>", source, code);
         response.append("<Message>");
-        response.encodeXML(message);
+        response.encodeString(message, strlen(message));
         response.append("</Message>");
     }
     catch(IException *EE)
@@ -873,11 +873,32 @@ void FlushingStringBuffer::appendf(const char *format, ...)
     append(t.length(), t.str());
 }
 
-void FlushingStringBuffer::encodeXML(const char *x, unsigned flags, unsigned len, bool utf8)
+void FlushingStringBuffer::encodeString(const char *x, unsigned len, bool utf8)
 {
-    StringBuffer t;
-    ::encodeXML(x, t, flags, len, utf8);
-    append(t.length(), t.str());
+    if (mlFmt==MarkupFmt_XML)
+    {
+        StringBuffer t;
+        ::encodeXML(x, t, 0, len, utf8);
+        append(t.length(), t.str());
+    }
+    else
+        append(len, x);
+}
+
+void FlushingStringBuffer::encodeData(const void *data, unsigned len)
+{
+    static char hexchar[] = "0123456789ABCDEF";
+    if (mlFmt==MarkupFmt_XML)
+    {
+        const byte *field = (const byte *) data;
+        for (int i = 0; i < len; i++)
+        {
+            append(hexchar[field[i] >> 4]);
+            append(hexchar[field[i] & 0x0f]);
+        }
+    }
+    else
+        append(len, (const char *) data);
 }
 
 void FlushingStringBuffer::addPayload(StringBuffer &s, unsigned int reserve)
@@ -1096,12 +1117,18 @@ void FlushingStringBuffer::incrementRowCount()
     rowCount++;
 }
 
-void FlushingJsonBuffer::encodeXML(const char *x, unsigned flags, unsigned len, bool utf8)
+void FlushingJsonBuffer::encodeString(const char *x, unsigned len, bool utf8)
 {
     CriticalBlock b(crit);
     appendJSONStringValue(s, NULL, len, x, true);
 }
 
+void FlushingJsonBuffer::encodeData(const void *data, unsigned len)
+{
+    CriticalBlock b(crit);
+    appendJSONDataValue(s, NULL, len, data);
+}
+
 void FlushingJsonBuffer::startDataset(const char *elementName, const char *resultName, unsigned sequence, bool _extend, const IProperties *xmlns)
 {
     CriticalBlock b(crit);

+ 4 - 2
common/thorhelper/roxiehelper.hpp

@@ -155,7 +155,8 @@ public:
     virtual void append(const char *data);
     virtual void append(unsigned len, const char *data);
     virtual void appendf(const char *format, ...) __attribute__((format(printf, 2, 3)));
-    virtual void encodeXML(const char *x, unsigned flags=0, unsigned len=(unsigned)-1, bool utf8=false);
+    virtual void encodeString(const char *x, unsigned len, bool utf8=false);
+    virtual void encodeData(const void *data, unsigned len);
     virtual void flushXML(StringBuffer &current, bool isClosing);
     virtual void flush(bool closing) ;
     virtual void addPayload(StringBuffer &s, unsigned int reserve=0);
@@ -173,7 +174,8 @@ public:
     {
     }
 
-    void encodeXML(const char *x, unsigned flags=0, unsigned len=(unsigned)-1, bool utf8=false);
+    void encodeString(const char *x, unsigned len, bool utf8=false);
+    void encodeData(const void *data, unsigned len);
     void startDataset(const char *elementName, const char *resultName, unsigned sequence, bool _extend = false, const IProperties *xmlns=NULL);
     void startScalar(const char *resultName, unsigned sequence);
 };

+ 3 - 13
roxie/ccd/ccdcontext.cpp

@@ -3029,17 +3029,7 @@ public:
             if (r)
             {
                 r->startScalar(name, sequence);
-                if (isRaw)
-                    r->append(len, (const char *) data);
-                else
-                {
-                    const byte *field = (const byte *) data;
-                    for (int i = 0; i < len; i++)
-                    {
-                        r->append(hexchar[field[i] >> 4]);
-                        r->append(hexchar[field[i] & 0x0f]);
-                    }
-                }
+                r->encodeData(data, len);
             }
         }
         if (workUnit)
@@ -3437,7 +3427,7 @@ public:
                 }
                 else
                 {
-                    r->encodeXML(str, 0, len);
+                    r->encodeString(str, len);
                 }
             }
         }
@@ -3488,7 +3478,7 @@ public:
                     rtlDataAttr buff;
                     unsigned bufflen = 0;
                     rtlUnicodeToCodepageX(bufflen, buff.refstr(), len, str, "utf-8");
-                    r->encodeXML(buff.getstr(), 0, bufflen, true); // output as UTF-8
+                    r->encodeString(buff.getstr(), bufflen, true); // output as UTF-8
                 }
             }
         }