File: json_file.cpp

package info (click to toggle)
zytrax 0%2Bgit20201215-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,488 kB
  • sloc: cpp: 41,800; ansic: 3,387; makefile: 8; sh: 3
file content (42 lines) | stat: -rw-r--r-- 872 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "json_file.h"

Error save_json(const String &p_path, const JSON::Node &p_node) {
	JSON::Writer w;
	std::string str;
	w.setFormat(JSON::StandardFormat);
	w.writeString(p_node, str);

#ifdef WINDOWS_ENABLED
	FILE *f = _wfopen(p_path.c_str(), L"wb");
#else
	FILE *f = fopen(p_path.utf8().get_data(), "wb");
#endif
	ERR_FAIL_COND_V(!f, ERR_FILE_CANT_OPEN);
	fwrite(&str[0], str.length(), 1, f);
	fclose(f);
	return OK;
}
Error load_json(const String &p_path, JSON::Node &p_node) {

#ifdef WINDOWS_ENABLED
	FILE *f = _wfopen(p_path.c_str(), L"rb");
#else
	FILE *f = fopen(p_path.utf8().get_data(), "rb");
#endif
	ERR_FAIL_COND_V(!f, ERR_FILE_CANT_OPEN);

	std::string str;

	fseek(f, 0, SEEK_END);
	size_t pos = ftell(f);
	str.resize(pos);
	fseek(f, 0, SEEK_SET);
	fread(&str[0], pos, 1, f);

	JSON::Parser p;
	p_node = p.parseString(str);

	fclose(f);

	return OK;
}