Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26756] Android: Object details should be printed instead of "[object Object]" (parity)

GitHub Issuen/a
TypeImprovement
PriorityNone
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsandroid, ios, log, parity
ReporterHans Knöchel
AssigneeJan Vennemann
Created2019-01-21T06:15:50.000+0000
Updated2020-01-08T18:31:04.000+0000

Description

We noticed that logs between iOS and Android differ. On iOS, an object is printed with it's keys, so it can be inspected. On Android, it only prints \[object Object\], requiring to manually stringify it. Unifying it would be a quick but useful improvement to the SDK.

Comments

  1. Joshua Quick 2019-01-23

    When I tested the below, the behavior between Android and iOS were the same.
       var window = Ti.UI.createWindow();
       window.addEventListener("open", function(e) {
       	Ti.API.info("@@@ e.toString(): " + e);
       	Ti.API.info("@@@ JSON.stringify(e): " + JSON.stringify(e));
       });
       window.open();
       
    When concatenating a Titanium object to a JavaScript string, both Android and iOS return "[object Object". And JSON.stringify() correctly returns a JSON of all of the object's enumerable properties on both Android and iOS. Can you give me a code snippet please? I'm curious where you're seeing the difference. Thanks.
  2. Hans Knöchel 2019-01-23

    In our case, we very often log plain objects, e.g. console.log({ hello: 'world' }); On iOS, it will be displayed "kind" of stringified - at least a bit formatted (which is done by the internal iOS logging of the underlaying NSDictionary object). On Android, it does not attempt to show any key/values but onky the above [object Object]. I hope that can reproduce it. But still no blocker, all okay!
  3. Joshua Quick 2019-01-24

    Okay. I've confirmed that both Android and Windows return \[object Object\] when doing the below.
       var table = {
       	myBoolean: true,
       	myNumber: 123.456,
       	myString: "Hello World",
       };
       Ti.API.info("@@@ table: " + table);
       
    Interesting. iOS and Windows both use JavaScriptCore. So, I'm guessing we have written something custom on the iOS side to JSON stringify a dictionary. It's probably not a built-in JavaScriptCore things since the Windows results are the same as Android.
  4. Joshua Quick 2019-01-24

    So, the below works on Titanium for Windows. It also works in node.js too. Not sure if it's a wise idea or not, but here it is.
       Object.prototype.toString = function() {
       	return JSON.stringify(this);
       };
       
    Unfortunately, the above won't work on Android. Not sure why. But in the end, the solution is to JSON.stringify() the dictionary.
  5. Gary Mathews 2019-01-24

    [~jquick] That works on Android, I put it in the ti.main.js
  6. Joshua Quick 2019-01-24

    Oh interesting. It turns out the JS minify prevents the below from working, which is used by default when doing a "development" or "production" build. When you build for Android emulator (ie: "test" build), a JS minify does not happen and the below solutions works fine.
       Object.prototype.toString = function() {
       	return JSON.stringify(this);
       };
       var table = {
       	myBoolean: true,
       	myNumber: 123.456,
       	myString: "Hello World",
       };
       Ti.API.info("@@@ table: " + table);
       
    So, I got the above to work for a device/development build via the following command line...
       appc run -p android -T device -C "<AndroidDeviceId>" --skip-js-minify
       

JSON Source