Pyrogenesis  trunk
JSON.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SCRIPTINTERFACE_JSON
19 #define INCLUDED_SCRIPTINTERFACE_JSON
20 
21 #include "ScriptForward.h"
22 
23 #include <string>
24 
25 class Path;
26 using VfsPath = Path;
27 class ScriptRequest;
28 
29 /**
30  * @file JSON.h
31  * Contains JSON and more generally object-string conversion functions.
32  */
33 
34 namespace Script
35 {
36 /**
37  * Convert an object to a UTF-8 encoded string, either with JSON
38  * (if pretty == true and there is no JSON error) or with toSource().
39  */
40 std::string ToString(const ScriptRequest& rq, JS::MutableHandleValue obj, bool pretty = false);
41 
42 /**
43  * Parse a UTF-8-encoded JSON string. Returns the unmodified value on error
44  * and prints an error message.
45  * @return true on success; false otherwise
46  */
47 bool ParseJSON(const ScriptRequest& rq, const std::string& string_utf8, JS::MutableHandleValue out);
48 
49 /**
50  * Read a JSON file. Returns the unmodified value on error and prints an error message.
51  */
52 void ReadJSONFile(const ScriptRequest& rq, const VfsPath& path, JS::MutableHandleValue out);
53 
54 /**
55  * Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
56  */
57 std::string StringifyJSON(const ScriptRequest& rq, JS::MutableHandleValue obj, bool indent = true);
58 }
59 
60 #endif // INCLUDED_SCRIPTINTERFACE_JSON
std::string ToString(const ScriptRequest &rq, JS::MutableHandleValue obj, bool pretty=false)
Convert an object to a UTF-8 encoded string, either with JSON (if pretty == true and there is no JSON...
Definition: JSON.cpp:117
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:421
bool ParseJSON(const ScriptRequest &rq, const std::string &string_utf8, JS::MutableHandleValue out)
Parse a UTF-8-encoded JSON string.
Definition: JSON.cpp:48
Wraps SM APIs for manipulating JS objects.
Definition: JSON.h:34
std::string StringifyJSON(const ScriptRequest &rq, JS::MutableHandleValue obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
Definition: JSON.cpp:103
Definition: path.h:79
void ReadJSONFile(const ScriptRequest &rq, const VfsPath &path, JS::MutableHandleValue out)
Read a JSON file.
Definition: JSON.cpp:59
Path()
Definition: path.h:84
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59