Move json util code to common
So that it can be shared by CE CDM extraction tool later. Test: m wv_factory_extraction_tool Bug: 414642286 Change-Id: I6dba70227ce2789dd3686ebbf6ed3a0dbf68dc00
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
// Copyright 2025 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#ifndef WIDEVINE_JSON_UTILS_H_
|
||||
#define WIDEVINE_JSON_UTILS_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace widevine {
|
||||
|
||||
std::string EscapeJson(const std::string& input);
|
||||
std::string StringMapToJson(
|
||||
const std::map<std::string, std::string>& string_map);
|
||||
|
||||
} // namespace widevine
|
||||
|
||||
#endif // WIDEVINE_JSON_UTILS_H_
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2025 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "json_utils.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace widevine {
|
||||
|
||||
std::string EscapeJson(const std::string& input) {
|
||||
std::string result;
|
||||
for (const char& c : input) {
|
||||
switch (c) {
|
||||
case '\"':
|
||||
result += "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
result += "\\\\";
|
||||
break;
|
||||
case '\b':
|
||||
result += "\\b";
|
||||
break;
|
||||
case '\f':
|
||||
result += "\\f";
|
||||
break;
|
||||
case '\n':
|
||||
result += "\\n";
|
||||
break;
|
||||
case '\r':
|
||||
result += "\\r";
|
||||
break;
|
||||
case '\t':
|
||||
result += "\\t";
|
||||
break;
|
||||
default:
|
||||
result += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string StringMapToJson(
|
||||
const std::map<std::string, std::string>& string_map) {
|
||||
if (string_map.empty()) {
|
||||
return "{}";
|
||||
}
|
||||
std::ostringstream json_stream;
|
||||
json_stream << "{";
|
||||
bool is_first_element = true;
|
||||
for (const auto& pair : string_map) {
|
||||
if (!is_first_element) {
|
||||
json_stream << ",";
|
||||
}
|
||||
json_stream << "\"" << pair.first << "\": \"" << pair.second << "\"";
|
||||
is_first_element = false;
|
||||
}
|
||||
json_stream << "}";
|
||||
return json_stream.str();
|
||||
}
|
||||
|
||||
} // namespace widevine
|
||||
Reference in New Issue
Block a user