|
5 | 5 | #include "plus_player.h"
|
6 | 6 |
|
7 | 7 | #include <app_manager.h>
|
8 |
| -#include <json-glib/json-glib.h> |
9 | 8 | #include <system_info.h>
|
10 | 9 |
|
11 | 10 | #include <sstream>
|
12 | 11 |
|
13 | 12 | #include "log.h"
|
| 13 | +#include "rapidjson/document.h" |
| 14 | +#include "rapidjson/stringbuffer.h" |
| 15 | +#include "rapidjson/writer.h" |
14 | 16 |
|
15 | 17 | static std::vector<std::string> split(const std::string &s, char delim) {
|
16 | 18 | std::stringstream ss(s);
|
@@ -669,87 +671,72 @@ bool PlusPlayer::SetDisplayMode(int64_t display_mode) {
|
669 | 671 | }
|
670 | 672 |
|
671 | 673 | std::string BuildJsonString(const flutter::EncodableMap &data) {
|
672 |
| - std::string result; |
673 |
| - JsonBuilder *builder = json_builder_new(); |
674 |
| - json_builder_begin_object(builder); |
| 674 | + rapidjson::Document doc; |
| 675 | + doc.SetObject(); |
| 676 | + rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); |
| 677 | + |
675 | 678 | for (const auto &pair : data) {
|
676 |
| - if (std::get<std::string>(pair.first) == "max-bandwidth") { |
677 |
| - json_builder_set_member_name(builder, |
678 |
| - std::get<std::string>(pair.first).c_str()); |
679 |
| - json_builder_add_int_value(builder, std::get<int64_t>(pair.second)); |
| 679 | + std::string key_str = std::get<std::string>(pair.first); |
| 680 | + rapidjson::Value key(key_str.c_str(), allocator); |
| 681 | + if (key_str == "max-bandwidth") { |
| 682 | + doc.AddMember(key, rapidjson::Value(std::get<int64_t>(pair.second)), |
| 683 | + allocator); |
680 | 684 | } else {
|
681 |
| - json_builder_set_member_name(builder, |
682 |
| - std::get<std::string>(pair.first).c_str()); |
683 |
| - json_builder_add_string_value(builder, |
684 |
| - std::get<std::string>(pair.second).c_str()); |
| 685 | + doc.AddMember(key, |
| 686 | + rapidjson::Value(std::get<std::string>(pair.second).c_str(), |
| 687 | + allocator), |
| 688 | + allocator); |
685 | 689 | }
|
686 | 690 | }
|
687 |
| - json_builder_end_object(builder); |
688 |
| - JsonGenerator *gen = json_generator_new(); |
689 |
| - JsonNode *root = json_builder_get_root(builder); |
690 |
| - json_generator_set_root(gen, root); |
691 |
| - result = std::string(json_generator_to_data(gen, nullptr)); |
692 |
| - json_node_free(root); |
693 |
| - g_object_unref(gen); |
694 |
| - g_object_unref(builder); |
695 |
| - return result; |
| 691 | + rapidjson::StringBuffer buffer; |
| 692 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 693 | + doc.Accept(writer); |
| 694 | + return buffer.GetString(); |
696 | 695 | }
|
697 | 696 |
|
698 |
| -std::string BuildJsonString(const flutter::EncodableList &keys) { |
699 |
| - std::string result; |
700 |
| - JsonBuilder *builder = json_builder_new(); |
701 |
| - json_builder_begin_object(builder); |
702 |
| - for (const auto &key : keys) { |
703 |
| - if (std::get<std::string>(key) == "max-bandwidth") { |
704 |
| - json_builder_set_member_name(builder, std::get<std::string>(key).c_str()); |
705 |
| - json_builder_add_int_value(builder, 0); |
| 697 | +std::string BuildJsonString(const flutter::EncodableList &encodable_keys) { |
| 698 | + rapidjson::Document doc; |
| 699 | + doc.SetObject(); |
| 700 | + rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); |
| 701 | + |
| 702 | + for (const auto &encodable_key : encodable_keys) { |
| 703 | + std::string key_str = std::get<std::string>(encodable_key); |
| 704 | + rapidjson::Value key(key_str.c_str(), allocator); |
| 705 | + if (key_str == "max-bandwidth") { |
| 706 | + doc.AddMember(key, 0, allocator); |
706 | 707 | } else {
|
707 |
| - json_builder_set_member_name(builder, std::get<std::string>(key).c_str()); |
708 |
| - json_builder_add_string_value(builder, ""); |
| 708 | + doc.AddMember(key, "", allocator); |
709 | 709 | }
|
710 | 710 | }
|
711 |
| - json_builder_end_object(builder); |
712 |
| - JsonGenerator *gen = json_generator_new(); |
713 |
| - JsonNode *root = json_builder_get_root(builder); |
714 |
| - json_generator_set_root(gen, root); |
715 |
| - result = std::string(json_generator_to_data(gen, nullptr)); |
716 |
| - json_node_free(root); |
717 |
| - g_object_unref(gen); |
718 |
| - g_object_unref(builder); |
719 |
| - return result; |
| 711 | + rapidjson::StringBuffer buffer; |
| 712 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 713 | + doc.Accept(writer); |
| 714 | + return buffer.GetString(); |
720 | 715 | }
|
721 | 716 |
|
722 |
| -void ParseJsonString(std::string json_str, const flutter::EncodableList &keys, |
| 717 | +void ParseJsonString(std::string json_str, |
| 718 | + const flutter::EncodableList &encodable_keys, |
723 | 719 | flutter::EncodableMap &output) {
|
724 |
| - JsonParser *parser = json_parser_new(); |
725 |
| - GError *error = nullptr; |
726 |
| - |
727 |
| - if (!json_parser_load_from_data(parser, json_str.c_str(), -1, &error)) { |
728 |
| - LOG_ERROR("[PlusPlayer] Fail to parse json string : %s.", error->message); |
729 |
| - g_error_free(error); |
730 |
| - g_object_unref(parser); |
| 720 | + rapidjson::Document doc; |
| 721 | + doc.Parse(json_str.c_str()); |
| 722 | + if (doc.HasParseError()) { |
| 723 | + LOG_ERROR("[PlusPlayer] Fail to parse json string."); |
731 | 724 | return;
|
732 | 725 | }
|
733 |
| - |
734 |
| - JsonNode *root = json_parser_get_root(parser); |
735 |
| - if (JSON_NODE_HOLDS_OBJECT(root)) { |
736 |
| - JsonObject *root_object = json_node_get_object(root); |
737 |
| - for (const auto &key : keys) { |
738 |
| - if (json_object_has_member(root_object, |
739 |
| - std::get<std::string>(key).c_str())) { |
740 |
| - if (std::get<std::string>(key) == "max-bandwidth") { |
741 |
| - output.insert_or_assign( |
742 |
| - key, flutter::EncodableValue(json_object_get_int_member( |
743 |
| - root_object, std::get<std::string>(key).c_str()))); |
744 |
| - } else { |
745 |
| - output.insert_or_assign( |
746 |
| - key, flutter::EncodableValue(json_object_get_string_member( |
747 |
| - root_object, std::get<std::string>(key).c_str()))); |
748 |
| - } |
| 726 | + for (const auto &encodable_key : encodable_keys) { |
| 727 | + std::string key_str = std::get<std::string>(encodable_key); |
| 728 | + if (doc.HasMember(key_str.c_str())) { |
| 729 | + if (key_str == "max-bandwidth") { |
| 730 | + output.insert_or_assign( |
| 731 | + encodable_key, |
| 732 | + flutter::EncodableValue(doc[key_str.c_str()].GetInt64())); |
| 733 | + } else { |
| 734 | + output.insert_or_assign( |
| 735 | + encodable_key, |
| 736 | + flutter::EncodableValue(doc[key_str.c_str()].GetString())); |
749 | 737 | }
|
750 | 738 | }
|
751 | 739 | }
|
752 |
| - g_object_unref(parser); |
753 | 740 | }
|
754 | 741 |
|
755 | 742 | bool PlusPlayer::SetData(const flutter::EncodableMap &data) {
|
|
0 commit comments