Problem with empty SFSObject

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

pukkun
Posts: 6
Joined: 22 Aug 2019, 03:21

Problem with empty SFSObject

Postby pukkun » 23 Aug 2019, 04:03

When i send the empty object its become null value.
Example:
- In server side:

Code: Select all

ISFSObject response = SFSObject.newInstance();
         response.putSFSObject("test", SFSObject.newInstance());


But, in client side:

Code: Select all

 ISFSObject  response = data.GetSFSObject("test");


response is null, must be an empty sfsobject

Similar, function SFSObject.NewFromJsonData convert empty object to null. Example {"test":{}} mean SFSObject <test> is null
I'm using Server & Client version 2.13
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem with empty SFSObject

Postby Lapo » 23 Aug 2019, 07:09

Hi,
what you're doing is incorrect.

This line in your client side code:
ISFSObject response = data.GetSFSObject("test");

is trying to extract a property called "test" from the SFSObject that you sent, which is empty. Hence the error.

In other words the Event.Params field that you get on client side IS the SFSObject you sent. In order for you to be able to access a "test" property the SFSObject you sent should contain an object with that key. But it doesn't because it's empty.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
pukkun
Posts: 6
Joined: 22 Aug 2019, 03:21

Re: Problem with empty SFSObject

Postby pukkun » 23 Aug 2019, 07:31

Lapo wrote:Hi,
what you're doing is incorrect.

This line in your client side code:
ISFSObject response = data.GetSFSObject("test");

is trying to extract a property called "test" from the SFSObject that you sent, which is empty. Hence the error.

In other words the Event.Params field that you get on client side IS the SFSObject you sent. In order for you to be able to access a "test" property the SFSObject you sent should contain an object with that key. But it doesn't because it's empty.

Hope it helps


Thanks for your reply!

an empty object different null object. function SFSObject.NewFromJsonData convert empty object to null. Example {"test":{}} mean SFSObject <test> is null

ISFSObject a = SFSObject.NewFromJsonData(" {"test":{}} ");

When you use a.getSFSObject("test"). it return null value. it's wrong logic.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem with empty SFSObject

Postby Lapo » 23 Aug 2019, 09:10

Please show me the full code example that reproduces this issue, not just a snippet.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
pukkun
Posts: 6
Joined: 22 Aug 2019, 03:21

Re: Problem with empty SFSObject

Postby pukkun » 23 Aug 2019, 09:51

Lapo wrote:Please show me the full code example that reproduces this issue, not just a snippet.

Thanks


You can test with this json:

Code: Select all

{"1":{"pet_id":2,"id":2,"level":41,"star":0,"enhance":0,"evolve":2,"skin":1,"bonus_equip":{"hp":0,"damage":0,"critical":0,"armor":0,"armor_penetration":0,"resist_branch":{}}},"2":{"pet_id":12,"id":12,"level":1,"star":0,"enhance":0,"evolve":0,"skin":1,"bonus_equip":{"hp":0,"damage":0,"critical":0,"armor":0,"armor_penetration":0,"resist_branch":{}}},"3":{"pet_id":3004,"id":3004,"level":50,"star":2,"enhance":0,"evolve":2,"skin":1,"bonus_equip":{"hp":0,"damage":0,"critical":0,"armor":0,"armor_penetration":0,"resist_branch":{}}},"4":{"pet_id":3003,"id":3003,"level":50,"star":2,"enhance":0,"evolve":2,"skin":1,"bonus_equip":{"hp":0,"damage":0,"critical":0,"armor":0,"armor_penetration":0,"resist_branch":{}}},"main":{"pet_id":3002,"id":3002,"level":40,"star":0,"enhance":0,"evolve":1,"skin":1,"bonus_equip":{"hp":0,"damage":0,"critical":0,"armor":0,"armor_penetration":0,"resist_branch":{}}}}


Code: Select all

 ISFSObject dataLineUp = SFSObject.NewFromJsonData(TestTeam);
ISFSObject main = dataLineUp.GetSFSObject("main");
ISFSObject bonus = main.GetSFSObject("bonus_equip");
ISFSObject resist = bonus.GetSFSObject("resist_branch");
print("main " + main);
print("bonus " + bonus);
print("resist " + resist);


result:
main Sfs2X.Entities.Data.SFSObjectLite
bonus Sfs2X.Entities.Data.SFSObjectLite
resist
pukkun
Posts: 6
Joined: 22 Aug 2019, 03:21

Re: Problem with empty SFSObject

Postby pukkun » 23 Aug 2019, 10:00

Other case:

Code: Select all

[{"list_add_spec_piece":[[],[],[]],"list_add_piece":[[{"q":1,"r":0,"c":1,"t":4},{"q":1,"r":1,"c":1,"t":3},{"q":1,"r":2,"c":1,"t":1}],[{"q":1,"r":0,"c":1,"t":3},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":2,"c":1,"t":5}],[{"q":1,"r":0,"c":0,"t":1},{"q":1,"r":0,"c":1,"t":2},{"q":1,"r":0,"c":2,"t":5},{"q":1,"r":1,"c":0,"t":1},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":1,"c":2,"t":4},{"q":1,"r":2,"c":0,"t":2},{"q":1,"r":2,"c":1,"t":5},{"q":1,"r":2,"c":2,"t":5}]],"list_remove_piece":[{"array_count_piece_in_step":[0,0,0,0,0,3],"list_remove_piece_step":[{"q":1,"r":3,"c":1,"t":5},{"q":1,"r":4,"c":1,"t":5},{"q":1,"r":5,"c":1,"t":5}],"data_per_step":{}},{"array_count_piece_in_step":[0,3,0,0,0,0],"list_remove_piece_step":[{"q":1,"r":2,"c":1,"t":1},{"q":1,"r":3,"c":1,"t":1},{"q":1,"r":4,"c":1,"t":1}],"data_per_step":{}},{"array_count_piece_in_step":[0,0,0,3,3,3],"list_remove_piece_step":[{"q":1,"r":1,"c":0,"t":5},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":1,"c":2,"t":5},{"q":1,"r":3,"c":0,"t":4},{"q":1,"r":3,"c":1,"t":4},{"q":1,"r":3,"c":2,"t":4},{"q":1,"r":4,"c":0,"t":3},{"q":1,"r":4,"c":1,"t":3},{"q":1,"r":4,"c":2,"t":3}],"data_per_step":{}}],"list_piece_in_turn":"0_3_0_3_3_6"}]


this is json after I log by use SFSObject.toJson(), but after send client:

Code: Select all

client side:
            ISFSArray listPieceRemove = response.GetSFSArray(ServerKey.LIST_REMOVE_PIECE);
            ISFSObject dataRemove = listPieceRemove.RemoveElementAt(0) as ISFSObject;
            ISFSArray listRemoveStep = dataRemove.GetSFSArray(ServerKey.LIST_REMOVE_PIECE_STEP);
            ISFSObject dataPerStep = dataRemove.GetSFSObject(ServerKey.DATA_PER_STEP);
            if (dataPerStep == null) dataPerStep = SFSObject.NewInstance();


dataPerStep can be null so I avoid errors by checking
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem with empty SFSObject

Postby Lapo » 23 Aug 2019, 13:03

Thanks.
In your original post you showed an example that isn't using JSON.

I can't reproduce your problem where JSON is not used. This server side code:

Code: Select all

ISFSObject response = new SFSObject();
response.putSFSObject("test", new SFSObject());

send("EmptyObj", response, sender);


works without issues with this client side C# code:

Code: Select all

private void OnExtensionRes(BaseEvent evt)
{
    ISFSObject res = evt.Params["params"] as SFSObject;
    Console.WriteLine("Test object ==> " + res.GetSFSObject("test"));
}

The object exists as SFSObject and it's not null.

As regards the example using JSON:

Code: Select all

ISFSObject a = SFSObject.NewFromJsonData(" {"test":{}} ");

I was able to reproduce the problem although this only happens in C#, and not in other languages supporting JSON.
We'll have to investigate.

As a side note I have to say that support for JSON was only introduced to support the old non-binary websocket protocol, which is now surpassed and therefore JSON support still exists for retro-compatibility but it will be dropped eventually.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
pukkun
Posts: 6
Joined: 22 Aug 2019, 03:21

Re: Problem with empty SFSObject

Postby pukkun » 23 Aug 2019, 15:21

Thanks.
Sorry that example maybe wrong. Reality, my SFSObject nested more SFSObject.
This is a SFSArray (a part of my SFSObject) after convert to JSON

Code: Select all

[{"list_add_spec_piece":[[],[],[]],"list_add_piece":[[{"q":1,"r":0,"c":1,"t":4},{"q":1,"r":1,"c":1,"t":3},{"q":1,"r":2,"c":1,"t":1}],[{"q":1,"r":0,"c":1,"t":3},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":2,"c":1,"t":5}],[{"q":1,"r":0,"c":0,"t":1},{"q":1,"r":0,"c":1,"t":2},{"q":1,"r":0,"c":2,"t":5},{"q":1,"r":1,"c":0,"t":1},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":1,"c":2,"t":4},{"q":1,"r":2,"c":0,"t":2},{"q":1,"r":2,"c":1,"t":5},{"q":1,"r":2,"c":2,"t":5}]],"list_remove_piece":[{"array_count_piece_in_step":[0,0,0,0,0,3],"list_remove_piece_step":[{"q":1,"r":3,"c":1,"t":5},{"q":1,"r":4,"c":1,"t":5},{"q":1,"r":5,"c":1,"t":5}],"data_per_step":{}},{"array_count_piece_in_step":[0,3,0,0,0,0],"list_remove_piece_step":[{"q":1,"r":2,"c":1,"t":1},{"q":1,"r":3,"c":1,"t":1},{"q":1,"r":4,"c":1,"t":1}],"data_per_step":{}},{"array_count_piece_in_step":[0,0,0,3,3,3],"list_remove_piece_step":[{"q":1,"r":1,"c":0,"t":5},{"q":1,"r":1,"c":1,"t":5},{"q":1,"r":1,"c":2,"t":5},{"q":1,"r":3,"c":0,"t":4},{"q":1,"r":3,"c":1,"t":4},{"q":1,"r":3,"c":2,"t":4},{"q":1,"r":4,"c":0,"t":3},{"q":1,"r":4,"c":1,"t":3},{"q":1,"r":4,"c":2,"t":3}],"data_per_step":{}}],"list_piece_in_turn":"0_3_0_3_3_6"}]


data_per_step is null.

Sometime, data_per_step value null make function SFSObject.toJson (JAVA in my case) be error

As regards the example using JSON:
Code: Select all

ISFSObject a = SFSObject.NewFromJsonData(" {"test":{}} ");

I was able to reproduce the problem although this only happens in C#, and not in other languages supporting JSON.


Same error in server side (JAVA)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem with empty SFSObject

Postby Lapo » 23 Aug 2019, 17:15

Same error in server side (JAVA)

No, I can't reproduce this.

If you run this code in server side Java:

Code: Select all

ISFSObject obj = SFSObject.newFromJsonData("{'test':{}}")
System.out.println(obj.getSFSObject("test"));


You get this output:

Code: Select all

[SFSObject, size: 0]

Which is correct. It's an SFSObject with zero elements stored in it.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Problem with empty SFSObject

Postby Bax » 26 Aug 2019, 09:51

We fixed the issue with empty JSON objects.
If you send us an email, we will provide the download link to get the updated API.
Thank you.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 67 guests