Skip to content

Instantly share code, notes, and snippets.

@iamOgunyinka
Last active November 3, 2021 11:49
Show Gist options
  • Save iamOgunyinka/44932ab6aa23b1476f6e2b438f1d993f to your computer and use it in GitHub Desktop.
Save iamOgunyinka/44932ab6aa23b1476f6e2b438f1d993f to your computer and use it in GitHub Desktop.
Add JSON print for desciptors
// Aseprite PSD Library
// Copyright (C) 2021 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef PSD_DEBUG_H_INCLUDED
#define PSD_DEBUG_H_INCLUDED
#pragma once
#include "json.hpp"
#include "psd.h"
using json = nlohmann::json;
namespace psd {
inline json pretty_print(const OSType* ptr)
{
//using key_type = std::result_of_t<decltype(std::declval<OSType>().type())>::type;
using key_type = OSTypeKey;
switch (ptr->type()) {
case key_type::Descriptor: {
json::object_t root_object;
const auto desc = static_cast<const OSTypeDescriptor*>(ptr);
root_object["class_name"] = desc->classId.name;
root_object["desc_name"] = desc->descriptorName;
json::object_t object;
for (const auto& item: desc->descriptors) {
object[item.first] = pretty_print(item.second.get());
}
root_object["desc"] = object;
return root_object;
}
case key_type::List: {
json::array_t json_list;
const auto list = static_cast<const OSTypeList*>(ptr);
for (const auto& value: list->values) {
json_list.push_back(pretty_print(value.get()));
}
return json_list;
}
case key_type::Double:
return static_cast<const OSTypeDouble*>(ptr)->value;
case key_type::Long:
return static_cast<const OSTypeInt*>(ptr)->value;
case key_type::LargeInteger:
return static_cast<const OSTypeLargeInt*>(ptr)->value;
case key_type::String:
return static_cast<const OSTypeString*>(ptr)->value;
case key_type::Boolean:
return static_cast<const OSTypeBoolean*>(ptr)->value;
case key_type::UnitFloat: {
json::object_t obj;
const auto unitPtr = static_cast<const OSTypeUnitFloat*>(ptr);
obj["value"] = unitPtr->value;
obj["unit"] = unitPtr->unit;
return obj;
}
default:
return std::string();
}
}
}
#undef TRACE
#define TRACE std::printf
#define PSD_JSON_PRINT(value) (TRACE("%s\n", \
json(psd::pretty_print(value.get())).dump(1).c_str()))
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment