mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
add json base on nxjson(nx-json/nx_json) decoder for http api. 0.9.102
This commit is contained in:
parent
2c059d3a80
commit
b60e8418c6
4 changed files with 525 additions and 73 deletions
|
@ -29,6 +29,144 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// whether use nxjson
|
||||
// @see: https://bitbucket.org/yarosla/nxjson
|
||||
#undef SRS_JSON_USE_NXJSON
|
||||
#define SRS_JSON_USE_NXJSON
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// json decode
|
||||
// 1. SrsJsonAny: read any from str:char*
|
||||
// SrsJsonAny* pany = NULL;
|
||||
// if ((ret = srs_json_read_any(str, &pany)) != ERROR_SUCCESS) {
|
||||
// return ret;
|
||||
// }
|
||||
// srs_assert(pany); // if success, always valid object.
|
||||
// 2. SrsJsonAny: convert to specifid type, for instance, string
|
||||
// SrsJsonAny* pany = ...
|
||||
// if (pany->is_string()) {
|
||||
// string v = pany->to_str();
|
||||
// }
|
||||
//
|
||||
// for detail usage, see interfaces of each object.
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// @see: https://bitbucket.org/yarosla/nxjson
|
||||
// @see: https://github.com/udp/json-parser
|
||||
|
||||
class SrsJsonArray;
|
||||
class SrsJsonObject;
|
||||
|
||||
class SrsJsonAny
|
||||
{
|
||||
public:
|
||||
char marker;
|
||||
// donot directly create this object,
|
||||
// instead, for examle, use SrsJsonAny::str() to create a concreated one.
|
||||
protected:
|
||||
SrsJsonAny();
|
||||
public:
|
||||
virtual ~SrsJsonAny();
|
||||
public:
|
||||
virtual bool is_string();
|
||||
virtual bool is_boolean();
|
||||
virtual bool is_integer();
|
||||
virtual bool is_number();
|
||||
virtual bool is_object();
|
||||
virtual bool is_array();
|
||||
virtual bool is_null();
|
||||
public:
|
||||
/**
|
||||
* get the string of any when is_string() indicates true.
|
||||
* user must ensure the type is a string, or assert failed.
|
||||
*/
|
||||
virtual std::string to_str();
|
||||
/**
|
||||
* get the boolean of any when is_boolean() indicates true.
|
||||
* user must ensure the type is a boolean, or assert failed.
|
||||
*/
|
||||
virtual bool to_boolean();
|
||||
/**
|
||||
* get the integer of any when is_integer() indicates true.
|
||||
* user must ensure the type is a integer, or assert failed.
|
||||
*/
|
||||
virtual int64_t to_integer();
|
||||
/**
|
||||
* get the number of any when is_number() indicates true.
|
||||
* user must ensure the type is a number, or assert failed.
|
||||
*/
|
||||
virtual double to_number();
|
||||
/**
|
||||
* get the object of any when is_object() indicates true.
|
||||
* user must ensure the type is a object, or assert failed.
|
||||
*/
|
||||
virtual SrsJsonObject* to_object();
|
||||
/**
|
||||
* get the ecma array of any when is_ecma_array() indicates true.
|
||||
* user must ensure the type is a ecma array, or assert failed.
|
||||
*/
|
||||
virtual SrsJsonArray* to_array();
|
||||
public:
|
||||
static SrsJsonAny* str(const char* value = NULL);
|
||||
static SrsJsonAny* boolean(bool value = false);
|
||||
static SrsJsonAny* ingeter(int64_t value = 0);
|
||||
static SrsJsonAny* number(double value = 0.0);
|
||||
static SrsJsonAny* null();
|
||||
static SrsJsonObject* object();
|
||||
static SrsJsonArray* array();
|
||||
public:
|
||||
/**
|
||||
* read json tree from str:char*
|
||||
*/
|
||||
static SrsJsonAny* loads(char* str);
|
||||
};
|
||||
|
||||
class SrsJsonObject : public SrsJsonAny
|
||||
{
|
||||
private:
|
||||
typedef std::pair<std::string, SrsJsonAny*> SrsJsonObjectPropertyType;
|
||||
std::vector<SrsJsonObjectPropertyType> properties;
|
||||
private:
|
||||
// use SrsJsonAny::object() to create it.
|
||||
friend class SrsJsonAny;
|
||||
SrsJsonObject();
|
||||
public:
|
||||
virtual ~SrsJsonObject();
|
||||
public:
|
||||
virtual int count();
|
||||
// @remark: max index is count().
|
||||
virtual std::string key_at(int index);
|
||||
// @remark: max index is count().
|
||||
virtual SrsJsonAny* value_at(int index);
|
||||
public:
|
||||
virtual void set(std::string key, SrsJsonAny* value);
|
||||
virtual SrsJsonAny* get_property(std::string name);
|
||||
};
|
||||
|
||||
class SrsJsonArray : public SrsJsonAny
|
||||
{
|
||||
private:
|
||||
std::vector<SrsJsonAny*> properties;
|
||||
|
||||
private:
|
||||
// use SrsJsonAny::array() to create it.
|
||||
friend class SrsJsonAny;
|
||||
SrsJsonArray();
|
||||
public:
|
||||
virtual ~SrsJsonArray();
|
||||
public:
|
||||
virtual int count();
|
||||
// @remark: max index is count().
|
||||
virtual SrsJsonAny* at(int index);
|
||||
virtual void add(SrsJsonAny* value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
@ -81,27 +219,4 @@ that is:
|
|||
#define JARRAY_START "["
|
||||
#define JARRAY_END "]"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// json decode
|
||||
// 1. SrsJsonAny: read any from stream
|
||||
// SrsJsonAny* pany = NULL;
|
||||
// if ((ret = srs_json_read_any(stream, &pany)) != ERROR_SUCCESS) {
|
||||
// return ret;
|
||||
// }
|
||||
// srs_assert(pany); // if success, always valid object.
|
||||
// 2. SrsJsonAny: convert to specifid type, for instance, string
|
||||
// SrsJsonAny* pany = ...
|
||||
// if (pany->is_string()) {
|
||||
// string v = pany->to_str();
|
||||
// }
|
||||
//
|
||||
// for detail usage, see interfaces of each object.
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// @see: https://bitbucket.org/yarosla/nxjson
|
||||
// @see: https://github.com/udp/json-parser
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue