Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
JSON.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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 <js/TypeDecls.h>
22#include <string>
23
24class Path;
25using VfsPath = Path;
26namespace Script { class Request; }
27
28/**
29 * @file JSON.h
30 * Contains JSON and more generally object-string conversion functions.
31 */
32
33namespace Script
34{
35/**
36 * Convert an object to a UTF-8 encoded string, either with JSON
37 * (if pretty == true and there is no JSON error) or with toSource().
38 */
39std::string ToString(const Script::Request& rq, JS::MutableHandleValue obj, bool pretty = false);
40
41/**
42 * Parse a UTF-8-encoded JSON string. Returns the unmodified value on error
43 * and prints an error message.
44 * @return true on success; false otherwise
45 */
46bool ParseJSON(const Script::Request& rq, const std::string& string_utf8, JS::MutableHandleValue out);
47
48/**
49 * Read a JSON file. Returns the unmodified value on error and prints an error message.
50 */
51void ReadJSONFile(const Script::Request& rq, const VfsPath& path, JS::MutableHandleValue out);
52
53/**
54 * Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
55 */
56std::string StringifyJSON(const Script::Request& rq, JS::MutableHandleValue obj, bool indent = true);
57}
58
59#endif // INCLUDED_SCRIPTINTERFACE_JSON
Definition: path.h:84
Path()
Definition: path.h:88
Definition: Request.h:51
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
bool ParseJSON(const Script::Request &rq, const std::string &string_utf8, JS::MutableHandleValue out)
Parse a UTF-8-encoded JSON string.
Definition: JSON.cpp:40
std::string ToString(const Script::Request &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:109
std::string StringifyJSON(const Script::Request &rq, JS::MutableHandleValue obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
Definition: JSON.cpp:95
void ReadJSONFile(const Script::Request &rq, const VfsPath &path, JS::MutableHandleValue out)
Read a JSON file.
Definition: JSON.cpp:51
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407