2020-06-15 20:32:25 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <tools/toolsdllapi.h>
|
|
|
|
#include <rtl/ustring.hxx>
|
2020-06-18 14:17:47 +03:00
|
|
|
#include <algorithm>
|
2020-06-15 20:32:25 +02:00
|
|
|
|
|
|
|
/** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
|
|
|
|
*
|
|
|
|
* (1) Minimal allocations/re-allocations/copying
|
|
|
|
* (2) Small/simple JSON documents
|
|
|
|
* (3) ascii property names
|
|
|
|
*/
|
|
|
|
namespace tools
|
|
|
|
{
|
|
|
|
class ScopedJsonWriterNode;
|
|
|
|
|
|
|
|
class TOOLS_DLLPUBLIC JsonWriter
|
|
|
|
{
|
|
|
|
friend class ScopedJsonWriterNode;
|
|
|
|
|
|
|
|
int mSpaceAllocated;
|
2020-06-19 09:57:58 +02:00
|
|
|
char* mpBuffer;
|
2020-06-15 20:32:25 +02:00
|
|
|
int mStartNodeCount;
|
|
|
|
char* mPos;
|
|
|
|
bool mbFirstFieldInNode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
JsonWriter();
|
|
|
|
~JsonWriter();
|
|
|
|
|
|
|
|
[[nodiscard]] ScopedJsonWriterNode startNode(const char*);
|
|
|
|
|
|
|
|
void put(const char* pPropName, const OUString& rPropValue);
|
|
|
|
void put(const char* pPropName, const OString& rPropValue);
|
|
|
|
void put(const char* pPropName, const char* pPropVal);
|
|
|
|
void put(const char*, int);
|
|
|
|
|
2020-06-18 22:48:42 +02:00
|
|
|
/** Hands ownership of the underlying storage buffer to the caller,
|
2020-06-15 20:32:25 +02:00
|
|
|
* after this no more document modifications may be written. */
|
|
|
|
char* extractData();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void endNode();
|
|
|
|
void addCommaBeforeField();
|
|
|
|
|
|
|
|
inline void ensureSpace(int noMoreBytesRequired)
|
|
|
|
{
|
2020-06-19 09:57:58 +02:00
|
|
|
int currentUsed = mPos - mpBuffer;
|
2020-06-15 20:32:25 +02:00
|
|
|
if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
|
|
|
|
{
|
|
|
|
auto newSize = std::max(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2);
|
2020-06-19 09:57:58 +02:00
|
|
|
char* pNew = static_cast<char*>(malloc(newSize));
|
|
|
|
memcpy(pNew, mpBuffer, currentUsed);
|
|
|
|
free(mpBuffer);
|
|
|
|
mpBuffer = pNew;
|
|
|
|
mPos = mpBuffer;
|
2020-06-15 20:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-closes the node.
|
|
|
|
*/
|
|
|
|
class ScopedJsonWriterNode
|
|
|
|
{
|
|
|
|
friend class JsonWriter;
|
|
|
|
|
|
|
|
JsonWriter& mrWriter;
|
|
|
|
|
|
|
|
ScopedJsonWriterNode(JsonWriter& rWriter)
|
|
|
|
: mrWriter(rWriter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
~ScopedJsonWriterNode() { mrWriter.endNode(); }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|