101 lines
2.6 KiB
C
101 lines
2.6 KiB
C
|
|
// Copyright (c) 2013-2015 IONU Security, Inc. All rights reserved.
|
||
|
|
// Copyright (c) 2016 Sequence Logic, Inc. All rights reserved.
|
||
|
|
//
|
||
|
|
// JSON utility class
|
||
|
|
|
||
|
|
#ifndef eyejson_h
|
||
|
|
#define eyejson_h
|
||
|
|
|
||
|
|
#include <string>
|
||
|
|
#include <iostream>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#ifndef LIBEYE_DLL
|
||
|
|
#ifdef WIN32
|
||
|
|
# ifdef DLLEXPORT
|
||
|
|
# define LIBEYE_DLL __declspec(dllexport)
|
||
|
|
# else
|
||
|
|
# define LIBEYE_DLL __declspec(dllimport)
|
||
|
|
# endif
|
||
|
|
#else
|
||
|
|
#define LIBEYE_DLL
|
||
|
|
#endif
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifdef ANDROID
|
||
|
|
#include "stdlib.h"
|
||
|
|
#endif
|
||
|
|
|
||
|
|
namespace sequencelogic {
|
||
|
|
|
||
|
|
// Enumeration of the JSON types and JSON utility classes
|
||
|
|
enum JSON_TYPE {
|
||
|
|
JSON_NULL,
|
||
|
|
JSON_ARRAY,
|
||
|
|
JSON_BOOL,
|
||
|
|
JSON_FLOAT,
|
||
|
|
JSON_INT,
|
||
|
|
JSON_OBJECT,
|
||
|
|
JSON_STRING
|
||
|
|
};
|
||
|
|
|
||
|
|
class EyeJSONNode
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
virtual ~EyeJSONNode ();
|
||
|
|
virtual void GetJSONString (std::string& json) = 0;
|
||
|
|
virtual char* GetKey () = 0;
|
||
|
|
virtual void ReplaceKey (const char* key) = 0;
|
||
|
|
JSON_TYPE GetType () {return _type;}
|
||
|
|
protected:
|
||
|
|
char* ParseString (const char* str);
|
||
|
|
int ProcessEscape (const char* jsonstr, char* value, int* escbytes);
|
||
|
|
JSON_TYPE _type;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Scalar type (BOOL, FLOAT, INT, STRING, NULL)
|
||
|
|
class EyeJSONScalar : public EyeJSONNode
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
LIBEYE_DLL EyeJSONScalar (const char* key, const char* value, JSON_TYPE type);
|
||
|
|
LIBEYE_DLL ~EyeJSONScalar ();
|
||
|
|
LIBEYE_DLL void GetJSONString (std::string& json);
|
||
|
|
LIBEYE_DLL char* GetKey () {return _key;}
|
||
|
|
LIBEYE_DLL char* GetValue () {return _value;}
|
||
|
|
LIBEYE_DLL int GetIntValue () {return atoi(_value);}
|
||
|
|
LIBEYE_DLL void ReplaceKey (const char* key);
|
||
|
|
LIBEYE_DLL void ReplaceValue (const char* value);
|
||
|
|
private:
|
||
|
|
char* _key;
|
||
|
|
char* _value;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Container type (ARRAY, OBJECT)
|
||
|
|
class EyeJSONObject : public EyeJSONNode
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
LIBEYE_DLL EyeJSONObject (const char* jsonstr);
|
||
|
|
LIBEYE_DLL EyeJSONObject (const char* key, JSON_TYPE type, EyeJSONObject* parent);
|
||
|
|
LIBEYE_DLL ~EyeJSONObject ();
|
||
|
|
LIBEYE_DLL bool AddMember (EyeJSONNode* member);
|
||
|
|
LIBEYE_DLL bool RemoveMember (const char* member);
|
||
|
|
LIBEYE_DLL bool RemoveMember (size_t member);
|
||
|
|
LIBEYE_DLL bool UpdateMember (EyeJSONNode* member);
|
||
|
|
LIBEYE_DLL EyeJSONNode* GetMember (const char* member);
|
||
|
|
LIBEYE_DLL EyeJSONNode* GetMember (size_t member);
|
||
|
|
size_t GetNumMembers () {return _members.size();}
|
||
|
|
LIBEYE_DLL void Empty ();
|
||
|
|
LIBEYE_DLL void GetJSONString (std::string& json);
|
||
|
|
LIBEYE_DLL std::string Stringify ();
|
||
|
|
EyeJSONObject* GetParent () {return _parent;}
|
||
|
|
char* GetKey () {return _key;}
|
||
|
|
LIBEYE_DLL void ReplaceKey (const char* key);
|
||
|
|
private:
|
||
|
|
char* _key;
|
||
|
|
EyeJSONObject* _parent;
|
||
|
|
std::vector<EyeJSONNode*> _members;
|
||
|
|
};
|
||
|
|
|
||
|
|
} //namespace sequencelogic
|
||
|
|
#endif
|