Skip to content

Commit

Permalink
cpl_json: add CPLJSONArray::AddNull() and CPLJSONObject constructor w…
Browse files Browse the repository at this point in the history
…ith primitive types
  • Loading branch information
rouault committed Aug 23, 2023
1 parent af5b75e commit 94a731a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/ogrinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ static void ReportOnLayer(CPLString &osRet, CPLJSONObject oLayer,
}
}
else
oGeometries.AddNull("geometry");
oGeometries.AddNull();
}
}
}
Expand Down
46 changes: 44 additions & 2 deletions autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,47 @@ TEST_F(test_cpl, CPLJSONDocument)
ASSERT_TRUE(!oDocument.Save("/i_do/not/exist"));
CPLPopErrorHandler();
}
{
CPLJSONObject oObj(nullptr);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Null);
}
{
CPLJSONObject oObj(true);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Boolean);
EXPECT_EQ(oObj.ToBool(), true);
}
{
CPLJSONObject oObj(1);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Integer);
EXPECT_EQ(oObj.ToInteger(), 1);
}
{
CPLJSONObject oObj(static_cast<int64_t>(123) * 1024 * 1024 * 1024);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Long);
EXPECT_EQ(oObj.ToLong(),
static_cast<int64_t>(123) * 1024 * 1024 * 1024);
}
{
CPLJSONObject oObj(static_cast<uint64_t>(123) * 1024 * 1024 * 1024);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Long);
EXPECT_EQ(oObj.ToLong(),
static_cast<int64_t>(123) * 1024 * 1024 * 1024);
}
{
CPLJSONObject oObj(1.5);
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::Double);
EXPECT_EQ(oObj.ToDouble(), 1.5);
}
{
CPLJSONObject oObj("ab");
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::String);
EXPECT_STREQ(oObj.ToString().c_str(), "ab");
}
{
CPLJSONObject oObj(std::string("ab"));
EXPECT_EQ(oObj.GetType(), CPLJSONObject::Type::String);
EXPECT_STREQ(oObj.ToString().c_str(), "ab");
}
{
CPLJSONObject oObj;
oObj.Add("string", std::string("my_string"));
Expand Down Expand Up @@ -2659,7 +2700,8 @@ TEST_F(test_cpl, CPLJSONDocument)
oArray.Add(1);
oArray.Add(GINT64_MAX);
oArray.Add(true);
ASSERT_EQ(oArray.Size(), 7);
oArray.AddNull();
ASSERT_EQ(oArray.Size(), 8);

int nCount = 0;
for (const auto &obj : oArray)
Expand All @@ -2668,7 +2710,7 @@ TEST_F(test_cpl, CPLJSONDocument)
oArray[nCount].GetInternalHandle());
nCount++;
}
ASSERT_EQ(nCount, 7);
ASSERT_EQ(nCount, 8);
}
{
CPLJSONDocument oDocument;
Expand Down
50 changes: 50 additions & 0 deletions port/cpl_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,45 @@ CPLJSONObject::CPLJSONObject() : m_poJsonObject(json_object_new_object())
{
}

CPLJSONObject::CPLJSONObject(std::nullptr_t) : m_poJsonObject(nullptr)
{
}

CPLJSONObject::CPLJSONObject(const std::string &osVal)
: m_poJsonObject(json_object_new_string(osVal.c_str()))
{
}

CPLJSONObject::CPLJSONObject(const char *pszValue)
: m_poJsonObject(json_object_new_string(pszValue))
{
}

CPLJSONObject::CPLJSONObject(bool bVal)
: m_poJsonObject(json_object_new_boolean(bVal))
{
}

CPLJSONObject::CPLJSONObject(int nVal)
: m_poJsonObject(json_object_new_int(nVal))
{
}

CPLJSONObject::CPLJSONObject(int64_t nVal)
: m_poJsonObject(json_object_new_int64(nVal))
{
}

CPLJSONObject::CPLJSONObject(uint64_t nVal)
: m_poJsonObject(json_object_new_uint64(nVal))
{
}

CPLJSONObject::CPLJSONObject(double dfVal)
: m_poJsonObject(json_object_new_double(dfVal))
{
}

CPLJSONObject::CPLJSONObject(const std::string &osName,
const CPLJSONObject &oParent)
: m_poJsonObject(json_object_get(json_object_new_object())), m_osKey(osName)
Expand Down Expand Up @@ -1418,6 +1457,17 @@ int CPLJSONArray::Size() const
return 0;
}

/**
* Add null object to array.
*
* @since GDAL 3.8
*/
void CPLJSONArray::AddNull()
{
if (m_poJsonObject)
json_object_array_add(TO_JSONOBJ(m_poJsonObject), nullptr);
}

/**
* Add json object to array.
* @param oValue Json array.
Expand Down
9 changes: 9 additions & 0 deletions port/cpl_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class CPL_DLL CPLJSONObject
CPLJSONObject();
explicit CPLJSONObject(const std::string &osName,
const CPLJSONObject &oParent);
explicit CPLJSONObject(std::nullptr_t);
explicit CPLJSONObject(const std::string &osVal);
explicit CPLJSONObject(const char *pszValue);
explicit CPLJSONObject(bool bVal);
explicit CPLJSONObject(int nVal);
explicit CPLJSONObject(int64_t nVal);
explicit CPLJSONObject(uint64_t nVal);
explicit CPLJSONObject(double dfVal);
~CPLJSONObject();
CPLJSONObject(const CPLJSONObject &other);
CPLJSONObject(CPLJSONObject &&other);
Expand Down Expand Up @@ -227,6 +235,7 @@ class CPL_DLL CPLJSONArray : public CPLJSONObject
/*! @endcond */
public:
int Size() const;
void AddNull();
void Add(const CPLJSONObject &oValue);
void Add(const std::string &osValue);
void Add(const char *pszValue);
Expand Down

0 comments on commit 94a731a

Please sign in to comment.