From 8a723db303f28d0befcb0e9e5cd070ba3492e610 Mon Sep 17 00:00:00 2001 From: Michal Michalski Date: Wed, 24 Jul 2019 10:38:45 +0200 Subject: [PATCH] [tizen][mediacontroller] Change toJSON implementation to simplify serialization. Before this commit, in order to send Bundle object to the C++ layer it was necessary to call Bundle.toString() method, and than picojson::parse() in C++ to deserialize it. This commit removes the need for both this steps. They will be done implicitly by the framework. [Verification] bundle js tests 100% pass rate. Signed-off-by: Michal Michalski Change-Id: I89b11bc634b56b720dd8a5ecea8537ef1ce51b24 --- src/mediacontroller/mediacontroller_api.js | 4 ++-- src/mediacontroller/mediacontroller_instance.cc | 23 ++++------------------- src/mediacontroller/mediacontroller_server.cc | 1 - src/tizen/js/ut/bundle_ut.js | 6 +++--- src/tizen/tizen_api.js | 16 +++++----------- 5 files changed, 14 insertions(+), 36 deletions(-) diff --git a/src/mediacontroller/mediacontroller_api.js b/src/mediacontroller/mediacontroller_api.js index 7ad023c..4a78a2c 100755 --- a/src/mediacontroller/mediacontroller_api.js +++ b/src/mediacontroller/mediacontroller_api.js @@ -124,7 +124,7 @@ var ServerCommandListener = new ListenerManager(native_, '_ServerCommandListener var nativeData = { clientName: msg.clientName, requestId: msg.requestId, - data: bundle.toString() + data: bundle }; var result = native_.callSync('MediaControllerServer_replyCommand', nativeData); @@ -1100,7 +1100,7 @@ MediaControllerServerInfo.prototype.sendCommand = function(command, data, succes var nativeData = { command: args.command, - data: bundle.toString(), + data: bundle, name: this.name, listenerId: ReplyCommandListener.listenerName }; diff --git a/src/mediacontroller/mediacontroller_instance.cc b/src/mediacontroller/mediacontroller_instance.cc index 2d0696f..2629d35 100644 --- a/src/mediacontroller/mediacontroller_instance.cc +++ b/src/mediacontroller/mediacontroller_instance.cc @@ -449,14 +449,7 @@ void MediaControllerInstance::MediaControllerServerReplyCommand(const picojson:: CHECK_EXIST(args, "requestId", out) CHECK_EXIST(args, "data", out) - auto serialized_data = args.get("data").get(); - picojson::value json; - std::string err = picojson::parse(json, serialized_data); - if (!err.empty()) { - LoggerE("json parse error: %s", err.c_str()); - return; - } - + picojson::value json = args.get("data"); auto result = server_->CommandReply(args.get("clientName").get(), args.get("requestId").get(), json); @@ -922,17 +915,9 @@ void MediaControllerInstance::MediaControllerServerInfoSendCommand(const picojso free(request_id); }; - auto serialized_data = args.get("data").get(); - picojson::value json; - std::string err = picojson::parse(json, serialized_data); - if (!err.empty()) { - LoggerE("json parse error: %s", err.c_str()); - return; - } - - PlatformResult result = - client_->SendCommand(args.get("name").get(), - args.get("command").get(), json, reply_cb, &request_id); + PlatformResult result = client_->SendCommand( + args.get("name").get(), args.get("command").get(), + args.get("data"), reply_cb, &request_id); if (result) { ReportSuccess(out); diff --git a/src/mediacontroller/mediacontroller_server.cc b/src/mediacontroller/mediacontroller_server.cc index 26f6464..93e6d88 100644 --- a/src/mediacontroller/mediacontroller_server.cc +++ b/src/mediacontroller/mediacontroller_server.cc @@ -334,7 +334,6 @@ PlatformResult MediaControllerServer::CommandReply(const std::string& client_nam }; if (!data.is()) { - ScopeLogger("data is not null"); bundle = bundle_create(); const picojson::object& json = data.get(); PlatformResult result = common::JsonToBundle(json, bundle); diff --git a/src/tizen/js/ut/bundle_ut.js b/src/tizen/js/ut/bundle_ut.js index a35725f..f9c66d8 100644 --- a/src/tizen/js/ut/bundle_ut.js +++ b/src/tizen/js/ut/bundle_ut.js @@ -211,10 +211,10 @@ function TestToJson() { var result = (new tizen.Bundle(json)).toJSON(); assertEqual(json.key1, result.key1, 'key1 differs'); assertArrayEqual(json.key2, result.key2, 'key2 differs'); - assertEqual(true, result.key3 instanceof Uint8Array, 'key3 not Uint8Array'); + assertEqual(true, Array.isArray(result.key3), 'key3 not an array'); assertEqual(result.key4.length, json.key4.length, 'key4 length differs'); - assertEqual(true, result.key4[0] instanceof Uint8Array, 'key4[0] element not Uint8Array'); - assertEqual(true, result.key4[1] instanceof Uint8Array, 'key4[1] element not Uint8Array'); + assertEqual(true, Array.isArray(result.key4[0]), 'key4[0] element not an array'); + assertEqual(true, Array.isArray(result.key4[1]), 'key4[1] element not an array'); assertArrayEqual(json.key4[0], result.key4[0], 'key4[0] differs'); assertArrayEqual(json.key4[1], result.key4[1], 'key4[1] differs'); } diff --git a/src/tizen/tizen_api.js b/src/tizen/tizen_api.js index 2509791..5dfb159 100644 --- a/src/tizen/tizen_api.js +++ b/src/tizen/tizen_api.js @@ -570,16 +570,6 @@ exports.Bundle.prototype.forEach = function(callback) { exports.Bundle.prototype.toJSON = function() { var json = {}; - this.forEach(function(key, value) { - json[key] = value; - }); - return json; -}; - -// Conversion from Uint8Array to Array happens in toString instead of toJSON -// because toJSON method should preserve bundle values types. -exports.Bundle.prototype.toString = function() { - var json = {}; this.forEach(function(key, value, type) { if (type == BundleValueType.BYTES) { json[key] = [].slice.call(value); @@ -594,6 +584,10 @@ exports.Bundle.prototype.toString = function() { json[key] = value; } }); - return JSON.stringify(json); + return json; +}; + +exports.Bundle.prototype.toString = function() { + return JSON.stringify(this); }; -- 2.7.4