userver: samples/json2yaml/json2yaml.cpp
Loading...
Searching...
No Matches
samples/json2yaml/json2yaml.cpp
#include <iostream>
#include <string>
USERVER_NAMESPACE_BEGIN
namespace formats::parse {
yaml::Value Convert(const json::Value& json, parse::To<yaml::Value>) {
yaml::ValueBuilder yaml_vb;
if (json.IsBool()) {
yaml_vb = json.ConvertTo<bool>();
} else if (json.IsInt64()) {
yaml_vb = json.ConvertTo<int64_t>();
} else if (json.IsUInt64()) {
yaml_vb = json.ConvertTo<uint64_t>();
} else if (json.IsDouble()) {
yaml_vb = json.ConvertTo<double>();
} else if (json.IsString()) {
yaml_vb = json.ConvertTo<std::string>();
} else if (json.IsArray()) {
yaml_vb = {common::Type::kArray};
for (const auto& elem : json) {
yaml_vb.PushBack(elem.ConvertTo<yaml::Value>());
}
} else if (json.IsObject()) {
yaml_vb = {common::Type::kObject};
for (auto it = json.begin(); it != json.end(); ++it) {
yaml_vb[it.GetName()] = it->ConvertTo<yaml::Value>();
}
}
return yaml_vb.ExtractValue();
}
} // namespace formats::parse
USERVER_NAMESPACE_END
int main() {
namespace formats = USERVER_NAMESPACE::formats;
auto json = formats::json::FromStream(std::cin);
formats::yaml::Serialize(json.ConvertTo<formats::yaml::Value>(), std::cout);
std::cout << std::endl;
}