In AsyncSocketTest.SendMessageFlags test use folly::test::TemporaryFile object instea...
[folly.git] / folly / json.cpp
index 5adf787d2186a0538b9b08240003396d22c9a140..48ef486ebc87d3f17195f6d15c77d95726371121 100644 (file)
  */
 
 #include <folly/json.h>
+
+#include <algorithm>
 #include <cassert>
+#include <functional>
+
 #include <boost/next_prior.hpp>
 #include <boost/algorithm/string.hpp>
 
 #include <folly/Conv.h>
-#include <folly/Portability.h>
 #include <folly/Range.h>
 #include <folly/String.h>
 #include <folly/Unicode.h>
@@ -111,10 +114,13 @@ private:
     indent();
     newline();
     if (opts_.sort_keys) {
-      std::vector<std::pair<dynamic, dynamic>> items(
-        o.items().begin(), o.items().end());
-      std::sort(items.begin(), items.end());
-      printKVPairs(items.begin(), items.end());
+      using ref = std::reference_wrapper<decltype(o.items())::value_type const>;
+      std::vector<ref> refs(o.items().begin(), o.items().end());
+      std::sort(refs.begin(), refs.end(), [](ref a, ref b) {
+        // Only compare keys.  No ordering among identical keys.
+        return a.get().first < b.get().first;
+      });
+      printKVPairs(refs.cbegin(), refs.cend());
     } else {
       printKVPairs(o.items().begin(), o.items().end());
     }
@@ -172,25 +178,20 @@ private:
  serialization_opts const& opts_;
 };
 
-  //////////////////////////////////////////////////////////////////////
-
-  struct ParseError : std::runtime_error {
-    explicit ParseError(int line)
-      : std::runtime_error(to<std::string>("json parse error on line ", line))
-    {}
-
-    explicit ParseError(int line, std::string const& context,
-        std::string const& expected)
-      : std::runtime_error(to<std::string>("json parse error on line ", line,
-          !context.empty() ? to<std::string>(" near `", context, '\'')
-                          : "",
-          ": ", expected))
-    {}
+//////////////////////////////////////////////////////////////////////
 
-    explicit ParseError(std::string const& msg)
-      : std::runtime_error("json parse error: " + msg)
-    {}
-  };
+struct ParseError : std::runtime_error {
+  explicit ParseError(
+      unsigned int line,
+      std::string const& context,
+      std::string const& expected)
+      : std::runtime_error(to<std::string>(
+            "json parse error on line ",
+            line,
+            !context.empty() ? to<std::string>(" near `", context, '\'') : "",
+            ": ",
+            expected)) {}
+};
 
 // Wraps our input buffer with some helper functions.
 struct Input {
@@ -481,7 +482,7 @@ std::string decodeUnicodeEscape(Input& in) {
       in.error("expected 4 hex digits");
     }
 
-    uint16_t ret = hexVal(*in) * 4096;
+    uint16_t ret = uint16_t(hexVal(*in) * 4096);
     ++in;
     ret += hexVal(*in) * 256;
     ++in;
@@ -663,12 +664,12 @@ void escapeString(
           // note that this if condition captures non readable chars
           // with value < 32, so size = 1 byte (e.g control chars).
           out.append("\\u00");
-          out.push_back(hexDigit((*p & 0xf0) >> 4));
-          out.push_back(hexDigit(*p & 0xf));
+          out.push_back(hexDigit(uint8_t((*p & 0xf0) >> 4)));
+          out.push_back(hexDigit(uint8_t(*p & 0xf)));
           p++;
       }
     } else {
-      out.push_back(*p++);
+      out.push_back(char(*p++));
     }
   }