X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fjson.h;h=458ab63a3b67686777aa7ee0d7ebc63b8bdeb921;hb=f429f348e5c986a4595fc98025178769d57c6ca6;hp=149ca8ff58a73f19dc9d66869d888fe22aa96c02;hpb=bfa6ffb55e8a371130b1d391926afd3030a8cedc;p=folly.git diff --git a/folly/json.h b/folly/json.h index 149ca8ff..458ab63a 100644 --- a/folly/json.h +++ b/folly/json.h @@ -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 +#include +#include namespace folly { @@ -51,6 +51,27 @@ namespace folly { namespace json { + ////////////////////////////////////////////////////////////////////// + + struct ParseError : std::runtime_error { + explicit ParseError(int line) + : std::runtime_error(to("json parse error on line ", line)) + {} + + explicit ParseError(int line, std::string const& context, + std::string const& expected) + : std::runtime_error(to("json parse error on line ", line, + !context.empty() ? to(" 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); } //////////////////////////////////////////////////////////////////////