-
Notifications
You must be signed in to change notification settings - Fork 1
/
list-json.cpp
86 lines (75 loc) · 2.16 KB
/
list-json.cpp
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
/*
* Copyright (c) 2015-2019 amded workers, All rights reserved.
* Terms for redistribution and use can be found in LICENCE.
*/
/**
* @file list-json.cpp
* @brief Tag reader frontend for machines via JSON.
*/
#include <cstdlib>
#include <iostream>
#include <memory>
#include <sstream>
#include <json/json.h>
#include <json/writer.h>
#include <b64/encode.h>
#include "list-json.h"
#include "list.h"
#include "setup.h"
static Json::Value data;
static void
push_item(const std::string &file, std::pair<const std::string, Value> &value)
{
switch (value.second.get_type()) {
case TAG_INTEGER:
data[file][value.first] = value.second.get_int();
break;
case TAG_BOOLEAN:
data[file][value.first] = value.second.get_bool();
break;
case TAG_STRING:
if (get_opt(AMDED_JSON_DONT_USE_BASE64)) {
data[file][value.first] =
value.second.get_str().toCString(true);
} else {
base64::encoder enc;
std::istringstream in {value.second.get_str().to8Bit(true)};
std::ostringstream out;
enc.encode(in, out);
data[file][value.first] = out.str();
}
break;
default:
std::cout << "Invalid type in: " << value.first << std::endl;
std::cout << "This is a bug. Please report!" << std::endl;
exit(EXIT_FAILURE);
}
}
void
amded_push_json(const struct amded_file &file)
{
std::map<std::string, Value> basics;
std::map<std::string, Value> tags;
std::map<std::string, Value> props;
basics = amded_list_amded(file);
tags = amded_list_tags(file);
props = amded_list_audioprops(file.fh->audioProperties());
for (auto &iter : basics) {
push_item(file.name, iter);
}
for (auto &iter : tags) {
push_item(file.name, iter);
}
for (auto &iter : props) {
push_item(file.name, iter);
}
}
void
amded_list_json(void)
{
Json::StreamWriterBuilder builder;
/* This makes the JSON output minimally packed. */
builder.settings_["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(data, &std::cout);
}