Commit 30a0c070e41212172f5516936594ec0f6e72ac70
1 parent
651179b5
Add QPDFObjectHandle::getJSON()
Showing
28 changed files
with
143 additions
and
0 deletions
ChangeLog
| 1 | 1 | 2018-12-18 Jay Berkenbilt <ejb@ql.org> |
| 2 | 2 | |
| 3 | + * New method QPDFObjectHandle::getJSON() returns a JSON object | |
| 4 | + with a partial representation of the object. See | |
| 5 | + QPDFObjectHandle.hh for a detailed description. | |
| 6 | + | |
| 3 | 7 | * Add a simple JSON serializer. This is not a complete or |
| 4 | 8 | general-purpose JSON library. It allows assembly and serialization |
| 5 | 9 | of JSON structures with some restrictions, which are described in | ... | ... |
include/qpdf/QPDFObject.hh
| ... | ... | @@ -24,6 +24,7 @@ |
| 24 | 24 | |
| 25 | 25 | #include <qpdf/DLL.h> |
| 26 | 26 | #include <qpdf/PointerHolder.hh> |
| 27 | +#include <qpdf/JSON.hh> | |
| 27 | 28 | |
| 28 | 29 | #include <string> |
| 29 | 30 | |
| ... | ... | @@ -62,6 +63,7 @@ class QPDFObject |
| 62 | 63 | |
| 63 | 64 | virtual ~QPDFObject() {} |
| 64 | 65 | virtual std::string unparse() = 0; |
| 66 | + virtual JSON getJSON() = 0; | |
| 65 | 67 | |
| 66 | 68 | // Return a unique type code for the object |
| 67 | 69 | virtual object_type_e getTypeCode() const = 0; | ... | ... |
include/qpdf/QPDFObjectHandle.hh
| ... | ... | @@ -733,6 +733,25 @@ class QPDFObjectHandle |
| 733 | 733 | QPDF_DLL |
| 734 | 734 | std::string unparseBinary(); |
| 735 | 735 | |
| 736 | + // Return encoded as JSON. For most object types, there is an | |
| 737 | + // obvious mapping. The JSON is generated as follows: | |
| 738 | + // * Names are encoded as strings representing the normalized value of | |
| 739 | + // getName() | |
| 740 | + // * Indirect references are encoded as strings containing "obj gen R" | |
| 741 | + // * Strings are encoded as UTF-8 strings with unrepresentable binary | |
| 742 | + // characters encoded as \uHHHH | |
| 743 | + // * Encoding streams just encodes the stream's dictionary; the stream | |
| 744 | + // data is not represented | |
| 745 | + // * Object types that are only valid in content streams (inline | |
| 746 | + // image, operator) as well as "reserved" objects are not | |
| 747 | + // representable and will be serialized as "null". | |
| 748 | + // If dereference_indirect is true and this is an indirect object, | |
| 749 | + // show the actual contents of the object. The effect of | |
| 750 | + // dereference_indirect applies only to this object. It is not | |
| 751 | + // recursive. | |
| 752 | + QPDF_DLL | |
| 753 | + JSON getJSON(bool dereference_indirect = false); | |
| 754 | + | |
| 736 | 755 | // Legacy helper methods for commonly performed operations on |
| 737 | 756 | // pages. Newer code should use QPDFPageObjectHelper instead. The |
| 738 | 757 | // specification and behavior of these methods are the same as the | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -1235,6 +1235,25 @@ QPDFObjectHandle::unparseBinary() |
| 1235 | 1235 | } |
| 1236 | 1236 | } |
| 1237 | 1237 | |
| 1238 | +JSON | |
| 1239 | +QPDFObjectHandle::getJSON(bool dereference_indirect) | |
| 1240 | +{ | |
| 1241 | + if ((! dereference_indirect) && this->isIndirect()) | |
| 1242 | + { | |
| 1243 | + return JSON::makeString(unparse()); | |
| 1244 | + } | |
| 1245 | + else | |
| 1246 | + { | |
| 1247 | + if (this->m->reserved) | |
| 1248 | + { | |
| 1249 | + throw std::logic_error( | |
| 1250 | + "QPDFObjectHandle: attempting to unparse a reserved object"); | |
| 1251 | + } | |
| 1252 | + dereference(); | |
| 1253 | + return this->m->obj->getJSON(); | |
| 1254 | + } | |
| 1255 | +} | |
| 1256 | + | |
| 1238 | 1257 | QPDFObjectHandle |
| 1239 | 1258 | QPDFObjectHandle::wrapInArray() |
| 1240 | 1259 | { | ... | ... |
libqpdf/QPDF_Array.cc
| ... | ... | @@ -35,6 +35,18 @@ QPDF_Array::unparse() |
| 35 | 35 | return result; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | +JSON | |
| 39 | +QPDF_Array::getJSON() | |
| 40 | +{ | |
| 41 | + JSON j = JSON::makeArray(); | |
| 42 | + for (std::vector<QPDFObjectHandle>::iterator iter = this->items.begin(); | |
| 43 | + iter != this->items.end(); ++iter) | |
| 44 | + { | |
| 45 | + j.addArrayElement((*iter).getJSON()); | |
| 46 | + } | |
| 47 | + return j; | |
| 48 | +} | |
| 49 | + | |
| 38 | 50 | QPDFObject::object_type_e |
| 39 | 51 | QPDF_Array::getTypeCode() const |
| 40 | 52 | { | ... | ... |
libqpdf/QPDF_Bool.cc
libqpdf/QPDF_Dictionary.cc
| ... | ... | @@ -39,6 +39,20 @@ QPDF_Dictionary::unparse() |
| 39 | 39 | return result; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | +JSON | |
| 43 | +QPDF_Dictionary::getJSON() | |
| 44 | +{ | |
| 45 | + JSON j = JSON::makeDictionary(); | |
| 46 | + for (std::map<std::string, QPDFObjectHandle>::iterator iter = | |
| 47 | + this->items.begin(); | |
| 48 | + iter != this->items.end(); ++iter) | |
| 49 | + { | |
| 50 | + j.addDictionaryMember(QPDF_Name::normalizeName((*iter).first), | |
| 51 | + (*iter).second.getJSON()); | |
| 52 | + } | |
| 53 | + return j; | |
| 54 | +} | |
| 55 | + | |
| 42 | 56 | QPDFObject::object_type_e |
| 43 | 57 | QPDF_Dictionary::getTypeCode() const |
| 44 | 58 | { | ... | ... |
libqpdf/QPDF_InlineImage.cc
libqpdf/QPDF_Integer.cc
libqpdf/QPDF_Name.cc
libqpdf/QPDF_Null.cc
libqpdf/QPDF_Operator.cc
libqpdf/QPDF_Real.cc
libqpdf/QPDF_Reserved.cc
| ... | ... | @@ -12,6 +12,13 @@ QPDF_Reserved::unparse() |
| 12 | 12 | return ""; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | +JSON | |
| 16 | +QPDF_Reserved::getJSON() | |
| 17 | +{ | |
| 18 | + throw std::logic_error("attempt to generate JSON from QPDF_Reserved"); | |
| 19 | + return JSON::makeNull(); | |
| 20 | +} | |
| 21 | + | |
| 15 | 22 | QPDFObject::object_type_e |
| 16 | 23 | QPDF_Reserved::getTypeCode() const |
| 17 | 24 | { | ... | ... |
libqpdf/QPDF_Stream.cc
libqpdf/QPDF_String.cc
libqpdf/qpdf/QPDF_Array.hh
| ... | ... | @@ -12,6 +12,7 @@ class QPDF_Array: public QPDFObject |
| 12 | 12 | QPDF_Array(std::vector<QPDFObjectHandle> const& items); |
| 13 | 13 | virtual ~QPDF_Array(); |
| 14 | 14 | virtual std::string unparse(); |
| 15 | + virtual JSON getJSON(); | |
| 15 | 16 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 16 | 17 | virtual char const* getTypeName() const; |
| 17 | 18 | virtual void setDescription(QPDF*, std::string const&); | ... | ... |
libqpdf/qpdf/QPDF_Bool.hh
| ... | ... | @@ -9,6 +9,7 @@ class QPDF_Bool: public QPDFObject |
| 9 | 9 | QPDF_Bool(bool val); |
| 10 | 10 | virtual ~QPDF_Bool(); |
| 11 | 11 | virtual std::string unparse(); |
| 12 | + virtual JSON getJSON(); | |
| 12 | 13 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 13 | 14 | virtual char const* getTypeName() const; |
| 14 | 15 | bool getVal() const; | ... | ... |
libqpdf/qpdf/QPDF_Dictionary.hh
| ... | ... | @@ -14,6 +14,7 @@ class QPDF_Dictionary: public QPDFObject |
| 14 | 14 | QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items); |
| 15 | 15 | virtual ~QPDF_Dictionary(); |
| 16 | 16 | virtual std::string unparse(); |
| 17 | + virtual JSON getJSON(); | |
| 17 | 18 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 18 | 19 | virtual char const* getTypeName() const; |
| 19 | 20 | virtual void setDescription(QPDF*, std::string const&); | ... | ... |
libqpdf/qpdf/QPDF_InlineImage.hh
| ... | ... | @@ -9,6 +9,7 @@ class QPDF_InlineImage: public QPDFObject |
| 9 | 9 | QPDF_InlineImage(std::string const& val); |
| 10 | 10 | virtual ~QPDF_InlineImage(); |
| 11 | 11 | virtual std::string unparse(); |
| 12 | + virtual JSON getJSON(); | |
| 12 | 13 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 13 | 14 | virtual char const* getTypeName() const; |
| 14 | 15 | std::string getVal() const; | ... | ... |
libqpdf/qpdf/QPDF_Integer.hh
| ... | ... | @@ -9,6 +9,7 @@ class QPDF_Integer: public QPDFObject |
| 9 | 9 | QPDF_Integer(long long val); |
| 10 | 10 | virtual ~QPDF_Integer(); |
| 11 | 11 | virtual std::string unparse(); |
| 12 | + virtual JSON getJSON(); | |
| 12 | 13 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 13 | 14 | virtual char const* getTypeName() const; |
| 14 | 15 | long long getVal() const; | ... | ... |
libqpdf/qpdf/QPDF_Name.hh
| ... | ... | @@ -9,6 +9,7 @@ class QPDF_Name: public QPDFObject |
| 9 | 9 | QPDF_Name(std::string const& name); |
| 10 | 10 | virtual ~QPDF_Name(); |
| 11 | 11 | virtual std::string unparse(); |
| 12 | + virtual JSON getJSON(); | |
| 12 | 13 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 13 | 14 | virtual char const* getTypeName() const; |
| 14 | 15 | std::string getName() const; | ... | ... |
libqpdf/qpdf/QPDF_Null.hh
libqpdf/qpdf/QPDF_Operator.hh
| ... | ... | @@ -9,6 +9,7 @@ class QPDF_Operator: public QPDFObject |
| 9 | 9 | QPDF_Operator(std::string const& val); |
| 10 | 10 | virtual ~QPDF_Operator(); |
| 11 | 11 | virtual std::string unparse(); |
| 12 | + virtual JSON getJSON(); | |
| 12 | 13 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 13 | 14 | virtual char const* getTypeName() const; |
| 14 | 15 | std::string getVal() const; | ... | ... |
libqpdf/qpdf/QPDF_Real.hh
| ... | ... | @@ -10,6 +10,7 @@ class QPDF_Real: public QPDFObject |
| 10 | 10 | QPDF_Real(double value, int decimal_places = 0); |
| 11 | 11 | virtual ~QPDF_Real(); |
| 12 | 12 | virtual std::string unparse(); |
| 13 | + virtual JSON getJSON(); | |
| 13 | 14 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 14 | 15 | virtual char const* getTypeName() const; |
| 15 | 16 | std::string getVal(); | ... | ... |
libqpdf/qpdf/QPDF_Reserved.hh
libqpdf/qpdf/QPDF_Stream.hh
| ... | ... | @@ -17,6 +17,7 @@ class QPDF_Stream: public QPDFObject |
| 17 | 17 | qpdf_offset_t offset, size_t length); |
| 18 | 18 | virtual ~QPDF_Stream(); |
| 19 | 19 | virtual std::string unparse(); |
| 20 | + virtual JSON getJSON(); | |
| 20 | 21 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 21 | 22 | virtual char const* getTypeName() const; |
| 22 | 23 | virtual void setDescription(QPDF*, std::string const&); | ... | ... |
libqpdf/qpdf/QPDF_String.hh
| ... | ... | @@ -15,6 +15,7 @@ class QPDF_String: public QPDFObject |
| 15 | 15 | virtual QPDFObject::object_type_e getTypeCode() const; |
| 16 | 16 | virtual char const* getTypeName() const; |
| 17 | 17 | std::string unparse(bool force_binary); |
| 18 | + virtual JSON getJSON(); | |
| 18 | 19 | std::string getVal() const; |
| 19 | 20 | std::string getUTF8Val() const; |
| 20 | 21 | ... | ... |