2014-04-03 03:49:14 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-12-23 03:35:40 +00:00
|
|
|
Copyright (c) 2013-2016 SRS(ossrs)
|
2014-04-03 03:49:14 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-08-21 08:20:19 +00:00
|
|
|
#ifndef SRS_PROTOCOL_JSON_HPP
|
|
|
|
#define SRS_PROTOCOL_JSON_HPP
|
2014-04-03 03:49:14 +00:00
|
|
|
|
|
|
|
/*
|
2015-08-21 08:20:19 +00:00
|
|
|
#include <srs_protocol_json.hpp>
|
2014-04-03 03:49:14 +00:00
|
|
|
*/
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2014-05-18 06:34:45 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// whether use nxjson
|
|
|
|
// @see: https://bitbucket.org/yarosla/nxjson
|
2015-03-21 02:25:03 +00:00
|
|
|
#undef SRS_JSON_USE_NXJSON
|
|
|
|
#define SRS_JSON_USE_NXJSON
|
2014-05-18 06:34:45 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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
|
|
|
|
|
2015-09-19 05:31:57 +00:00
|
|
|
class SrsAmf0Any;
|
2014-05-18 06:34:45 +00:00
|
|
|
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();
|
2015-09-19 04:27:31 +00:00
|
|
|
public:
|
2015-09-19 05:37:56 +00:00
|
|
|
virtual std::string dumps();
|
2015-09-19 05:31:57 +00:00
|
|
|
virtual SrsAmf0Any* to_amf0();
|
2014-05-18 06:34:45 +00:00
|
|
|
public:
|
|
|
|
static SrsJsonAny* str(const char* value = NULL);
|
|
|
|
static SrsJsonAny* boolean(bool value = false);
|
2015-09-19 05:31:57 +00:00
|
|
|
static SrsJsonAny* integer(int64_t value = 0);
|
2014-05-18 06:34:45 +00:00
|
|
|
static SrsJsonAny* number(double value = 0.0);
|
|
|
|
static SrsJsonAny* null();
|
|
|
|
static SrsJsonObject* object();
|
|
|
|
static SrsJsonArray* array();
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* read json tree from str:char*
|
2015-02-21 15:09:21 +00:00
|
|
|
* @return json object. NULL if error.
|
2014-05-18 06:34:45 +00:00
|
|
|
*/
|
|
|
|
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);
|
2015-09-19 04:27:31 +00:00
|
|
|
public:
|
2015-09-19 05:37:56 +00:00
|
|
|
virtual std::string dumps();
|
2015-09-19 05:31:57 +00:00
|
|
|
virtual SrsAmf0Any* to_amf0();
|
2014-05-18 06:34:45 +00:00
|
|
|
public:
|
|
|
|
virtual void set(std::string key, SrsJsonAny* value);
|
|
|
|
virtual SrsJsonAny* get_property(std::string name);
|
2014-05-18 08:15:35 +00:00
|
|
|
virtual SrsJsonAny* ensure_property_string(std::string name);
|
2015-03-31 10:06:55 +00:00
|
|
|
virtual SrsJsonAny* ensure_property_integer(std::string name);
|
2015-12-07 10:22:55 +00:00
|
|
|
virtual SrsJsonAny* ensure_property_number(std::string name);
|
2015-02-21 15:09:21 +00:00
|
|
|
virtual SrsJsonAny* ensure_property_boolean(std::string name);
|
2015-09-17 05:36:02 +00:00
|
|
|
virtual SrsJsonAny* ensure_property_object(std::string name);
|
|
|
|
virtual SrsJsonAny* ensure_property_array(std::string name);
|
2014-05-18 06:34:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2015-09-19 04:27:31 +00:00
|
|
|
// alias to add.
|
|
|
|
virtual void append(SrsJsonAny* value);
|
|
|
|
public:
|
2015-09-19 05:37:56 +00:00
|
|
|
virtual std::string dumps();
|
2015-09-19 05:31:57 +00:00
|
|
|
virtual SrsAmf0Any* to_amf0();
|
2014-05-18 06:34:45 +00:00
|
|
|
};
|
|
|
|
|
2014-05-16 03:56:43 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2015-09-19 05:37:56 +00:00
|
|
|
// json encode, please use JSON.dumps() to encode json object.
|
2014-04-03 03:49:14 +00:00
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
#endif
|