Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
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
25class Path;
26using VfsPath = Path;
27class ScriptRequest;
28
29/**
30 * @file JSON.h
31 * Contains JSON and more generally object-string conversion functions.
32 */
33
34namespace 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 */
40std::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 */
47bool 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 */
52void 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 */
57std::string StringifyJSON(const ScriptRequest& rq, JS::MutableHandleValue obj, bool indent = true);
58}
59
60#endif // INCLUDED_SCRIPTINTERFACE_JSON
Definition: path.h:80
Path()
Definition: path.h:84
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
Wraps SM APIs for manipulating JS objects.
Definition: JSON.h:35
bool ParseJSON(const ScriptRequest &rq, const std::string &string_utf8, JS::MutableHandleValue out)
Parse a UTF-8-encoded JSON string.
Definition: JSON.cpp:49
void ReadJSONFile(const ScriptRequest &rq, const VfsPath &path, JS::MutableHandleValue out)
Read a JSON file.
Definition: JSON.cpp:60
std::string StringifyJSON(const ScriptRequest &rq, JS::MutableHandleValue obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
Definition: JSON.cpp:104
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:118
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407