Skip to content

Commit

Permalink
allowSingleQuotes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdunn2001 committed Feb 24, 2015
1 parent b9229b7 commit 0c66e69
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
- true if dropped null placeholders are allowed. (See StreamWriterBuilder.)
- `"allowNumericKeys": false or true`
- true if numeric object keys are allowed.
- `"allowSingleQuotes": false or true`
- true if '' are allowed for strings (both keys and values)
- `"stackLimit": integer`
- Exceeding stackLimit (recursive depth of `readValue()`) will
cause an exception.
Expand Down
44 changes: 31 additions & 13 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,12 @@ bool Reader::good() const {
class OurFeatures {
public:
static OurFeatures all();
static OurFeatures strictMode();
OurFeatures();
bool allowComments_;
bool strictRoot_;
bool allowDroppedNullPlaceholders_;
bool allowNumericKeys_;
bool allowSingleQuotes_;
bool failIfExtra_;
int stackLimit_;
}; // OurFeatures
Expand All @@ -923,20 +923,15 @@ class OurFeatures {
// ////////////////////////////////

OurFeatures::OurFeatures()
: allowComments_(true), strictRoot_(false),
allowDroppedNullPlaceholders_(false), allowNumericKeys_(false) {}
: allowComments_(true), strictRoot_(false)
, allowDroppedNullPlaceholders_(false), allowNumericKeys_(false)
, allowSingleQuotes_(false)
, failIfExtra_(false)
{
}

OurFeatures OurFeatures::all() { return OurFeatures(); }

OurFeatures OurFeatures::strictMode() {
OurFeatures features;
features.allowComments_ = false;
features.strictRoot_ = true;
features.allowDroppedNullPlaceholders_ = false;
features.allowNumericKeys_ = false;
return features;
}

// Implementation of class Reader
// ////////////////////////////////

Expand Down Expand Up @@ -1006,6 +1001,7 @@ class OurReader {
bool readCStyleComment();
bool readCppStyleComment();
bool readString();
bool readStringSingleQuote();
void readNumber();
bool readValue();
bool readObject(Token& token);
Expand Down Expand Up @@ -1220,6 +1216,12 @@ bool OurReader::readToken(Token& token) {
token.type_ = tokenString;
ok = readString();
break;
case '\'':
if (features_.allowSingleQuotes_) {
token.type_ = tokenString;
ok = readStringSingleQuote();
break;
} // else continue
case '/':
token.type_ = tokenComment;
ok = readComment();
Expand Down Expand Up @@ -1371,7 +1373,6 @@ void OurReader::readNumber() {
c = (current_ = p) < end_ ? *p++ : 0;
}
}

bool OurReader::readString() {
Char c = 0;
while (current_ != end_) {
Expand All @@ -1384,6 +1385,19 @@ bool OurReader::readString() {
return c == '"';
}


bool OurReader::readStringSingleQuote() {
Char c = 0;
while (current_ != end_) {
c = getNextChar();
if (c == '\\')
getNextChar();
else if (c == '\'')
break;
}
return c == '\'';
}

bool OurReader::readObject(Token& tokenStart) {
Token tokenName;
std::string name;
Expand Down Expand Up @@ -1878,6 +1892,7 @@ CharReader* CharReaderBuilder::newCharReader() const
features.strictRoot_ = settings_["strictRoot"].asBool();
features.allowDroppedNullPlaceholders_ = settings_["allowDroppedNullPlaceholders"].asBool();
features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool();
features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool();
features.stackLimit_ = settings_["stackLimit"].asInt();
features.failIfExtra_ = settings_["failIfExtra"].asBool();
return new OurCharReader(collectComments, features);
Expand All @@ -1890,6 +1905,7 @@ static void getValidReaderKeys(std::set<std::string>* valid_keys)
valid_keys->insert("strictRoot");
valid_keys->insert("allowDroppedNullPlaceholders");
valid_keys->insert("allowNumericKeys");
valid_keys->insert("allowSingleQuotes");
valid_keys->insert("stackLimit");
valid_keys->insert("failIfExtra");
}
Expand Down Expand Up @@ -1919,6 +1935,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings)
(*settings)["strictRoot"] = true;
(*settings)["allowDroppedNullPlaceholders"] = false;
(*settings)["allowNumericKeys"] = false;
(*settings)["allowSingleQuotes"] = false;
(*settings)["failIfExtra"] = true;
//! [CharReaderBuilderStrictMode]
}
Expand All @@ -1931,6 +1948,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings)
(*settings)["strictRoot"] = false;
(*settings)["allowDroppedNullPlaceholders"] = false;
(*settings)["allowNumericKeys"] = false;
(*settings)["allowSingleQuotes"] = false;
(*settings)["stackLimit"] = 1000;
(*settings)["failIfExtra"] = false;
//! [CharReaderBuilderDefaults]
Expand Down

0 comments on commit 0c66e69

Please sign in to comment.