Fix json serialization for {x | -1 < x < 1} (fixes #308)

JSON serialization was preserving the value as presented, but JSON
doesn't accept decimal values without a 0 before the decimal point.
This commit is contained in:
Jay Berkenbilt 2019-03-11 16:21:27 -04:00
parent d2260925f0
commit da7c2c0ee9
3 changed files with 34 additions and 2 deletions

View File

@ -1,3 +1,8 @@
2019-03-11 Jay Berkenbilt <ejb@ql.org>
* JSON serialization: add missing leading 0 to decimal values
between -1 and 1. Fixes #308.
2019-02-01 Jay Berkenbilt <ejb@ql.org>
* 8.4.0: release

View File

@ -25,7 +25,30 @@ QPDF_Real::unparse()
JSON
QPDF_Real::getJSON()
{
return JSON::makeNumber(this->val);
// While PDF allows .x or -.x, JSON does not. Rather than
// convering from string to double and back, just handle this as a
// special case for JSON.
std::string result;
if (this->val.length() == 0)
{
// Can't really happen...
result = "0";
}
else if (this->val.at(0) == '.')
{
result = "0" + this->val;
}
else if ((this->val.length() >= 2) &&
(this->val.at(0) == '-') &&
(this->val.at(1) == '.'))
{
result = "-0." + this->val.substr(2);
}
else
{
result = this->val;
}
return JSON::makeNumber(result);
}
QPDFObject::object_type_e

View File

@ -3,7 +3,7 @@
#include <iostream>
#include <assert.h>
static void check(JSON& j, std::string const& exp)
static void check(JSON const& j, std::string const& exp)
{
if (exp != j.unparse())
{
@ -69,6 +69,10 @@ static void test_main()
" ],\n"
" \"yes\": false\n"
"}");
check(QPDFObjectHandle::newReal("0.12").getJSON(), "0.12");
check(QPDFObjectHandle::newReal(".34").getJSON(), "0.34");
check(QPDFObjectHandle::newReal("-0.56").getJSON(), "-0.56");
check(QPDFObjectHandle::newReal("-.78").getJSON(), "-0.78");
}
static void check_schema(JSON& obj, JSON& schema, bool exp,