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 "include/cef_parser.h"
|
|
|
|
#include "libcef/common/values_impl.h"
|
|
|
|
|
|
|
|
#include "base/json/json_reader.h"
|
|
|
|
#include "base/json/json_writer.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "base/values.h"
|
2015-04-17 16:19:30 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
int GetJSONReaderOptions(cef_json_parser_options_t options) {
|
|
|
|
int op = base::JSON_PARSE_RFC;
|
|
|
|
if (options & JSON_PARSER_ALLOW_TRAILING_COMMAS)
|
|
|
|
op |= base::JSON_ALLOW_TRAILING_COMMAS;
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetJSONWriterOptions(cef_json_writer_options_t options) {
|
|
|
|
int op = 0;
|
2019-05-08 17:10:19 +02:00
|
|
|
if (options & JSON_WRITER_OMIT_BINARY_VALUES)
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES;
|
2019-05-08 17:10:19 +02: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;
|
2019-05-08 17:10:19 +02:00
|
|
|
if (options & JSON_WRITER_PRETTY_PRINT)
|
2015-04-17 16:19:30 +02:00
|
|
|
op |= base::JSONWriter::OPTIONS_PRETTY_PRINT;
|
|
|
|
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) {
|
|
|
|
if (!json || json_size == 0)
|
|
|
|
return nullptr;
|
2021-06-04 03:34:56 +02:00
|
|
|
absl::optional<base::Value> parse_result = base::JSONReader::Read(
|
2020-06-13 02:54:08 +02:00
|
|
|
base::StringPiece(static_cast<const char*>(json), json_size),
|
|
|
|
GetJSONReaderOptions(options));
|
2019-03-13 22:27:37 +01:00
|
|
|
if (parse_result) {
|
|
|
|
return new CefValueImpl(
|
|
|
|
base::Value::ToUniquePtrValue(std::move(parse_result.value()))
|
|
|
|
.release());
|
|
|
|
}
|
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()) {
|
2019-03-13 22:27:37 +01:00
|
|
|
return new CefValueImpl(
|
2022-07-21 19:26:10 +02:00
|
|
|
base::Value::ToUniquePtrValue(std::move(*result)).release());
|
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) {
|
|
|
|
if (!node.get() || !node->IsValid())
|
|
|
|
return CefString();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|