mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-04-07 13:55:33 +08:00
Added arrays
This commit is contained in:
@ -6,6 +6,10 @@ namespace NSJSON
|
||||
{
|
||||
}
|
||||
|
||||
IBaseValue::IBaseValue(ValueType type) : m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
IBaseValue::~IBaseValue()
|
||||
{
|
||||
}
|
||||
@ -29,6 +33,31 @@ namespace NSJSON
|
||||
{
|
||||
}
|
||||
|
||||
CValue::CValue(const bool& value) : IBaseValue(vtBoolean)
|
||||
{
|
||||
m_bool = value;
|
||||
}
|
||||
|
||||
CValue::CValue(const int& value) : IBaseValue(vtInteger)
|
||||
{
|
||||
m_int = value;
|
||||
}
|
||||
|
||||
CValue::CValue(const double& value) : IBaseValue(vtDouble)
|
||||
{
|
||||
m_double = value;
|
||||
}
|
||||
|
||||
CValue::CValue(const std::string& str) : IBaseValue(vtStringA)
|
||||
{
|
||||
new (&m_string) std::string(str);
|
||||
}
|
||||
|
||||
CValue::CValue(const std::wstring& wstr) : IBaseValue(vtStringW)
|
||||
{
|
||||
new (&m_wstring) std::wstring(wstr);
|
||||
}
|
||||
|
||||
CValue::CValue(const CValue& other)
|
||||
{
|
||||
*this = other;
|
||||
@ -127,12 +156,49 @@ namespace NSJSON
|
||||
}
|
||||
}
|
||||
|
||||
CObject::CObject()
|
||||
CArray::CArray() : IBaseValue(vtArray)
|
||||
{
|
||||
}
|
||||
|
||||
CObject::~CObject()
|
||||
CArray::~CArray()
|
||||
{
|
||||
for (IBaseValue* pValue : m_values)
|
||||
{
|
||||
delete pValue;
|
||||
}
|
||||
}
|
||||
|
||||
void CArray::add(IBaseValue* value)
|
||||
{
|
||||
m_values.push_back(value);
|
||||
}
|
||||
|
||||
void CArray::addNull()
|
||||
{
|
||||
CValue* pValue = new CValue();
|
||||
pValue->setNull();
|
||||
m_values.push_back(pValue);
|
||||
}
|
||||
|
||||
void CArray::addUndefined()
|
||||
{
|
||||
CValue* pValue = new CValue();
|
||||
m_values.push_back(pValue);
|
||||
}
|
||||
|
||||
int CArray::getCount() const
|
||||
{
|
||||
return static_cast<int>(m_values.size());
|
||||
}
|
||||
|
||||
IBaseValue* CArray::operator[](int index)
|
||||
{
|
||||
return m_values[index];
|
||||
}
|
||||
|
||||
const IBaseValue* CArray::operator[](int index) const
|
||||
{
|
||||
return m_values[index];
|
||||
}
|
||||
|
||||
void CObject::addMember(const IBaseValue* pValue, const std::string& name)
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace NSJSON
|
||||
{
|
||||
@ -24,11 +25,13 @@ namespace NSJSON
|
||||
vtDouble,
|
||||
vtStringA,
|
||||
vtStringW,
|
||||
vtArray,
|
||||
vtObject
|
||||
};
|
||||
|
||||
public:
|
||||
IBaseValue();
|
||||
IBaseValue(ValueType type);
|
||||
virtual ~IBaseValue();
|
||||
|
||||
public:
|
||||
@ -42,6 +45,7 @@ namespace NSJSON
|
||||
ValueType m_type;
|
||||
};
|
||||
|
||||
// for primitive values
|
||||
class JS_DECL CValue : public IBaseValue
|
||||
{
|
||||
public:
|
||||
@ -49,7 +53,11 @@ namespace NSJSON
|
||||
CValue(const CValue& other);
|
||||
CValue(CValue&& other) = delete;
|
||||
// TODO: move constructor and assignment operator ???
|
||||
// TODO: constructors from value ???
|
||||
CValue(const bool& value);
|
||||
CValue(const int& value);
|
||||
CValue(const double& value);
|
||||
CValue(const std::string& str);
|
||||
CValue(const std::wstring& wstr);
|
||||
virtual ~CValue();
|
||||
|
||||
CValue& operator=(const CValue& other);
|
||||
@ -81,12 +89,34 @@ namespace NSJSON
|
||||
};
|
||||
};
|
||||
|
||||
class JS_DECL CArray : public IBaseValue
|
||||
{
|
||||
public:
|
||||
CArray();
|
||||
// elements get deleted on array destruction
|
||||
virtual ~CArray();
|
||||
|
||||
public:
|
||||
void add(IBaseValue* value);
|
||||
void addNull();
|
||||
void addUndefined();
|
||||
int getCount() const;
|
||||
|
||||
IBaseValue* operator[](int index);
|
||||
const IBaseValue* operator[](int index) const;
|
||||
|
||||
friend JSSmart<NSJSBase::CJSValue> toJS(const IBaseValue* pValue);
|
||||
|
||||
private:
|
||||
std::vector<IBaseValue*> m_values;
|
||||
};
|
||||
|
||||
// extend this class to make custom objects serializable to JS
|
||||
class JS_DECL CObject : public IBaseValue
|
||||
{
|
||||
public:
|
||||
CObject();
|
||||
virtual ~CObject();
|
||||
CObject() = default;
|
||||
virtual ~CObject() = default;
|
||||
|
||||
public:
|
||||
// Add member to JS object when it will be serialized
|
||||
|
||||
@ -25,6 +25,16 @@ namespace NSJSON
|
||||
}
|
||||
ret = jsObj->toValue();
|
||||
}
|
||||
else if (type == IBaseValue::vtArray)
|
||||
{
|
||||
const CArray* pArray = static_cast<const CArray*>(pValue);
|
||||
JSSmart<NSJSBase::CJSArray> jsArr = NSJSBase::CJSContext::createArray(0);
|
||||
for (const IBaseValue* pArrValue : pArray->m_values)
|
||||
{
|
||||
jsArr->add(toJS(pArrValue).GetPointer());
|
||||
}
|
||||
ret = jsArr->toValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
// primitive type
|
||||
|
||||
Reference in New Issue
Block a user