File: goliveapi-censoredjson.cpp

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,928 kB
  • sloc: ansic: 202,137; cpp: 112,403; makefile: 868; python: 599; sh: 275; javascript: 19
file content (89 lines) | stat: -rw-r--r-- 2,024 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "goliveapi-censoredjson.hpp"
#include <unordered_map>
#include <nlohmann/json.hpp>

void censorRecurse(obs_data_t *);
void censorRecurseArray(obs_data_array_t *);

void censorRecurse(obs_data_t *data)
{
	// if we found what we came to censor, censor it
	const char *a = obs_data_get_string(data, "authentication");
	if (a && *a) {
		obs_data_set_string(data, "authentication", "CENSORED");
	}

	// recurse to child objects and arrays
	obs_data_item_t *item = obs_data_first(data);
	for (; item != NULL; obs_data_item_next(&item)) {
		enum obs_data_type typ = obs_data_item_gettype(item);

		if (typ == OBS_DATA_OBJECT) {
			obs_data_t *child_data = obs_data_item_get_obj(item);
			censorRecurse(child_data);
			obs_data_release(child_data);
		} else if (typ == OBS_DATA_ARRAY) {
			obs_data_array_t *child_array =
				obs_data_item_get_array(item);
			censorRecurseArray(child_array);
			obs_data_array_release(child_array);
		}
	}
}

void censorRecurseArray(obs_data_array_t *array)
{
	const size_t sz = obs_data_array_count(array);
	for (size_t i = 0; i < sz; i++) {
		obs_data_t *item = obs_data_array_item(array, i);
		censorRecurse(item);
		obs_data_release(item);
	}
}

QString censoredJson(obs_data_t *data, bool pretty)
{
	if (!data) {
		return "";
	}

	// Ugly clone via JSON write/read
	const char *j = obs_data_get_json(data);
	obs_data_t *clone = obs_data_create_from_json(j);

	// Censor our copy
	censorRecurse(clone);

	// Turn our copy into JSON
	QString s = pretty ? obs_data_get_json_pretty(clone)
			   : obs_data_get_json(clone);

	// Eliminate our copy
	obs_data_release(clone);

	return s;
}

using json = nlohmann::json;

void censorRecurse(json &data)
{
	if (!data.is_structured())
		return;

	auto it = data.find("authentication");
	if (it != data.end() && it->is_string()) {
		*it = "CENSORED";
	}

	for (auto &child : data) {
		censorRecurse(child);
	}
}

QString censoredJson(json data, bool pretty)
{
	censorRecurse(data);

	return QString::fromStdString(data.dump(pretty ? 4 : -1));
}