add json base on nxjson(nx-json/nx_json) decoder for http api. 0.9.102
正在显示
4 个修改的文件
包含
525 行增加
和
73 行删除
| @@ -23,31 +23,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -23,31 +23,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | 23 | ||
| 24 | #include <srs_app_json.hpp> | 24 | #include <srs_app_json.hpp> |
| 25 | 25 | ||
| 26 | -#include <srs_kernel_log.hpp> | 26 | +using namespace std; |
| 27 | 27 | ||
| 28 | -// whether use nxjson | ||
| 29 | -// @see: https://bitbucket.org/yarosla/nxjson | ||
| 30 | -#undef SRS_JSON_USE_NXJSON | ||
| 31 | -#define SRS_JSON_USE_NXJSON | 28 | +#include <srs_kernel_log.hpp> |
| 29 | +#include <srs_kernel_error.hpp> | ||
| 32 | 30 | ||
| 33 | #ifdef SRS_JSON_USE_NXJSON | 31 | #ifdef SRS_JSON_USE_NXJSON |
| 34 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 35 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 36 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 37 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 38 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 39 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 40 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 41 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 42 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 43 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 44 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 45 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 46 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 47 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 48 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 49 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 50 | -//////////////////////////////////////////////////////////////////////////////////////////////// | 32 | + |
| 51 | //////////////////////////////////////////////////////////////////////////////////////////////// | 33 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 52 | //////////////////////////////////////////////////////////////////////////////////////////////// | 34 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 53 | //////////////////////////////////////////////////////////////////////////////////////////////// | 35 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| @@ -117,6 +99,374 @@ const nx_json* nx_json_item(const nx_json* json, int idx); // get array element | @@ -117,6 +99,374 @@ const nx_json* nx_json_item(const nx_json* json, int idx); // get array element | ||
| 117 | 99 | ||
| 118 | #endif /* NXJSON_H */ | 100 | #endif /* NXJSON_H */ |
| 119 | 101 | ||
| 102 | +#endif | ||
| 103 | + | ||
| 104 | +// Json marker | ||
| 105 | +#define SRS_JSON_Boolean 0x01 | ||
| 106 | +#define SRS_JSON_String 0x02 | ||
| 107 | +#define SRS_JSON_Object 0x03 | ||
| 108 | +#define SRS_JSON_Integer 0x04 | ||
| 109 | +#define SRS_JSON_Number 0x05 | ||
| 110 | +#define SRS_JSON_Null 0x06 | ||
| 111 | +#define SRS_JSON_Array 0x07 | ||
| 112 | + | ||
| 113 | +class __SrsJsonString : public SrsJsonAny | ||
| 114 | +{ | ||
| 115 | +public: | ||
| 116 | + std::string value; | ||
| 117 | + | ||
| 118 | + __SrsJsonString(const char* _value) | ||
| 119 | + { | ||
| 120 | + marker = SRS_JSON_String; | ||
| 121 | + if (_value) { | ||
| 122 | + value = _value; | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + virtual ~__SrsJsonString() | ||
| 126 | + { | ||
| 127 | + } | ||
| 128 | +}; | ||
| 129 | + | ||
| 130 | +class __SrsJsonBoolean : public SrsJsonAny | ||
| 131 | +{ | ||
| 132 | +public: | ||
| 133 | + bool value; | ||
| 134 | + | ||
| 135 | + __SrsJsonBoolean(bool _value) | ||
| 136 | + { | ||
| 137 | + marker = SRS_JSON_Boolean; | ||
| 138 | + value = _value; | ||
| 139 | + } | ||
| 140 | + virtual ~__SrsJsonBoolean() | ||
| 141 | + { | ||
| 142 | + } | ||
| 143 | +}; | ||
| 144 | + | ||
| 145 | +class __SrsJsonInteger : public SrsJsonAny | ||
| 146 | +{ | ||
| 147 | +public: | ||
| 148 | + int64_t value; | ||
| 149 | + | ||
| 150 | + __SrsJsonInteger(int64_t _value) | ||
| 151 | + { | ||
| 152 | + marker = SRS_JSON_Integer; | ||
| 153 | + value = _value; | ||
| 154 | + } | ||
| 155 | + virtual ~__SrsJsonInteger() | ||
| 156 | + { | ||
| 157 | + } | ||
| 158 | +}; | ||
| 159 | + | ||
| 160 | +class __SrsJsonNumber : public SrsJsonAny | ||
| 161 | +{ | ||
| 162 | +public: | ||
| 163 | + double value; | ||
| 164 | + | ||
| 165 | + __SrsJsonNumber(double _value) | ||
| 166 | + { | ||
| 167 | + marker = SRS_JSON_Number; | ||
| 168 | + value = _value; | ||
| 169 | + } | ||
| 170 | + virtual ~__SrsJsonNumber() | ||
| 171 | + { | ||
| 172 | + } | ||
| 173 | +}; | ||
| 174 | + | ||
| 175 | +class __SrsJsonNull : public SrsJsonAny | ||
| 176 | +{ | ||
| 177 | +public: | ||
| 178 | + __SrsJsonNull() { | ||
| 179 | + marker = SRS_JSON_Null; | ||
| 180 | + } | ||
| 181 | + virtual ~__SrsJsonNull() { | ||
| 182 | + } | ||
| 183 | +}; | ||
| 184 | + | ||
| 185 | +SrsJsonAny::SrsJsonAny() | ||
| 186 | +{ | ||
| 187 | + marker = 0; | ||
| 188 | +} | ||
| 189 | + | ||
| 190 | +SrsJsonAny::~SrsJsonAny() | ||
| 191 | +{ | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +bool SrsJsonAny::is_string() | ||
| 195 | +{ | ||
| 196 | + return marker == SRS_JSON_String; | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +bool SrsJsonAny::is_boolean() | ||
| 200 | +{ | ||
| 201 | + return marker == SRS_JSON_Boolean; | ||
| 202 | +} | ||
| 203 | + | ||
| 204 | +bool SrsJsonAny::is_number() | ||
| 205 | +{ | ||
| 206 | + return marker == SRS_JSON_Number; | ||
| 207 | +} | ||
| 208 | + | ||
| 209 | +bool SrsJsonAny::is_integer() | ||
| 210 | +{ | ||
| 211 | + return marker == SRS_JSON_Integer; | ||
| 212 | +} | ||
| 213 | + | ||
| 214 | +bool SrsJsonAny::is_object() | ||
| 215 | +{ | ||
| 216 | + return marker == SRS_JSON_Object; | ||
| 217 | +} | ||
| 218 | + | ||
| 219 | +bool SrsJsonAny::is_array() | ||
| 220 | +{ | ||
| 221 | + return marker == SRS_JSON_Array; | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +bool SrsJsonAny::is_null() | ||
| 225 | +{ | ||
| 226 | + return marker == SRS_JSON_Null; | ||
| 227 | +} | ||
| 228 | + | ||
| 229 | +string SrsJsonAny::to_str() | ||
| 230 | +{ | ||
| 231 | + __SrsJsonString* p = dynamic_cast<__SrsJsonString*>(this); | ||
| 232 | + srs_assert(p != NULL); | ||
| 233 | + return p->value; | ||
| 234 | +} | ||
| 235 | + | ||
| 236 | +bool SrsJsonAny::to_boolean() | ||
| 237 | +{ | ||
| 238 | + __SrsJsonBoolean* p = dynamic_cast<__SrsJsonBoolean*>(this); | ||
| 239 | + srs_assert(p != NULL); | ||
| 240 | + return p->value; | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +int64_t SrsJsonAny::to_integer() | ||
| 244 | +{ | ||
| 245 | + __SrsJsonInteger* p = dynamic_cast<__SrsJsonInteger*>(this); | ||
| 246 | + srs_assert(p != NULL); | ||
| 247 | + return p->value; | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +double SrsJsonAny::to_number() | ||
| 251 | +{ | ||
| 252 | + __SrsJsonNumber* p = dynamic_cast<__SrsJsonNumber*>(this); | ||
| 253 | + srs_assert(p != NULL); | ||
| 254 | + return p->value; | ||
| 255 | +} | ||
| 256 | + | ||
| 257 | +SrsJsonObject* SrsJsonAny::to_object() | ||
| 258 | +{ | ||
| 259 | + SrsJsonObject* p = dynamic_cast<SrsJsonObject*>(this); | ||
| 260 | + srs_assert(p != NULL); | ||
| 261 | + return p; | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +SrsJsonArray* SrsJsonAny::to_array() | ||
| 265 | +{ | ||
| 266 | + SrsJsonArray* p = dynamic_cast<SrsJsonArray*>(this); | ||
| 267 | + srs_assert(p != NULL); | ||
| 268 | + return p; | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +SrsJsonAny* SrsJsonAny::str(const char* value) | ||
| 272 | +{ | ||
| 273 | + return new __SrsJsonString(value); | ||
| 274 | +} | ||
| 275 | + | ||
| 276 | +SrsJsonAny* SrsJsonAny::boolean(bool value) | ||
| 277 | +{ | ||
| 278 | + return new __SrsJsonBoolean(value); | ||
| 279 | +} | ||
| 280 | + | ||
| 281 | +SrsJsonAny* SrsJsonAny::ingeter(int64_t value) | ||
| 282 | +{ | ||
| 283 | + return new __SrsJsonInteger(value); | ||
| 284 | +} | ||
| 285 | + | ||
| 286 | +SrsJsonAny* SrsJsonAny::number(double value) | ||
| 287 | +{ | ||
| 288 | + return new __SrsJsonNumber(value); | ||
| 289 | +} | ||
| 290 | + | ||
| 291 | +SrsJsonAny* SrsJsonAny::null() | ||
| 292 | +{ | ||
| 293 | + return new __SrsJsonNull(); | ||
| 294 | +} | ||
| 295 | + | ||
| 296 | +SrsJsonObject* SrsJsonAny::object() | ||
| 297 | +{ | ||
| 298 | + return new SrsJsonObject(); | ||
| 299 | +} | ||
| 300 | + | ||
| 301 | +SrsJsonArray* SrsJsonAny::array() | ||
| 302 | +{ | ||
| 303 | + return new SrsJsonArray(); | ||
| 304 | +} | ||
| 305 | + | ||
| 306 | +#ifdef SRS_JSON_USE_NXJSON | ||
| 307 | +SrsJsonAny* srs_json_parse_tree_nx_json(const nx_json* node) | ||
| 308 | +{ | ||
| 309 | + switch (node->type) { | ||
| 310 | + case NX_JSON_NULL: | ||
| 311 | + return SrsJsonAny::null(); | ||
| 312 | + case NX_JSON_STRING: | ||
| 313 | + return SrsJsonAny::str(node->text_value); | ||
| 314 | + case NX_JSON_INTEGER: | ||
| 315 | + return SrsJsonAny::ingeter(node->int_value); | ||
| 316 | + case NX_JSON_DOUBLE: | ||
| 317 | + return SrsJsonAny::number(node->dbl_value); | ||
| 318 | + case NX_JSON_BOOL: | ||
| 319 | + return SrsJsonAny::boolean(node->int_value != 0); | ||
| 320 | + case NX_JSON_OBJECT: { | ||
| 321 | + SrsJsonObject* obj = SrsJsonAny::object(); | ||
| 322 | + for (nx_json* p = node->child; p != NULL; p = p->next) { | ||
| 323 | + SrsJsonAny* value = srs_json_parse_tree_nx_json(p); | ||
| 324 | + if (value) { | ||
| 325 | + obj->set(p->key, value); | ||
| 326 | + } | ||
| 327 | + } | ||
| 328 | + return obj; | ||
| 329 | + } | ||
| 330 | + case NX_JSON_ARRAY: { | ||
| 331 | + SrsJsonArray* arr = SrsJsonAny::array(); | ||
| 332 | + for (nx_json* p = node->child; p != NULL; p = p->next) { | ||
| 333 | + SrsJsonAny* value = srs_json_parse_tree_nx_json(p); | ||
| 334 | + if (value) { | ||
| 335 | + arr->add(value); | ||
| 336 | + } | ||
| 337 | + } | ||
| 338 | + return arr; | ||
| 339 | + } | ||
| 340 | + } | ||
| 341 | + return NULL; | ||
| 342 | +} | ||
| 343 | + | ||
| 344 | +SrsJsonAny* SrsJsonAny::loads(char* str) | ||
| 345 | +{ | ||
| 346 | + srs_assert(str); | ||
| 347 | + if (strlen(str) == 0) { | ||
| 348 | + return NULL; | ||
| 349 | + } | ||
| 350 | + | ||
| 351 | + const nx_json* o = nx_json_parse(str, 0); | ||
| 352 | + SrsJsonAny* json = srs_json_parse_tree_nx_json(o); | ||
| 353 | + nx_json_free(o); | ||
| 354 | + return json; | ||
| 355 | +} | ||
| 356 | +#endif | ||
| 357 | + | ||
| 358 | +SrsJsonObject::SrsJsonObject() | ||
| 359 | +{ | ||
| 360 | + marker = SRS_JSON_Object; | ||
| 361 | +} | ||
| 362 | + | ||
| 363 | +SrsJsonObject::~SrsJsonObject() | ||
| 364 | +{ | ||
| 365 | + std::vector<SrsJsonObjectPropertyType>::iterator it; | ||
| 366 | + for (it = properties.begin(); it != properties.end(); ++it) { | ||
| 367 | + SrsJsonObjectPropertyType item = *it; | ||
| 368 | + SrsJsonAny* obj = item.second; | ||
| 369 | + srs_freep(obj); | ||
| 370 | + } | ||
| 371 | + properties.clear(); | ||
| 372 | +} | ||
| 373 | + | ||
| 374 | +int SrsJsonObject::count() | ||
| 375 | +{ | ||
| 376 | + return (int)properties.size(); | ||
| 377 | +} | ||
| 378 | + | ||
| 379 | +string SrsJsonObject::key_at(int index) | ||
| 380 | +{ | ||
| 381 | + srs_assert(index < count()); | ||
| 382 | + SrsJsonObjectPropertyType& elem = properties[index]; | ||
| 383 | + return elem.first; | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +SrsJsonAny* SrsJsonObject::value_at(int index) | ||
| 387 | +{ | ||
| 388 | + srs_assert(index < count()); | ||
| 389 | + SrsJsonObjectPropertyType& elem = properties[index]; | ||
| 390 | + return elem.second; | ||
| 391 | +} | ||
| 392 | + | ||
| 393 | +void SrsJsonObject::set(string key, SrsJsonAny* value) | ||
| 394 | +{ | ||
| 395 | + if (!value) { | ||
| 396 | + srs_warn("add a NULL propertity %s", key.c_str()); | ||
| 397 | + return; | ||
| 398 | + } | ||
| 399 | + | ||
| 400 | + std::vector<SrsJsonObjectPropertyType>::iterator it; | ||
| 401 | + | ||
| 402 | + for (it = properties.begin(); it != properties.end(); ++it) { | ||
| 403 | + SrsJsonObjectPropertyType& elem = *it; | ||
| 404 | + std::string name = elem.first; | ||
| 405 | + SrsJsonAny* any = elem.second; | ||
| 406 | + | ||
| 407 | + if (key == name) { | ||
| 408 | + srs_freep(any); | ||
| 409 | + properties.erase(it); | ||
| 410 | + break; | ||
| 411 | + } | ||
| 412 | + } | ||
| 413 | + | ||
| 414 | + properties.push_back(std::make_pair(key, value)); | ||
| 415 | +} | ||
| 416 | + | ||
| 417 | +SrsJsonAny* SrsJsonObject::get_property(string name) | ||
| 418 | +{ | ||
| 419 | + std::vector<SrsJsonObjectPropertyType>::iterator it; | ||
| 420 | + | ||
| 421 | + for (it = properties.begin(); it != properties.end(); ++it) { | ||
| 422 | + SrsJsonObjectPropertyType& elem = *it; | ||
| 423 | + std::string key = elem.first; | ||
| 424 | + SrsJsonAny* any = elem.second; | ||
| 425 | + if (key == name) { | ||
| 426 | + return any; | ||
| 427 | + } | ||
| 428 | + } | ||
| 429 | + | ||
| 430 | + return NULL; | ||
| 431 | +} | ||
| 432 | + | ||
| 433 | +SrsJsonArray::SrsJsonArray() | ||
| 434 | +{ | ||
| 435 | + marker = SRS_JSON_Array; | ||
| 436 | +} | ||
| 437 | + | ||
| 438 | +SrsJsonArray::~SrsJsonArray() | ||
| 439 | +{ | ||
| 440 | + std::vector<SrsJsonAny*>::iterator it; | ||
| 441 | + for (it = properties.begin(); it != properties.end(); ++it) { | ||
| 442 | + SrsJsonAny* item = *it; | ||
| 443 | + srs_freep(item); | ||
| 444 | + } | ||
| 445 | + properties.clear(); | ||
| 446 | +} | ||
| 447 | + | ||
| 448 | +int SrsJsonArray::count() | ||
| 449 | +{ | ||
| 450 | + return (int)properties.size(); | ||
| 451 | +} | ||
| 452 | + | ||
| 453 | +SrsJsonAny* SrsJsonArray::at(int index) | ||
| 454 | +{ | ||
| 455 | + srs_assert(index < count()); | ||
| 456 | + SrsJsonAny* elem = properties[index]; | ||
| 457 | + return elem; | ||
| 458 | +} | ||
| 459 | + | ||
| 460 | +void SrsJsonArray::add(SrsJsonAny* value) | ||
| 461 | +{ | ||
| 462 | + properties.push_back(value); | ||
| 463 | +} | ||
| 464 | + | ||
| 465 | +#ifdef SRS_JSON_USE_NXJSON | ||
| 466 | + | ||
| 467 | +//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 468 | +//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 469 | +//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 120 | /* | 470 | /* |
| 121 | * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com> | 471 | * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com> |
| 122 | * | 472 | * |
| @@ -170,6 +520,7 @@ static const nx_json dummy={ NX_JSON_NULL }; | @@ -170,6 +520,7 @@ static const nx_json dummy={ NX_JSON_NULL }; | ||
| 170 | 520 | ||
| 171 | static nx_json* create_json(nx_json_type type, const char* key, nx_json* parent) { | 521 | static nx_json* create_json(nx_json_type type, const char* key, nx_json* parent) { |
| 172 | nx_json* js=(nx_json*)NX_JSON_CALLOC(); | 522 | nx_json* js=(nx_json*)NX_JSON_CALLOC(); |
| 523 | + memset(js, 0, sizeof(nx_json)); | ||
| 173 | assert(js); | 524 | assert(js); |
| 174 | js->type=type; | 525 | js->type=type; |
| 175 | js->key=key; | 526 | js->key=key; |
| @@ -475,6 +826,7 @@ const nx_json* nx_json_parse_utf8(char* text) { | @@ -475,6 +826,7 @@ const nx_json* nx_json_parse_utf8(char* text) { | ||
| 475 | 826 | ||
| 476 | const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder) { | 827 | const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder) { |
| 477 | nx_json js; | 828 | nx_json js; |
| 829 | + memset(&js, 0, sizeof(nx_json)); | ||
| 478 | if (!parse_value(&js, 0, text, encoder)) { | 830 | if (!parse_value(&js, 0, text, encoder)) { |
| 479 | if (js.child) nx_json_free(js.child); | 831 | if (js.child) nx_json_free(js.child); |
| 480 | return 0; | 832 | return 0; |
| @@ -506,25 +858,10 @@ const nx_json* nx_json_item(const nx_json* json, int idx) { | @@ -506,25 +858,10 @@ const nx_json* nx_json_item(const nx_json* json, int idx) { | ||
| 506 | #endif | 858 | #endif |
| 507 | 859 | ||
| 508 | #endif /* NXJSON_C */ | 860 | #endif /* NXJSON_C */ |
| 861 | + | ||
| 509 | //////////////////////////////////////////////////////////////////////////////////////////////// | 862 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 510 | //////////////////////////////////////////////////////////////////////////////////////////////// | 863 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 511 | //////////////////////////////////////////////////////////////////////////////////////////////// | 864 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 512 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 513 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 514 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 515 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 516 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 517 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 518 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 519 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 520 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 521 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 522 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 523 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 524 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 525 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 526 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 527 | -//////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 528 | -//////////////////////////////////////////////////////////////////////////////////////////////// | 865 | + |
| 529 | #endif | 866 | #endif |
| 530 | 867 |
| @@ -29,6 +29,144 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -29,6 +29,144 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 29 | */ | 29 | */ |
| 30 | #include <srs_core.hpp> | 30 | #include <srs_core.hpp> |
| 31 | 31 | ||
| 32 | +#include <string> | ||
| 33 | +#include <vector> | ||
| 34 | + | ||
| 35 | +// whether use nxjson | ||
| 36 | +// @see: https://bitbucket.org/yarosla/nxjson | ||
| 37 | +#undef SRS_JSON_USE_NXJSON | ||
| 38 | +#define SRS_JSON_USE_NXJSON | ||
| 39 | + | ||
| 40 | +//////////////////////////////////////////////////////////////////////// | ||
| 41 | +//////////////////////////////////////////////////////////////////////// | ||
| 42 | +//////////////////////////////////////////////////////////////////////// | ||
| 43 | +// json decode | ||
| 44 | +// 1. SrsJsonAny: read any from str:char* | ||
| 45 | +// SrsJsonAny* pany = NULL; | ||
| 46 | +// if ((ret = srs_json_read_any(str, &pany)) != ERROR_SUCCESS) { | ||
| 47 | +// return ret; | ||
| 48 | +// } | ||
| 49 | +// srs_assert(pany); // if success, always valid object. | ||
| 50 | +// 2. SrsJsonAny: convert to specifid type, for instance, string | ||
| 51 | +// SrsJsonAny* pany = ... | ||
| 52 | +// if (pany->is_string()) { | ||
| 53 | +// string v = pany->to_str(); | ||
| 54 | +// } | ||
| 55 | +// | ||
| 56 | +// for detail usage, see interfaces of each object. | ||
| 57 | +//////////////////////////////////////////////////////////////////////// | ||
| 58 | +//////////////////////////////////////////////////////////////////////// | ||
| 59 | +//////////////////////////////////////////////////////////////////////// | ||
| 60 | +// @see: https://bitbucket.org/yarosla/nxjson | ||
| 61 | +// @see: https://github.com/udp/json-parser | ||
| 62 | + | ||
| 63 | +class SrsJsonArray; | ||
| 64 | +class SrsJsonObject; | ||
| 65 | + | ||
| 66 | +class SrsJsonAny | ||
| 67 | +{ | ||
| 68 | +public: | ||
| 69 | + char marker; | ||
| 70 | +// donot directly create this object, | ||
| 71 | +// instead, for examle, use SrsJsonAny::str() to create a concreated one. | ||
| 72 | +protected: | ||
| 73 | + SrsJsonAny(); | ||
| 74 | +public: | ||
| 75 | + virtual ~SrsJsonAny(); | ||
| 76 | +public: | ||
| 77 | + virtual bool is_string(); | ||
| 78 | + virtual bool is_boolean(); | ||
| 79 | + virtual bool is_integer(); | ||
| 80 | + virtual bool is_number(); | ||
| 81 | + virtual bool is_object(); | ||
| 82 | + virtual bool is_array(); | ||
| 83 | + virtual bool is_null(); | ||
| 84 | +public: | ||
| 85 | + /** | ||
| 86 | + * get the string of any when is_string() indicates true. | ||
| 87 | + * user must ensure the type is a string, or assert failed. | ||
| 88 | + */ | ||
| 89 | + virtual std::string to_str(); | ||
| 90 | + /** | ||
| 91 | + * get the boolean of any when is_boolean() indicates true. | ||
| 92 | + * user must ensure the type is a boolean, or assert failed. | ||
| 93 | + */ | ||
| 94 | + virtual bool to_boolean(); | ||
| 95 | + /** | ||
| 96 | + * get the integer of any when is_integer() indicates true. | ||
| 97 | + * user must ensure the type is a integer, or assert failed. | ||
| 98 | + */ | ||
| 99 | + virtual int64_t to_integer(); | ||
| 100 | + /** | ||
| 101 | + * get the number of any when is_number() indicates true. | ||
| 102 | + * user must ensure the type is a number, or assert failed. | ||
| 103 | + */ | ||
| 104 | + virtual double to_number(); | ||
| 105 | + /** | ||
| 106 | + * get the object of any when is_object() indicates true. | ||
| 107 | + * user must ensure the type is a object, or assert failed. | ||
| 108 | + */ | ||
| 109 | + virtual SrsJsonObject* to_object(); | ||
| 110 | + /** | ||
| 111 | + * get the ecma array of any when is_ecma_array() indicates true. | ||
| 112 | + * user must ensure the type is a ecma array, or assert failed. | ||
| 113 | + */ | ||
| 114 | + virtual SrsJsonArray* to_array(); | ||
| 115 | +public: | ||
| 116 | + static SrsJsonAny* str(const char* value = NULL); | ||
| 117 | + static SrsJsonAny* boolean(bool value = false); | ||
| 118 | + static SrsJsonAny* ingeter(int64_t value = 0); | ||
| 119 | + static SrsJsonAny* number(double value = 0.0); | ||
| 120 | + static SrsJsonAny* null(); | ||
| 121 | + static SrsJsonObject* object(); | ||
| 122 | + static SrsJsonArray* array(); | ||
| 123 | +public: | ||
| 124 | + /** | ||
| 125 | + * read json tree from str:char* | ||
| 126 | + */ | ||
| 127 | + static SrsJsonAny* loads(char* str); | ||
| 128 | +}; | ||
| 129 | + | ||
| 130 | +class SrsJsonObject : public SrsJsonAny | ||
| 131 | +{ | ||
| 132 | +private: | ||
| 133 | + typedef std::pair<std::string, SrsJsonAny*> SrsJsonObjectPropertyType; | ||
| 134 | + std::vector<SrsJsonObjectPropertyType> properties; | ||
| 135 | +private: | ||
| 136 | + // use SrsJsonAny::object() to create it. | ||
| 137 | + friend class SrsJsonAny; | ||
| 138 | + SrsJsonObject(); | ||
| 139 | +public: | ||
| 140 | + virtual ~SrsJsonObject(); | ||
| 141 | +public: | ||
| 142 | + virtual int count(); | ||
| 143 | + // @remark: max index is count(). | ||
| 144 | + virtual std::string key_at(int index); | ||
| 145 | + // @remark: max index is count(). | ||
| 146 | + virtual SrsJsonAny* value_at(int index); | ||
| 147 | +public: | ||
| 148 | + virtual void set(std::string key, SrsJsonAny* value); | ||
| 149 | + virtual SrsJsonAny* get_property(std::string name); | ||
| 150 | +}; | ||
| 151 | + | ||
| 152 | +class SrsJsonArray : public SrsJsonAny | ||
| 153 | +{ | ||
| 154 | +private: | ||
| 155 | + std::vector<SrsJsonAny*> properties; | ||
| 156 | + | ||
| 157 | +private: | ||
| 158 | + // use SrsJsonAny::array() to create it. | ||
| 159 | + friend class SrsJsonAny; | ||
| 160 | + SrsJsonArray(); | ||
| 161 | +public: | ||
| 162 | + virtual ~SrsJsonArray(); | ||
| 163 | +public: | ||
| 164 | + virtual int count(); | ||
| 165 | + // @remark: max index is count(). | ||
| 166 | + virtual SrsJsonAny* at(int index); | ||
| 167 | + virtual void add(SrsJsonAny* value); | ||
| 168 | +}; | ||
| 169 | + | ||
| 32 | //////////////////////////////////////////////////////////////////////// | 170 | //////////////////////////////////////////////////////////////////////// |
| 33 | //////////////////////////////////////////////////////////////////////// | 171 | //////////////////////////////////////////////////////////////////////// |
| 34 | //////////////////////////////////////////////////////////////////////// | 172 | //////////////////////////////////////////////////////////////////////// |
| @@ -81,27 +219,4 @@ that is: | @@ -81,27 +219,4 @@ that is: | ||
| 81 | #define JARRAY_START "[" | 219 | #define JARRAY_START "[" |
| 82 | #define JARRAY_END "]" | 220 | #define JARRAY_END "]" |
| 83 | 221 | ||
| 84 | -//////////////////////////////////////////////////////////////////////// | ||
| 85 | -//////////////////////////////////////////////////////////////////////// | ||
| 86 | -//////////////////////////////////////////////////////////////////////// | ||
| 87 | -// json decode | ||
| 88 | -// 1. SrsJsonAny: read any from stream | ||
| 89 | -// SrsJsonAny* pany = NULL; | ||
| 90 | -// if ((ret = srs_json_read_any(stream, &pany)) != ERROR_SUCCESS) { | ||
| 91 | -// return ret; | ||
| 92 | -// } | ||
| 93 | -// srs_assert(pany); // if success, always valid object. | ||
| 94 | -// 2. SrsJsonAny: convert to specifid type, for instance, string | ||
| 95 | -// SrsJsonAny* pany = ... | ||
| 96 | -// if (pany->is_string()) { | ||
| 97 | -// string v = pany->to_str(); | ||
| 98 | -// } | ||
| 99 | -// | ||
| 100 | -// for detail usage, see interfaces of each object. | ||
| 101 | -//////////////////////////////////////////////////////////////////////// | ||
| 102 | -//////////////////////////////////////////////////////////////////////// | ||
| 103 | -//////////////////////////////////////////////////////////////////////// | ||
| 104 | -// @see: https://bitbucket.org/yarosla/nxjson | ||
| 105 | -// @see: https://github.com/udp/json-parser | ||
| 106 | - | ||
| 107 | #endif | 222 | #endif |
| @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 31 | // current release version | 31 | // current release version |
| 32 | #define VERSION_MAJOR "0" | 32 | #define VERSION_MAJOR "0" |
| 33 | #define VERSION_MINOR "9" | 33 | #define VERSION_MINOR "9" |
| 34 | -#define VERSION_REVISION "101" | 34 | +#define VERSION_REVISION "102" |
| 35 | #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION | 35 | #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION |
| 36 | // server info. | 36 | // server info. |
| 37 | #define RTMP_SIG_SRS_KEY "srs" | 37 | #define RTMP_SIG_SRS_KEY "srs" |
| @@ -157,8 +157,8 @@ public: | @@ -157,8 +157,8 @@ public: | ||
| 157 | class __SrsUnSortedHashtable | 157 | class __SrsUnSortedHashtable |
| 158 | { | 158 | { |
| 159 | private: | 159 | private: |
| 160 | - typedef std::pair<std::string, SrsAmf0Any*> SrsObjectPropertyType; | ||
| 161 | - std::vector<SrsObjectPropertyType> properties; | 160 | + typedef std::pair<std::string, SrsAmf0Any*> SrsAmf0ObjectPropertyType; |
| 161 | + std::vector<SrsAmf0ObjectPropertyType> properties; | ||
| 162 | public: | 162 | public: |
| 163 | __SrsUnSortedHashtable(); | 163 | __SrsUnSortedHashtable(); |
| 164 | virtual ~__SrsUnSortedHashtable(); | 164 | virtual ~__SrsUnSortedHashtable(); |
| @@ -399,9 +399,9 @@ __SrsUnSortedHashtable::__SrsUnSortedHashtable() | @@ -399,9 +399,9 @@ __SrsUnSortedHashtable::__SrsUnSortedHashtable() | ||
| 399 | 399 | ||
| 400 | __SrsUnSortedHashtable::~__SrsUnSortedHashtable() | 400 | __SrsUnSortedHashtable::~__SrsUnSortedHashtable() |
| 401 | { | 401 | { |
| 402 | - std::vector<SrsObjectPropertyType>::iterator it; | 402 | + std::vector<SrsAmf0ObjectPropertyType>::iterator it; |
| 403 | for (it = properties.begin(); it != properties.end(); ++it) { | 403 | for (it = properties.begin(); it != properties.end(); ++it) { |
| 404 | - SrsObjectPropertyType& elem = *it; | 404 | + SrsAmf0ObjectPropertyType& elem = *it; |
| 405 | SrsAmf0Any* any = elem.second; | 405 | SrsAmf0Any* any = elem.second; |
| 406 | srs_freep(any); | 406 | srs_freep(any); |
| 407 | } | 407 | } |
| @@ -421,14 +421,14 @@ void __SrsUnSortedHashtable::clear() | @@ -421,14 +421,14 @@ void __SrsUnSortedHashtable::clear() | ||
| 421 | string __SrsUnSortedHashtable::key_at(int index) | 421 | string __SrsUnSortedHashtable::key_at(int index) |
| 422 | { | 422 | { |
| 423 | srs_assert(index < count()); | 423 | srs_assert(index < count()); |
| 424 | - SrsObjectPropertyType& elem = properties[index]; | 424 | + SrsAmf0ObjectPropertyType& elem = properties[index]; |
| 425 | return elem.first; | 425 | return elem.first; |
| 426 | } | 426 | } |
| 427 | 427 | ||
| 428 | SrsAmf0Any* __SrsUnSortedHashtable::value_at(int index) | 428 | SrsAmf0Any* __SrsUnSortedHashtable::value_at(int index) |
| 429 | { | 429 | { |
| 430 | srs_assert(index < count()); | 430 | srs_assert(index < count()); |
| 431 | - SrsObjectPropertyType& elem = properties[index]; | 431 | + SrsAmf0ObjectPropertyType& elem = properties[index]; |
| 432 | return elem.second; | 432 | return elem.second; |
| 433 | } | 433 | } |
| 434 | 434 | ||
| @@ -439,10 +439,10 @@ void __SrsUnSortedHashtable::set(string key, SrsAmf0Any* value) | @@ -439,10 +439,10 @@ void __SrsUnSortedHashtable::set(string key, SrsAmf0Any* value) | ||
| 439 | return; | 439 | return; |
| 440 | } | 440 | } |
| 441 | 441 | ||
| 442 | - std::vector<SrsObjectPropertyType>::iterator it; | 442 | + std::vector<SrsAmf0ObjectPropertyType>::iterator it; |
| 443 | 443 | ||
| 444 | for (it = properties.begin(); it != properties.end(); ++it) { | 444 | for (it = properties.begin(); it != properties.end(); ++it) { |
| 445 | - SrsObjectPropertyType& elem = *it; | 445 | + SrsAmf0ObjectPropertyType& elem = *it; |
| 446 | std::string name = elem.first; | 446 | std::string name = elem.first; |
| 447 | SrsAmf0Any* any = elem.second; | 447 | SrsAmf0Any* any = elem.second; |
| 448 | 448 | ||
| @@ -458,10 +458,10 @@ void __SrsUnSortedHashtable::set(string key, SrsAmf0Any* value) | @@ -458,10 +458,10 @@ void __SrsUnSortedHashtable::set(string key, SrsAmf0Any* value) | ||
| 458 | 458 | ||
| 459 | SrsAmf0Any* __SrsUnSortedHashtable::get_property(string name) | 459 | SrsAmf0Any* __SrsUnSortedHashtable::get_property(string name) |
| 460 | { | 460 | { |
| 461 | - std::vector<SrsObjectPropertyType>::iterator it; | 461 | + std::vector<SrsAmf0ObjectPropertyType>::iterator it; |
| 462 | 462 | ||
| 463 | for (it = properties.begin(); it != properties.end(); ++it) { | 463 | for (it = properties.begin(); it != properties.end(); ++it) { |
| 464 | - SrsObjectPropertyType& elem = *it; | 464 | + SrsAmf0ObjectPropertyType& elem = *it; |
| 465 | std::string key = elem.first; | 465 | std::string key = elem.first; |
| 466 | SrsAmf0Any* any = elem.second; | 466 | SrsAmf0Any* any = elem.second; |
| 467 | if (key == name) { | 467 | if (key == name) { |
-
请 注册 或 登录 后发表评论