Convert SFSObject to js normal object?

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

User avatar
khanhdev
Posts: 30
Joined: 24 Mar 2017, 11:32

Convert SFSObject to js normal object?

Postby khanhdev » 20 Jun 2017, 09:39

In version 1.7.2, how to convert sfsobject to normal jsobject. I want it to be easier to see than when logging on the browser.
Sorry for my English :)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 20 Jun 2017, 09:49

Hi,
there's no need to convert an SFSObject. You can dump it's content to the console, via:

Code: Select all

trace(sfsobject.getDump())


This will show all the content, including nested SFSObjects/Arrays

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
khanhdev
Posts: 30
Joined: 24 Mar 2017, 11:32

Re: Convert SFSObject to js normal object?

Postby khanhdev » 20 Jun 2017, 10:19

Lapo wrote:Hi,
there's no need to convert an SFSObject. You can dump it's content to the console, via:

Code: Select all

trace(sfsobject.getDump())


This will show all the content, including nested SFSObjects/Arrays

cheers


thank Lapo :D
User avatar
marsoups
Posts: 167
Joined: 14 Apr 2008, 03:30

Re: Convert SFSObject to js normal object?

Postby marsoups » 03 Apr 2018, 22:01

There is one situation where this would be helpful to be able to dump to a JS object - and that is using a JS library such as angular JS, where the SFSArray and SFSObject type don't work well when looping and accessing properties.

I've tried to make a few functions to loop through on each object in an array, but I'm a little bit stuck on how I can tell what type of property and how to unravel it - i.e. if this is an SFSArray, we need to loop through it to access each SFSObject in it, if it is an array of any type, then unravel it accordingly. My question is : is it possible to access the type of element in a get ?

Thanks!
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 04 Apr 2018, 08:50

marsoups wrote:There is one situation where this would be helpful to be able to dump to a JS object - and that is using a JS library such as angular JS, where the SFSArray and SFSObject type don't work well when looping and accessing properties.

Hi,
what do you mean by "dump to a JS object" exactly?
Converting the SFSObject into a regular JS Object?

I think it would be possible but it also sounds like a waste of resources, especially because SFSObject/Arrays are usually nested and thus you would need to recursively traverse the whole structure to recreate it in another format.
I am not sure what would be the advantage of that extra work?

In general I would not recommend it.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
marsoups
Posts: 167
Joined: 14 Apr 2008, 03:30

Re: Convert SFSObject to js normal object?

Postby marsoups » 04 Apr 2018, 16:23

Hey Lapo,

In Angular JS, you can for example produce a table by simply pointing a row to a JS array. Angular JS will handle the display part of updating the content. So in this situation it's efficient to produce native JS objects.

Here is a bit of code that I wrote that has helped me with this tasked (tested only minimally):

Code: Select all

var convertSFSArrayToArray = function (SFSArray) {
            var newArray = [];
            for(var i = 0 ; i < SFSArray.size(); i++) {
                var obj = convertSFSObjectToObject(SFSArray.get(i));
                newArray.push(obj);
            }
            return newArray;
        }
        var convertSFSObjectToObject = function (SFSObject) {
            var obj = {}
            var keyArray = SFSObject.getKeysArray();
            for(var j = 0; j < keyArray.length; j++) {
                if(typeof SFSObject.get(keyArray[j]) == "object") {
                    obj[keyArray[j]] = convertSFSArrayToArray(SFSObject.get(keyArray[j]));
                } else {
                    obj[keyArray[j]] = SFSObject.get(keyArray[j]);
                }
            }
            return obj;
        }
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 05 Apr 2018, 07:49

The code seems correct.
If you want to improve the type detection instead of using "typeof" you could use "instanceof"

Example:

Code: Select all

if (SFSObject.get(keyArray[j]) instanceof SFS2X.SFSObject)
{
    obj[keyArray[j]] = convertSFSArrayToArray(SFSObject.get(keyArray[j]));
} else {
    obj[keyArray[j]] = SFSObject.get(keyArray[j]);
}


hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
kiran.sinha
Posts: 4
Joined: 08 Oct 2018, 12:40

Re: Convert SFSObject to js normal object?

Postby kiran.sinha » 08 Oct 2018, 12:55

Hi All,

I am getting extension response in nested with sfsObject and sfsArray and not able to parse with given method, however able to parse another extension response, thanks for posting that method here, but not able to parse this one, It would be great to provide a solution here.
I am pasting a dump of that response here, please help.

Thanks In Advance
Kiran Sinha

Code: Select all

(sfs_object) MttInfo:
      (sfs_array) $F:
         (sfs_object) 0:
            (utf_string) V: Oct 5, 2018 17:00:00
            (utf_string) N: rgst
            
         (sfs_object) 1:
            (utf_string) V: Oct 5, 2018 17:10:00
            (utf_string) N: rget
            
         (sfs_object) 2:
            (utf_string) V: Oct 5, 2018 17:10:30
            (utf_string) N: mtst
            
         (sfs_object) 3:
            (int) V: 3
            (utf_string) N: rgp
            
         (sfs_object) 4:
            (int) V: 0
            (utf_string) N: mxp
            
         (sfs_object) 5:
            (int) V: 0
            (utf_string) N: mip
            
         (sfs_object) 6:
            (sfs_object) V:
               (sfs_array) $F:
                  (sfs_object) 0:
                     (sfs_array) V:
                        (sfs_object) 0:
                           (sfs_array) $F:
                              (sfs_object) 0:
                                 (utf_string) V: dummy
                                 (utf_string) N: type
                                 
                              (sfs_object) 1:
                                 (int) V: 10
                                 (utf_string) N: amt
                                 
                              (sfs_object) 2:
                                 (int) V: 50
                                 (utf_string) N: initAmt
                                 
                           (utf_string) $C: mtt.EntryChip
                     (utf_string) N: entryChips               
               (utf_string) $C: mtt.EntryChipInfo
            (utf_string) N: entryChipInfo
         (sfs_object) 7:
            (bool) V: false
            (utf_string) N: disunreg
         
         (sfs_object) 8:
            (null) V: null
            (utf_string) N: ltyst
            
         (sfs_object) 9:
            (null) V: null
            (utf_string) N: ltyet
            
         (sfs_object) 10:
            (bool) V: true
            (utf_string) N: isLateReg
            
         (sfs_object) 11:
            (int) V: 15
            (utf_string) N: lateregtime
            
         (sfs_object) 12:
            (int) V: 3
            (utf_string) N: minLateReg
            
         (sfs_object) 13:
            (bool) V: false
            (utf_string) N: isPrizeStruc
            
         (sfs_object) 14:
            (sfs_object) V:
               (sfs_array) $F:
                  (sfs_object) 0:
                     (sfs_array) V:
                        (sfs_object) 0:
                           (sfs_array) $F:
                              (sfs_object) 0:
                                 (utf_string) V: dummy
                                 (utf_string) N: type
                              (sfs_object) 1:
                                 (int) V: 10
                                 (utf_string) N: amt
                              (sfs_object) 2:
                                 (int) V: 50
                                 (utf_string) N: initAmt
                              
                           (utf_string) $C: mtt.EntryChip
                     
                     (utf_string) N: entryChips
               
               (utf_string) $C: mtt.EntryChipInfo
               
            (utf_string) N: rebuyChipInfo
            
         (sfs_object) 15:
            (bool) V: true
            (utf_string) N: isRebuy
            
         (sfs_object) 16:
            (bool) V: false
            (utf_string) N: finalAddon
            
         (sfs_object) 17:
            (int) V: -1
            (utf_string) N: maxRebuys
            
         (sfs_object) 18:
            (int) V: -1
            (utf_string) N: maxAddons
            
         (sfs_object) 19:
            (int) V: 0
            (utf_string) N: rebuyTime
            
         (sfs_object) 20:
            (int) V: 0
            (utf_string) N: addonTime
            
         (sfs_object) 21:
            (int) V: 0
            (utf_string) N: addonThreshold
            
         (sfs_object) 22:
            (int) V: 0
            (utf_string) N: bigBlindMultiple
            
         (sfs_object) 23:
            (int) V: 0
            (utf_string) N: finalAddonAmt
            
         (sfs_object) 24:
            (utf_string) V: HandsTourney
            (utf_string) N: tourneyFormat
            
         (sfs_object) 25:
            (int) V: 2
            (utf_string) N: tourneyLevel
            
         (sfs_object) 26:
            (utf_string) V: 1,3,2.0,1
            2,5,4.0,2
            3,8,6.0,3
            
            (utf_string) N: tourneyLevelInfo
            
         (sfs_object) 27:
            (int) V: 5
            (utf_string) N: noOfRebuy
            
         (sfs_object) 28:
            (int) V: 1
            (utf_string) N: rebuyUpToLevel
            
         (sfs_object) 29:
            (int) V: 50
            (utf_string) N: finalRebuyChips
            
         (sfs_object) 30:
            (bool) V: false
            (utf_string) N: finalReBuyAllowed
            
         (sfs_object) 31:
            (int) V: 350
            (utf_string) N: rebuyChips
            
         (sfs_object) 32:
            (int) V: 340
            (utf_string) N: reBuyThreshold
            
         (sfs_object) 33:
            (int) V: 12
            (utf_string) N: maxReBuySec
            
         (sfs_object) 34:
            (int) V: 35
            (utf_string) N: maxFinalReBuySec
            
      (utf_string) $C: mtt.MttInfo
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 08 Oct 2018, 14:14

Hi,
what do you mean by "not able to parse"?
The data dump you have posted shows the structure of the nested object, showing at which level each object is contained.

In any case the data dump here look like it was created with a SFSObject.putClass() from the server side. Therefore you should not convert it manually, instead you just need to extract the data according to the data structure you've built in your Extension.

Hope it helps.
Lapo

--

gotoAndPlay()

...addicted to flash games
kiran.sinha
Posts: 4
Joined: 08 Oct 2018, 12:40

Re: Convert SFSObject to js normal object?

Postby kiran.sinha » 09 Oct 2018, 10:25

Hi Lapo,

Thanks for reply.
regarding "not able to parse" means, everytime when I tried to parse this data from iteration it is throwing error for nested data, therefore i am not able to parse,I was able to parse straight data objects.
"In extension I have a POJO class, which is being sent in SFSObject.putclass(). Can you please give me an example that how can I create same structure in javascript and map it with POJO using new library compatible with SFS version 2.13"

Thanks
Kiran
Last edited by kiran.sinha on 09 Oct 2018, 11:47, edited 1 time in total.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 09 Oct 2018, 10:28

Hi Kiran,
I am still not entirely clear on this.
Can you show me an example of your parsing code that causes an error? And can you also post the full transcript of such error?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
kiran.sinha
Posts: 4
Joined: 08 Oct 2018, 12:40

Re: Convert SFSObject to js normal object?

Postby kiran.sinha » 09 Oct 2018, 12:43

Hi Lapo,
I am using below methods to parse the data (already posted in this forum thread)

Code: Select all

convertSFSArrayToArray :function (SFSArray) {
        var newArray = [];
        for(var i = 0 ; i < SFSArray.size(); i++) {
            var obj = this.convertSFSObjectToObject(SFSArray.get(i));
            newArray.push(obj);
        }
        return newArray;
    },
    convertSFSObjectToObject: function (SFSObject) {
        var obj = {}
        var keyArray = SFSObject.getKeysArray();
        for(var j = 0; j < keyArray.length; j++) {
            if (SFSObject.get(keyArray[j]) instanceof SFS2X.SFSObject)
            {
                obj[keyArray[j]] = this.convertSFSArrayToArray(SFSObject.get(keyArray[j]));
            } else {
                obj[keyArray[j]] = SFSObject.get(keyArray[j]);
            }
           
        }
        return obj;
       
    },


Below is the error we are getting

Code: Select all

SFS2X_API_JS.js:6 Uncaught TypeError: Cannot read property 'getKeysArray' of null
    at Object.convertSFSObjectToObject (globalVar.js?M.02:87)
    at Object.convertSFSArrayToArray (globalVar.js?M.02:80)
    at Object.convertSFSObjectToObject (globalVar.js?M.02:91)
    at onExtensionResponse (utils.js?M.02:758)
    at t.value (SFS2X_API_JS.js:6)
    at e.value (SFS2X_API_JS.js:7)
    at t.value (SFS2X_API_JS.js:7)
    at t.value (SFS2X_API_JS.js:6)


it is coming due to this piece of data

Code: Select all

(sfs_object) 6:
            (sfs_object) V:
               (sfs_array) $F:
                  (sfs_object) 0:
                     (sfs_array) V:
                        (sfs_object) 0:
                           (sfs_array) $F:
                              (sfs_object) 0:
                                 (utf_string) V: dummy
                                 (utf_string) N: type
                                 
                              (sfs_object) 1:
                                 (int) V: 10
                                 (utf_string) N: amt
                                 
                              (sfs_object) 2:
                                 (int) V: 50
                                 (utf_string) N: initAmt
                                 
                           (utf_string) $C: mtt.EntryChip
                     (utf_string) N: entryChips               
               (utf_string) $C: mtt.EntryChipInfo
            (utf_string) N: entryChipInfo
kiran.sinha
Posts: 4
Joined: 08 Oct 2018, 12:40

Re: Convert SFSObject to js normal object?

Postby kiran.sinha » 09 Oct 2018, 12:48

Hi Lapo

"In extension we have a POJO class, which is being sent in SFSObject.putclass(). Can you please give me an example that how can I create same structure in javascript and map it with POJO using new library compatible with SFS version 2.13"

Thanks
Kiran
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 09 Oct 2018, 13:23

Ok, now it's clearer, you're working with a Javascript client.
Unfortunately there is no support in JS for putClass/getClass as Java objects don't translate to Javascript due to fundamental differences in the two languages. (such as strong types and inheritance differences)

In this case you should replace the putClass from server side with an SFSObject that contains the properties of the class you want to send.

Hope it's clear.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: No registered users and 12 guests