2015-04-17 16:19:30 +02:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "base/json/json_reader.h"
|
|
|
|
#include "base/json/json_writer.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "base/values.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_parser.h"
|
|
|
|
#include "cef/libcef/common/values_impl.h"
|
2015-04-17 16:19:30 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
int GetJSONReaderOptions(cef_json_parser_options_t options) {
|
|
|
|
int op = base::JSON_PARSE_RFC;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (options & JSON_PARSER_ALLOW_TRAILING_COMMAS) {
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSON_ALLOW_TRAILING_COMMAS;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-17 16:19:30 +02:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetJSONWriterOptions(cef_json_writer_options_t options) {
|
|
|
|
int op = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (options & JSON_WRITER_OMIT_BINARY_VALUES) {
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (options & JSON_WRITER_OMIT_DOUBLE_TYPE_PRESERVATION) {
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (options & JSON_WRITER_PRETTY_PRINT) {
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSONWriter::OPTIONS_PRETTY_PRINT;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-17 16:19:30 +02:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefRefPtr<CefValue> CefParseJSON(const CefString& json_string,
|
|
|
|
cef_json_parser_options_t options) {
|
|
|
|
const std::string& json = json_string.ToString();
|
2020-06-13 02:54:08 +02:00
|
|
|
return CefParseJSON(json.data(), json.size(), options);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefValue> CefParseJSON(const void* json,
|
|
|
|
size_t json_size,
|
|
|
|
cef_json_parser_options_t options) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!json || json_size == 0) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<base::Value> parse_result = base::JSONReader::Read(
|
2024-04-23 22:06:00 +02:00
|
|
|
std::string_view(static_cast<const char*>(json), json_size),
|
2020-06-13 02:54:08 +02:00
|
|
|
GetJSONReaderOptions(options));
|
2019-03-13 22:27:37 +01:00
|
|
|
if (parse_result) {
|
2023-01-30 22:42:40 +01:00
|
|
|
return new CefValueImpl(std::move(parse_result.value()));
|
2019-03-13 22:27:37 +01:00
|
|
|
}
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-04-17 16:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefValue> CefParseJSONAndReturnError(
|
|
|
|
const CefString& json_string,
|
|
|
|
cef_json_parser_options_t options,
|
|
|
|
CefString& error_msg_out) {
|
|
|
|
const std::string& json = json_string.ToString();
|
|
|
|
|
|
|
|
std::string error_msg;
|
2022-07-21 19:26:10 +02:00
|
|
|
auto result = base::JSONReader::ReadAndReturnValueWithError(
|
|
|
|
json, GetJSONReaderOptions(options));
|
|
|
|
if (result.has_value()) {
|
2023-01-30 22:42:40 +01:00
|
|
|
return new CefValueImpl(std::move(*result));
|
2019-03-13 22:27:37 +01:00
|
|
|
}
|
2015-04-17 16:19:30 +02:00
|
|
|
|
2022-07-21 19:26:10 +02:00
|
|
|
error_msg_out = result.error().message;
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-04-17 16:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefWriteJSON(CefRefPtr<CefValue> node,
|
|
|
|
cef_json_writer_options_t options) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!node.get() || !node->IsValid()) {
|
2015-04-17 16:19:30 +02:00
|
|
|
return CefString();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-17 16:19:30 +02:00
|
|
|
|
|
|
|
CefValueImpl* impl = static_cast<CefValueImpl*>(node.get());
|
|
|
|
CefValueImpl::ScopedLockedValue scoped_value(impl);
|
|
|
|
|
|
|
|
std::string json_string;
|
2017-05-17 11:29:28 +02:00
|
|
|
if (base::JSONWriter::WriteWithOptions(
|
|
|
|
*scoped_value.value(), GetJSONWriterOptions(options), &json_string)) {
|
2015-04-17 16:19:30 +02:00
|
|
|
return json_string;
|
|
|
|
}
|
|
|
|
return CefString();
|
|
|
|
}
|