1 // Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
4
5 #include "include/cef_parser.h"
6 #include "libcef/common/values_impl.h"
7
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11
12 namespace {
13
GetJSONReaderOptions(cef_json_parser_options_t options)14 int GetJSONReaderOptions(cef_json_parser_options_t options) {
15 int op = base::JSON_PARSE_RFC;
16 if (options & JSON_PARSER_ALLOW_TRAILING_COMMAS)
17 op |= base::JSON_ALLOW_TRAILING_COMMAS;
18 return op;
19 }
20
GetJSONWriterOptions(cef_json_writer_options_t options)21 int GetJSONWriterOptions(cef_json_writer_options_t options) {
22 int op = 0;
23 if (options & JSON_WRITER_OMIT_BINARY_VALUES)
24 op |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES;
25 if (options & JSON_WRITER_OMIT_DOUBLE_TYPE_PRESERVATION)
26 op |= base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION;
27 if (options & JSON_WRITER_PRETTY_PRINT)
28 op |= base::JSONWriter::OPTIONS_PRETTY_PRINT;
29 return op;
30 }
31
32 } // namespace
33
CefParseJSON(const CefString & json_string,cef_json_parser_options_t options)34 CefRefPtr<CefValue> CefParseJSON(const CefString& json_string,
35 cef_json_parser_options_t options) {
36 const std::string& json = json_string.ToString();
37 return CefParseJSON(json.data(), json.size(), options);
38 }
39
CefParseJSON(const void * json,size_t json_size,cef_json_parser_options_t options)40 CefRefPtr<CefValue> CefParseJSON(const void* json,
41 size_t json_size,
42 cef_json_parser_options_t options) {
43 if (!json || json_size == 0)
44 return nullptr;
45 absl::optional<base::Value> parse_result = base::JSONReader::Read(
46 base::StringPiece(static_cast<const char*>(json), json_size),
47 GetJSONReaderOptions(options));
48 if (parse_result) {
49 return new CefValueImpl(
50 base::Value::ToUniquePtrValue(std::move(parse_result.value()))
51 .release());
52 }
53 return nullptr;
54 }
55
CefParseJSONAndReturnError(const CefString & json_string,cef_json_parser_options_t options,CefString & error_msg_out)56 CefRefPtr<CefValue> CefParseJSONAndReturnError(
57 const CefString& json_string,
58 cef_json_parser_options_t options,
59 CefString& error_msg_out) {
60 const std::string& json = json_string.ToString();
61
62 std::string error_msg;
63 base::JSONReader::ValueWithError value_and_error =
64 base::JSONReader::ReadAndReturnValueWithError(
65 json, GetJSONReaderOptions(options));
66 if (value_and_error.value) {
67 return new CefValueImpl(
68 base::Value::ToUniquePtrValue(std::move(value_and_error.value.value()))
69 .release());
70 }
71
72 error_msg_out = value_and_error.error_message;
73 return nullptr;
74 }
75
CefWriteJSON(CefRefPtr<CefValue> node,cef_json_writer_options_t options)76 CefString CefWriteJSON(CefRefPtr<CefValue> node,
77 cef_json_writer_options_t options) {
78 if (!node.get() || !node->IsValid())
79 return CefString();
80
81 CefValueImpl* impl = static_cast<CefValueImpl*>(node.get());
82 CefValueImpl::ScopedLockedValue scoped_value(impl);
83
84 std::string json_string;
85 if (base::JSONWriter::WriteWithOptions(
86 *scoped_value.value(), GetJSONWriterOptions(options), &json_string)) {
87 return json_string;
88 }
89 return CefString();
90 }
91