[tizen][mediacontroller] Change toJSON implementation to simplify serialization. 69/210769/2 accepted/tizen/unified/20190730.112003 submit/tizen/20190730.074817
authorMichal Michalski <m.michalski2@partner.samsung.com>
Wed, 24 Jul 2019 08:38:45 +0000 (10:38 +0200)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Wed, 24 Jul 2019 09:16:06 +0000 (11:16 +0200)
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 <m.michalski2@partner.samsung.com>
Change-Id: I89b11bc634b56b720dd8a5ecea8537ef1ce51b24

src/mediacontroller/mediacontroller_api.js
src/mediacontroller/mediacontroller_instance.cc
src/mediacontroller/mediacontroller_server.cc
src/tizen/js/ut/bundle_ut.js
src/tizen/tizen_api.js

index 7ad023c..4a78a2c 100755 (executable)
@@ -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
   };
index 2d0696f..2629d35 100644 (file)
@@ -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<std::string>();
-  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<std::string>(),
                                       args.get("requestId").get<std::string>(), json);
 
@@ -922,17 +915,9 @@ void MediaControllerInstance::MediaControllerServerInfoSendCommand(const picojso
     free(request_id);
   };
 
-  auto serialized_data = args.get("data").get<std::string>();
-  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<std::string>(),
-                           args.get("command").get<std::string>(), json, reply_cb, &request_id);
+  PlatformResult result = client_->SendCommand(
+      args.get("name").get<std::string>(), args.get("command").get<std::string>(),
+      args.get("data"), reply_cb, &request_id);
 
   if (result) {
     ReportSuccess(out);
index 26f6464..93e6d88 100644 (file)
@@ -334,7 +334,6 @@ PlatformResult MediaControllerServer::CommandReply(const std::string& client_nam
   };
 
   if (!data.is<picojson::null>()) {
-    ScopeLogger("data is not null");
     bundle = bundle_create();
     const picojson::object& json = data.get<picojson::object>();
     PlatformResult result = common::JsonToBundle(json, bundle);
index a35725f..f9c66d8 100644 (file)
@@ -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');
 }
index 2509791..5dfb159 100644 (file)
@@ -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);
 };