2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Add QPDFObjectHandle::getJSON()

This commit is contained in:
Jay Berkenbilt 2018-12-17 17:40:29 -05:00
parent 651179b5da
commit 30a0c070e4
28 changed files with 143 additions and 0 deletions

View File

@ -1,5 +1,9 @@
2018-12-18 Jay Berkenbilt <ejb@ql.org>
* New method QPDFObjectHandle::getJSON() returns a JSON object
with a partial representation of the object. See
QPDFObjectHandle.hh for a detailed description.
* Add a simple JSON serializer. This is not a complete or
general-purpose JSON library. It allows assembly and serialization
of JSON structures with some restrictions, which are described in

View File

@ -24,6 +24,7 @@
#include <qpdf/DLL.h>
#include <qpdf/PointerHolder.hh>
#include <qpdf/JSON.hh>
#include <string>
@ -62,6 +63,7 @@ class QPDFObject
virtual ~QPDFObject() {}
virtual std::string unparse() = 0;
virtual JSON getJSON() = 0;
// Return a unique type code for the object
virtual object_type_e getTypeCode() const = 0;

View File

@ -733,6 +733,25 @@ class QPDFObjectHandle
QPDF_DLL
std::string unparseBinary();
// Return encoded as JSON. For most object types, there is an
// obvious mapping. The JSON is generated as follows:
// * Names are encoded as strings representing the normalized value of
// getName()
// * Indirect references are encoded as strings containing "obj gen R"
// * Strings are encoded as UTF-8 strings with unrepresentable binary
// characters encoded as \uHHHH
// * Encoding streams just encodes the stream's dictionary; the stream
// data is not represented
// * Object types that are only valid in content streams (inline
// image, operator) as well as "reserved" objects are not
// representable and will be serialized as "null".
// If dereference_indirect is true and this is an indirect object,
// show the actual contents of the object. The effect of
// dereference_indirect applies only to this object. It is not
// recursive.
QPDF_DLL
JSON getJSON(bool dereference_indirect = false);
// Legacy helper methods for commonly performed operations on
// pages. Newer code should use QPDFPageObjectHelper instead. The
// specification and behavior of these methods are the same as the

View File

@ -1235,6 +1235,25 @@ QPDFObjectHandle::unparseBinary()
}
}
JSON
QPDFObjectHandle::getJSON(bool dereference_indirect)
{
if ((! dereference_indirect) && this->isIndirect())
{
return JSON::makeString(unparse());
}
else
{
if (this->m->reserved)
{
throw std::logic_error(
"QPDFObjectHandle: attempting to unparse a reserved object");
}
dereference();
return this->m->obj->getJSON();
}
}
QPDFObjectHandle
QPDFObjectHandle::wrapInArray()
{

View File

@ -35,6 +35,18 @@ QPDF_Array::unparse()
return result;
}
JSON
QPDF_Array::getJSON()
{
JSON j = JSON::makeArray();
for (std::vector<QPDFObjectHandle>::iterator iter = this->items.begin();
iter != this->items.end(); ++iter)
{
j.addArrayElement((*iter).getJSON());
}
return j;
}
QPDFObject::object_type_e
QPDF_Array::getTypeCode() const
{

View File

@ -15,6 +15,12 @@ QPDF_Bool::unparse()
return (val ? "true" : "false");
}
JSON
QPDF_Bool::getJSON()
{
return JSON::makeBool(this->val);
}
QPDFObject::object_type_e
QPDF_Bool::getTypeCode() const
{

View File

@ -39,6 +39,20 @@ QPDF_Dictionary::unparse()
return result;
}
JSON
QPDF_Dictionary::getJSON()
{
JSON j = JSON::makeDictionary();
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
this->items.begin();
iter != this->items.end(); ++iter)
{
j.addDictionaryMember(QPDF_Name::normalizeName((*iter).first),
(*iter).second.getJSON());
}
return j;
}
QPDFObject::object_type_e
QPDF_Dictionary::getTypeCode() const
{

View File

@ -17,6 +17,12 @@ QPDF_InlineImage::unparse()
return this->val;
}
JSON
QPDF_InlineImage::getJSON()
{
return JSON::makeNull();
}
QPDFObject::object_type_e
QPDF_InlineImage::getTypeCode() const
{

View File

@ -17,6 +17,12 @@ QPDF_Integer::unparse()
return QUtil::int_to_string(this->val);
}
JSON
QPDF_Integer::getJSON()
{
return JSON::makeInt(this->val);
}
QPDFObject::object_type_e
QPDF_Integer::getTypeCode() const
{

View File

@ -44,6 +44,12 @@ QPDF_Name::unparse()
return normalizeName(this->name);
}
JSON
QPDF_Name::getJSON()
{
return JSON::makeString(normalizeName(this->name));
}
QPDFObject::object_type_e
QPDF_Name::getTypeCode() const
{

View File

@ -10,6 +10,12 @@ QPDF_Null::unparse()
return "null";
}
JSON
QPDF_Null::getJSON()
{
return JSON::makeNull();
}
QPDFObject::object_type_e
QPDF_Null::getTypeCode() const
{

View File

@ -17,6 +17,12 @@ QPDF_Operator::unparse()
return this->val;
}
JSON
QPDF_Operator::getJSON()
{
return JSON::makeNull();
}
QPDFObject::object_type_e
QPDF_Operator::getTypeCode() const
{

View File

@ -22,6 +22,12 @@ QPDF_Real::unparse()
return this->val;
}
JSON
QPDF_Real::getJSON()
{
return JSON::makeNumber(this->val);
}
QPDFObject::object_type_e
QPDF_Real::getTypeCode() const
{

View File

@ -12,6 +12,13 @@ QPDF_Reserved::unparse()
return "";
}
JSON
QPDF_Reserved::getJSON()
{
throw std::logic_error("attempt to generate JSON from QPDF_Reserved");
return JSON::makeNull();
}
QPDFObject::object_type_e
QPDF_Reserved::getTypeCode() const
{

View File

@ -74,6 +74,12 @@ QPDF_Stream::unparse()
QUtil::int_to_string(this->generation) + " R";
}
JSON
QPDF_Stream::getJSON()
{
return this->stream_dict.getJSON();
}
QPDFObject::object_type_e
QPDF_Stream::getTypeCode() const
{

View File

@ -122,6 +122,12 @@ QPDF_String::unparse()
return unparse(false);
}
JSON
QPDF_String::getJSON()
{
return JSON::makeString(getUTF8Val());
}
QPDFObject::object_type_e
QPDF_String::getTypeCode() const
{

View File

@ -12,6 +12,7 @@ class QPDF_Array: public QPDFObject
QPDF_Array(std::vector<QPDFObjectHandle> const& items);
virtual ~QPDF_Array();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&);

View File

@ -9,6 +9,7 @@ class QPDF_Bool: public QPDFObject
QPDF_Bool(bool val);
virtual ~QPDF_Bool();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
bool getVal() const;

View File

@ -14,6 +14,7 @@ class QPDF_Dictionary: public QPDFObject
QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items);
virtual ~QPDF_Dictionary();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&);

View File

@ -9,6 +9,7 @@ class QPDF_InlineImage: public QPDFObject
QPDF_InlineImage(std::string const& val);
virtual ~QPDF_InlineImage();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
std::string getVal() const;

View File

@ -9,6 +9,7 @@ class QPDF_Integer: public QPDFObject
QPDF_Integer(long long val);
virtual ~QPDF_Integer();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
long long getVal() const;

View File

@ -9,6 +9,7 @@ class QPDF_Name: public QPDFObject
QPDF_Name(std::string const& name);
virtual ~QPDF_Name();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
std::string getName() const;

View File

@ -8,6 +8,7 @@ class QPDF_Null: public QPDFObject
public:
virtual ~QPDF_Null();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
};

View File

@ -9,6 +9,7 @@ class QPDF_Operator: public QPDFObject
QPDF_Operator(std::string const& val);
virtual ~QPDF_Operator();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
std::string getVal() const;

View File

@ -10,6 +10,7 @@ class QPDF_Real: public QPDFObject
QPDF_Real(double value, int decimal_places = 0);
virtual ~QPDF_Real();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
std::string getVal();

View File

@ -8,6 +8,7 @@ class QPDF_Reserved: public QPDFObject
public:
virtual ~QPDF_Reserved();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
};

View File

@ -17,6 +17,7 @@ class QPDF_Stream: public QPDFObject
qpdf_offset_t offset, size_t length);
virtual ~QPDF_Stream();
virtual std::string unparse();
virtual JSON getJSON();
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&);

View File

@ -15,6 +15,7 @@ class QPDF_String: public QPDFObject
virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const;
std::string unparse(bool force_binary);
virtual JSON getJSON();
std::string getVal() const;
std::string getUTF8Val() const;