question

Dean Loades avatar image
Dean Loades asked

ExecuteCloudScript FunctionResult frustration

Just wanted to give a bit of feedback on this.

Maybe it's just me but I just can't seem to get along with your version of JSON, it seems to be contriving to thwart me at every turn.

I am getting results from ExecuteCloudScript, and it appears the FunctionResult has already been put into a SimpleJSON object. I would MUCH prefer this was just left a string that I could then parse with a JSON of my choice.

Here are the reasons why I am hating it. Please let me know if I have overlooked something here.

I can't just deserialise it to a particular class, since it is not a string anymore. It is some kind of dictionary. I'd have to stringify it again before I could use DeserializeObject<T>. Seems like a waste.

It is really hard to use by comparison with my usual JSON. There is no TryGetValue that returns a JSON object. You have to pass in an object and then cast it.

There is no TryGetValue that returns a long. You have to convert it yourself.

CloudScriptTitle Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

[Edit below, to clarify the specifics of the SeralizeObject call]

Correct - the FunctionResult in a JSON Object, so generally speaking, you can convert it to a string via Seralize, SeralizeObject, or Stringify, depending on your language and JSON library. If you're using our Unity SDK, you can do this via the PlayFab.Json.JsonWrapper.SerializeObject(obj, (obj, PlayFabUtil.ApiSerializerStrategy); function.

We cannot change the behavior of FunctionResult in the SDK, as that would be a breaking change - something we're committed to not having. It is possible that we could add another return parameter which returns the result in string format, however. If you feel that's a definite need for the API method, I can open a backlog item for this.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dean Loades avatar image Dean Loades commented ·

I'm not sure you are fully understanding my serialisation issue.

I get how to do JSONObject <-> string.

But I really would prefer to deserialize straight to a class, without the intermediate JSONObject. Since it is already returned as JSONObject, I have to serialise it and then deserialise again. That's after its already been deserialized in your sdk. 3 times. Seems like a huge waste of processing. That's why i'd prefer a string to be returned.

I guess it's up to you guys whether its worth adding to the API. Its definitely not going to stop me getting stuff done though.

0 Likes 0 ·
brendan avatar image brendan Dean Loades commented ·

No, I do understand - it's just that changing the value returned in FunctionResult would be a breaking change, so that's not an option. I've added a backlog item to return an additional parameter which is the string version of the result. I'd recommend adding this request to the Feature Requests forum, so that others can vote it up and help to prioritize it.

1 Like 1 ·
Dean Loades avatar image
Dean Loades answered

@Brendan It's definitely not a huge issue.

I can stringify it, then parse it to json again, but that seems like a waste, since the api has already gone to the effort of parsing it.

What would work well in my case, is if there was a function to turn the existing json object directly into a particular class. ie

T DeserializeObject<T>(object json);

Don't if that is possible or not.

Failing that. I would save a huge amount of hassle if the Json had TryGetValue overloads for different types. Currently I have to TryGetValue to an object, check it, then try and cast / convert it to what I want and then check that. All that error checking should be included in a TryGetValue function.

Anyway that's my 2c. Keep up the great work you guys!

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt avatar image Matt commented ·

Hey folks,

I'm just wondering if anything's changed or updated with this. Currently I use this code and would love to update it if necessary, the webpages seem to show deprecated calls.

LevelReturnData data = PlayFab.Json.JsonWrapper.DeserializeObject<LevelReturnData>( PlayFab.Json.JsonWrapper.SerializeObject( result.FunctionResult ) );
0 Likes 0 ·
brendan avatar image brendan Matt commented ·

The FunctionResult is, itself, a JSON object so it will be necessary to perform this type of cast for non-trivial classes - have a look at our samples for some examples of this: https://github.com/PlayFab/PlayFab-Samples.

However, we should always be cleaning up old references to deprecated calls. What calls are you referring to, and what pages on the site refer to them?

0 Likes 0 ·
Matt avatar image Matt brendan commented ·

Thanks Brendan, that's not a problem at all, it works great.

So the tutorial of CloudScript ( https://api.playfab.com/docs/using-cloud-script ) has:

Debug.Log(PlayFab.SimpleJson.SerializeObject(result));
Debug.Log(PlayFab.SimpleJson.SerializeObject(result.FunctionResult));

I have my own class with 2 int's for my currencies. I wanted to deserialize the FunctionResult into that but intellisense tells me PlayFab.SimpleJson is deprecated. It also seems PlayFab.Json.SimpleJson points to the same framework. I ended up using PlayFab.Json.JsonWrapper, which works great, but it wasnt in the tutorial at all and maybe should be?

This is just the perspective of someone who's not quite sure if he's doing things right...

0 Likes 0 ·
Show more comments
Matt avatar image
Matt answered

Hey folks,

I currently have a project that uses the legacy cloudscript system and it has code in the client like this:

PlayerLoginResult loginResult = PlayFabSimpleJson.DeserializeObject<PlayerLoginResult>( ( string )result.FunctionResult );

I also have another project I've started to develop but it throws an error trying to cast the result.FunctionResult to a string:

CompleteLogin_Result loginResult = PlayFabSimpleJson.DeserializeObject<CompleteLogin_Result>( ( string )result.FunctionResult ) );

I have no idea why I'm getting an error in my new project and not the old since it shouldn't be any different from the way it used to work. Is there something inherently different with how Azure Functions packs the result...? That doesn't even make sense...

Thanks,

-Matt

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.