Commit 30a0c070e41212172f5516936594ec0f6e72ac70

Authored by Jay Berkenbilt
1 parent 651179b5

Add QPDFObjectHandle::getJSON()

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
... ... @@ -15,6 +15,12 @@ QPDF_Bool::unparse()
15 15 return (val ? "true" : "false");
16 16 }
17 17  
  18 +JSON
  19 +QPDF_Bool::getJSON()
  20 +{
  21 + return JSON::makeBool(this->val);
  22 +}
  23 +
18 24 QPDFObject::object_type_e
19 25 QPDF_Bool::getTypeCode() const
20 26 {
... ...
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
... ... @@ -17,6 +17,12 @@ QPDF_InlineImage::unparse()
17 17 return this->val;
18 18 }
19 19  
  20 +JSON
  21 +QPDF_InlineImage::getJSON()
  22 +{
  23 + return JSON::makeNull();
  24 +}
  25 +
20 26 QPDFObject::object_type_e
21 27 QPDF_InlineImage::getTypeCode() const
22 28 {
... ...
libqpdf/QPDF_Integer.cc
... ... @@ -17,6 +17,12 @@ QPDF_Integer::unparse()
17 17 return QUtil::int_to_string(this->val);
18 18 }
19 19  
  20 +JSON
  21 +QPDF_Integer::getJSON()
  22 +{
  23 + return JSON::makeInt(this->val);
  24 +}
  25 +
20 26 QPDFObject::object_type_e
21 27 QPDF_Integer::getTypeCode() const
22 28 {
... ...
libqpdf/QPDF_Name.cc
... ... @@ -44,6 +44,12 @@ QPDF_Name::unparse()
44 44 return normalizeName(this->name);
45 45 }
46 46  
  47 +JSON
  48 +QPDF_Name::getJSON()
  49 +{
  50 + return JSON::makeString(normalizeName(this->name));
  51 +}
  52 +
47 53 QPDFObject::object_type_e
48 54 QPDF_Name::getTypeCode() const
49 55 {
... ...
libqpdf/QPDF_Null.cc
... ... @@ -10,6 +10,12 @@ QPDF_Null::unparse()
10 10 return "null";
11 11 }
12 12  
  13 +JSON
  14 +QPDF_Null::getJSON()
  15 +{
  16 + return JSON::makeNull();
  17 +}
  18 +
13 19 QPDFObject::object_type_e
14 20 QPDF_Null::getTypeCode() const
15 21 {
... ...
libqpdf/QPDF_Operator.cc
... ... @@ -17,6 +17,12 @@ QPDF_Operator::unparse()
17 17 return this->val;
18 18 }
19 19  
  20 +JSON
  21 +QPDF_Operator::getJSON()
  22 +{
  23 + return JSON::makeNull();
  24 +}
  25 +
20 26 QPDFObject::object_type_e
21 27 QPDF_Operator::getTypeCode() const
22 28 {
... ...
libqpdf/QPDF_Real.cc
... ... @@ -22,6 +22,12 @@ QPDF_Real::unparse()
22 22 return this->val;
23 23 }
24 24  
  25 +JSON
  26 +QPDF_Real::getJSON()
  27 +{
  28 + return JSON::makeNumber(this->val);
  29 +}
  30 +
25 31 QPDFObject::object_type_e
26 32 QPDF_Real::getTypeCode() const
27 33 {
... ...
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
... ... @@ -74,6 +74,12 @@ QPDF_Stream::unparse()
74 74 QUtil::int_to_string(this->generation) + " R";
75 75 }
76 76  
  77 +JSON
  78 +QPDF_Stream::getJSON()
  79 +{
  80 + return this->stream_dict.getJSON();
  81 +}
  82 +
77 83 QPDFObject::object_type_e
78 84 QPDF_Stream::getTypeCode() const
79 85 {
... ...
libqpdf/QPDF_String.cc
... ... @@ -122,6 +122,12 @@ QPDF_String::unparse()
122 122 return unparse(false);
123 123 }
124 124  
  125 +JSON
  126 +QPDF_String::getJSON()
  127 +{
  128 + return JSON::makeString(getUTF8Val());
  129 +}
  130 +
125 131 QPDFObject::object_type_e
126 132 QPDF_String::getTypeCode() const
127 133 {
... ...
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
... ... @@ -8,6 +8,7 @@ class QPDF_Null: public QPDFObject
8 8 public:
9 9 virtual ~QPDF_Null();
10 10 virtual std::string unparse();
  11 + virtual JSON getJSON();
11 12 virtual QPDFObject::object_type_e getTypeCode() const;
12 13 virtual char const* getTypeName() const;
13 14 };
... ...
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
... ... @@ -8,6 +8,7 @@ class QPDF_Reserved: public QPDFObject
8 8 public:
9 9 virtual ~QPDF_Reserved();
10 10 virtual std::string unparse();
  11 + virtual JSON getJSON();
11 12 virtual QPDFObject::object_type_e getTypeCode() const;
12 13 virtual char const* getTypeName() const;
13 14 };
... ...
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  
... ...