Files
grpc/examples/cpp/route_guide/helper.cc
Qucheng Jiang 93971c4b57 [cpp/route_guide] Fix JSON minification removing spaces inside string values (#38954) (#38955)
[cpp/route_guide] Fix JSON minification removing spaces inside string values (#38954)

The existing JSON minification logic removes all whitespace characters using 'std::remove_if(db_.begin(), db_.end(), isspace);'. This approach inadvertently strips spaces inside JSON string values, leading to incorrect data parsing.

This commit introduces a new JSON minification function that:
- Removes spaces **outside** JSON string values.
- Preserves spaces **inside** JSON string values (`"..."`).
- Correctly handles escaped quotes (`\"`).

Fixes #38954

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->
## Verification

### Before Fix
```bash
> ./cmake/build/route_guide_client
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1741496886.397564  341105 helper.cc:201] DB parsed, loaded 100 features.
-------------- GetFeature --------------
Found feature called BerkshireValleyManagementAreaTrail,Jefferson,NJ,USA at 40.9146, -74.6189
Found no feature at 0, 0
-------------- ListFeatures --------------
Looking for features between 40, -75 and 42, -73
Found feature called PatriotsPath,Mendham,NJ07945,USA at 40.7838, -74.6144
Found feature called 101NewJersey10,Whippany,NJ07981,USA at 40.8123, -74.3999
Found feature called U.S.6,Shohola,PA18458,USA at 41.3628, -74.9016
Found feature called 5ConnersRoad,Kingston,NY12401,USA at 42, -74.0371
Found feature called MidHudsonPsychiatricCenter,NewHampton,NY10958,USA at 41.4008, -74.3951
Found feature called 287FlugertownRoad,LivingstonManor,NY12758,USA at 41.9611, -74.6525
Found feature called 4001TremleyPointRoad,Linden,NJ07036,USA at 40.611, -74.2187
Found feature called 352SouthMountainRoad,Wallkill,NY12589,USA at 41.6802, -74.237
Found feature called BaileyTurnRoad,Harriman,NY10926,USA at 41.295, -74.1077
Found feature called 193-199WawayandaRoad,Hewitt,NJ07421,USA at 41.2145, -74.395
Found feature called 406-496WardAvenue,PineBush,NY12566,USA at 41.5737, -74.2848
...
```

### After Fix
```bash
> ./cmake/build/route_guide_client
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1741496644.493263  337350 helper.cc:203] DB parsed, loaded 100 features.
-------------- GetFeature --------------
Found feature called Berkshire Valley Management Area Trail, Jefferson, NJ, USA at 40.9146, -74.6189
Found no feature at 0, 0
-------------- ListFeatures --------------
Looking for features between 40, -75 and 42, -73
Found feature called Patriots Path, Mendham, NJ 07945, USA at 40.7838, -74.6144
Found feature called 101 New Jersey 10, Whippany, NJ 07981, USA at 40.8123, -74.3999
Found feature called U.S. 6, Shohola, PA 18458, USA at 41.3628, -74.9016
Found feature called 5 Conners Road, Kingston, NY 12401, USA at 42, -74.0371
Found feature called Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA at 41.4008, -74.3951
Found feature called 287 Flugertown Road, Livingston Manor, NY 12758, USA at 41.9611, -74.6525
Found feature called 4001 Tremley Point Road, Linden, NJ 07036, USA at 40.611, -74.2187
Found feature called 352 South Mountain Road, Wallkill, NY 12589, USA at 41.6802, -74.237
Found feature called Bailey Turn Road, Harriman, NY 10926, USA at 41.295, -74.1077
Found feature called 193-199 Wawayanda Road, Hewitt, NJ 07421, USA at 41.2145, -74.395
Found feature called 406-496 Ward Avenue, Pine Bush, NY 12566, USA at 41.5737, -74.2848
...
```

Closes #38955

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/38955 from jiangqucheng:master 54089ec53b547f9c90e672104a668c12930f43b7
PiperOrigin-RevId: 744962894
2025-04-07 21:14:15 -07:00

162 lines
4.4 KiB
C++

/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/log/log.h"
#ifdef BAZEL_BUILD
#include "examples/protos/route_guide.grpc.pb.h"
#else
#include "route_guide.grpc.pb.h"
#endif
#ifdef BAZEL_BUILD
ABSL_FLAG(std::string, db_path, "examples/cpp/route_guide/route_guide_db.json",
"Path to db file");
#else
ABSL_FLAG(std::string, db_path, "route_guide_db.json", "Path to db file");
#endif
namespace routeguide {
std::string GetDbFileContent(int argc, char** argv) {
std::string db_path = absl::GetFlag(FLAGS_db_path);
std::ifstream db_file(db_path);
if (!db_file.is_open()) {
LOG(ERROR) << "Failed to open " << db_path;
abort();
}
std::stringstream db;
db << db_file.rdbuf();
return db.str();
}
// A simple parser for the json db file. It requires the db file to have the
// exact form of [{"location":{"latitude":123,"longitude":456},"name":
// "the name can be empty"},{ ... }...
class Parser {
public:
explicit Parser(const std::string& db) : db_(db) {
if (!Match("[")) {
SetFailedAndReturnFalse();
}
}
bool Finished() { return current_ >= db_.size(); }
bool TryParseOne(Feature* feature) {
if (failed_ || Finished() || !Match("{")) {
return SetFailedAndReturnFalse();
}
if (!Match(location_) || !Match("{") || !Match(latitude_)) {
return SetFailedAndReturnFalse();
}
long temp = 0;
ReadLong(&temp);
feature->mutable_location()->set_latitude(temp);
if (!Match(",") || !Match(longitude_)) {
return SetFailedAndReturnFalse();
}
ReadLong(&temp);
feature->mutable_location()->set_longitude(temp);
if (!Match("},") || !Match(name_) || !Match("\"")) {
return SetFailedAndReturnFalse();
}
size_t name_start = current_;
while (current_ != db_.size() && db_[current_++] != '"') {
}
if (current_ == db_.size()) {
return SetFailedAndReturnFalse();
}
feature->set_name(db_.substr(name_start, current_ - name_start - 1));
if (!Match("},")) {
if (db_[current_ - 1] == ']' && current_ == db_.size()) {
return true;
}
return SetFailedAndReturnFalse();
}
return true;
}
private:
bool SetFailedAndReturnFalse() {
failed_ = true;
return false;
}
bool Match(const std::string& prefix) {
bool eq = db_.substr(current_, prefix.size()) == prefix;
current_ += prefix.size();
return eq;
}
void ReadLong(long* l) {
size_t start = current_;
while (current_ != db_.size() && db_[current_] != ',' &&
db_[current_] != '}') {
current_++;
}
// It will throw an exception if fails.
*l = std::stol(db_.substr(start, current_ - start));
}
bool failed_ = false;
std::string db_;
size_t current_ = 0;
const std::string location_ = "\"location\":";
const std::string latitude_ = "\"latitude\":";
const std::string longitude_ = "\"longitude\":";
const std::string name_ = "\"name\":";
};
// Minifies a JSON string by removing all whitespace characters outside of
// strings.
std::string MinifyJson(const std::string& json) {
std::regex whitespaceOutsideQuotes(R"(\s+(?=(?:(?:[^"]*"){2})*[^"]*$))");
// Replace all matches with an empty string
return std::regex_replace(json, whitespaceOutsideQuotes, "");
}
void ParseDb(const std::string& db, std::vector<Feature>* feature_list) {
feature_list->clear();
std::string db_content(MinifyJson(db));
Parser parser(db_content);
Feature feature;
while (!parser.Finished()) {
feature_list->push_back(Feature());
if (!parser.TryParseOne(&feature_list->back())) {
LOG(ERROR) << "Error parsing the db file";
feature_list->clear();
break;
}
}
LOG(INFO) << "DB parsed, loaded " << feature_list->size() << " features.";
}
} // namespace routeguide