Better estimateSpaceNeeded(double)
[folly.git] / folly / json.h
index 149ca8ff58a73f19dc9d66869d888fe22aa96c02..458ab63a3b67686777aa7ee0d7ebc63b8bdeb921 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -41,9 +41,9 @@
 #ifndef FOLLY_JSON_H_
 #define FOLLY_JSON_H_
 
-#include "folly/dynamic.h"
-#include "folly/FBString.h"
-#include "folly/Range.h"
+#include <folly/dynamic.h>
+#include <folly/FBString.h>
+#include <folly/Range.h>
 
 namespace folly {
 
@@ -51,6 +51,27 @@ namespace folly {
 
 namespace json {
 
+  //////////////////////////////////////////////////////////////////////
+
+  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 serialization_opts {
     explicit serialization_opts()
       : allow_non_string_keys(false)
@@ -60,6 +81,10 @@ namespace json {
       , validate_utf8(false)
       , allow_trailing_comma(false)
       , sort_keys(false)
+      , skip_invalid_utf8(false)
+      , allow_nan_inf(false)
+      , double_mode(double_conversion::DoubleToStringConverter::SHORTEST)
+      , double_num_digits(0) // ignored when mode is SHORTEST
     {}
 
     // If true, keys in an object can be non-strings.  (In strict
@@ -89,6 +114,17 @@ namespace json {
 
     // Sort keys of all objects before printing out (potentially slow)
     bool sort_keys;
+
+    // Replace invalid utf8 characters with U+FFFD and continue
+    bool skip_invalid_utf8;
+
+    // true to allow NaN or INF values
+    bool allow_nan_inf;
+
+    // Options for how to print floating point values.  See Conv.h
+    // toAppend implementation for floating point for more info
+    double_conversion::DoubleToStringConverter::DtoaMode double_mode;
+    unsigned int double_num_digits;
   };
 
   /*
@@ -106,6 +142,11 @@ namespace json {
   void escapeString(StringPiece input,
                     fbstring& out,
                     const serialization_opts& opts);
+
+  /*
+   * Strip all C99-like comments (i.e. // and / * ... * /)
+   */
+  fbstring stripComments(StringPiece jsonC);
 }
 
 //////////////////////////////////////////////////////////////////////