Improved Json methods to correct work with swig and inheritance.

This commit is contained in:
Jonathan Thomas
2013-12-07 21:09:55 -06:00
parent e3d41808b9
commit cd7743fb88
44 changed files with 787 additions and 221 deletions

View File

@@ -29,6 +29,17 @@
using namespace openshot;
/// Default constructor (blank text)
TextReader::TextReader() : width(1024), height(768), x_offset(0), y_offset(0), text(""), font("Arial"), size(10.0), text_color("#ffffff"), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER) {
// Init FileInfo struct (clear all values)
InitFileInfo();
// Open and Close the reader, to populate it's attributes (such as height, width, etc...)
Open();
Close();
}
TextReader::TextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, string text, string font, double size, string text_color, string background_color)
: width(width), height(height), x_offset(x_offset), y_offset(y_offset), text(text), font(font), size(size), text_color(text_color), background_color(background_color), is_open(false), gravity(gravity)
{
@@ -157,11 +168,19 @@ tr1::shared_ptr<Frame> TextReader::GetFrame(int requested_frame) throw(ReaderClo
}
// Generate JSON string of this object
string TextReader::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value TextReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["type"] = "TextReader";
root["width"] = width;
root["height"] = height;
root["x_offset"] = x_offset;
@@ -177,11 +196,34 @@ Json::Value TextReader::JsonValue() {
return root;
}
// Load JSON string into this object
void TextReader::SetJson(string value) throw(InvalidJSON) {
// Parse JSON string into JSON objects
Json::Value root;
Json::Reader reader;
bool success = reader.parse( value, root );
if (!success)
// Raise exception
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
try
{
// Set all values that match
SetJsonValue(root);
}
catch (exception e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
}
}
// Load Json::JsonValue into this object
void TextReader::Json(Json::Value root) throw(InvalidFile) {
void TextReader::SetJsonValue(Json::Value root) throw(InvalidFile) {
// Set parent data
ReaderBase::Json(root);
ReaderBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (root["width"] != Json::nullValue)
@@ -205,7 +247,10 @@ void TextReader::Json(Json::Value root) throw(InvalidFile) {
if (root["gravity"] != Json::nullValue)
gravity = (GravityType) root["gravity"].asInt();
// Open path, and re-init everything
Close();
Open();
// Re-Open path, and re-init everything (if needed)
if (is_open)
{
Close();
Open();
}
}