question

ccastro avatar image
ccastro asked

SerializationException: Invalid JSON string using the HelloWorld on cloudscript

Hi I'm following this guide and working on unity 5.3.4f1

https://api.playfab.com/docs/using-cloud-script

When I call the Hello World example i Got

SerializationException: Invalid JSON string PlayFab.Json.PlayFabSimpleJson.DeserializeObject (System.String json) (at Assets/PlayFabSdk/Shared/Internal/SimpleJson.cs:568) PlayFab.Json.PlayFabSimpleJson.DeserializeObject (System.String json, System.Type type, IJsonSerializerStrategy jsonSerializerStrategy) (at Assets/PlayFabSdk/Shared/Internal/SimpleJson.cs:600) PlayFab.Json.PlayFabSimpleJson.DeserializeObject[HttpResponseObject] (System.String json, IJsonSerializerStrategy jsonSerializerStrategy) (at Assets/PlayFabSdk/Shared/Internal/SimpleJson.cs:613) PlayFab.Json.SimpleJsonInstance.DeserializeObject[HttpResponseObject] (System.String json, System.Object jsonSerializerStrategy) (at Assets/PlayFabSdk/Shared/Internal/ISerializer.cs:66) PlayFab.Json.JsonWrapper.DeserializeObject[HttpResponseObject] (System.String json, System.Object jsonSerializerStrategy) (at Assets/PlayFabSdk/Shared/Internal/ISerializer.cs:38) PlayFab.Internal.PlayFabWww+<MakeApiCall>c__AnonStorey5.<>m__5 (System.String response) (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabWWW.cs:75) UnityEngine.Debug:LogException(Exception)

I aprreciate any suggestions. Thank you

CloudScript
1 comment
10 |1200

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

marcowilliamspf avatar image marcowilliamspf commented ·

Hi @ccastro, we need more information about how you are deserializing this object. If you are following the tutorial then you should have had code that looked like this:

JsonObject jsonResult = (JsonObject)result.FunctionResult;


and your cloudscript would look like this:

// Cloud Script (JavaScript)
handlers.helloWorld = function (args)
{
  var message = "Hello " + currentPlayerId + "!";
  return { messageValue: message };
}

If you could share your code then we can see what you are doing different.

0 Likes 0 ·
1807605288 avatar image
1807605288 answered

I have discovered the issue.

The default Cloud Script that we put on new titles no longer matches our example documentation.

That will have to be fixed on our end.

For you, you can make that work by changing this:

FunctionParameter=new{ name ="YOUR NAME"},
To this:
FunctionParameter=new{ inputValue="YOUR NAME"},
1 comment
10 |1200

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

ccastro avatar image ccastro commented ·

Thank you !!!

0 Likes 0 ·
ccastro avatar image
ccastro answered

Hi thank you for your answer.

Here is the code.

private static void StartCloudHelloWorld()
	{
		ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
		{
			FunctionName = "helloWorld", 
			FunctionParameter = new { name = "YOUR NAME" }, 
			GeneratePlayStreamEvent = true, 
		};
		Debug.Log ("Request ---> " + request.ToString ());
		PlayFabClientAPI.ExecuteCloudScript(request, OnCloudHelloWorld, OnErrorShared, null);
	}

	private static void OnCloudHelloWorld(ExecuteCloudScriptResult result)
	{
		Debug.Log ("Result ----> ");
		Debug.Log(PlayFab.Json.JsonWrapper.SerializeObject(result));
		Debug.Log(PlayFab.Json.JsonWrapper.SerializeObject(result.FunctionResult));

		JsonObject jsonResult = (JsonObject)result.FunctionResult;
		object messageValue;
		jsonResult.TryGetValue("messageValue", out messageValue); // note how "messageValue" directly corresponds to the JSON values set in Cloud Script

		Debug.Log((string)messageValue);
	}

	private static void OnErrorShared(PlayFab.PlayFabError error)
	{
		Debug.Log(PlayFab.Json.JsonWrapper.SerializeObject(error));
		Debug.Log(PlayFab.Json.JsonWrapper.SerializeObject(error.Error));
	}
}

and the cloud script its just the auto generated code
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.

1807605288 avatar image 1807605288 ♦ commented ·

If you are using our helloWorld function, then request.FunctionParameter is unused and unnecessary.

What is your titleId?

[UPDATED]

This is the code we use for our internal testing:

https://github.com/PlayFab/UnitySDK/blob/master/Testing/Tests/Client/ClientApiTests.cs#L417

And it looks like your code is similar enough that it should be working.

Have you set your titleId properly here?:
{proj}/assets/PlayFabSdk/Shared/Public/Resources/PlayFabSharedSettings (Scriptable Object)

Which version of our sdk are you using? The latest is 2.10.161003. In your case it shouldn't matter, but in general we always suggest people use the latest version.

0 Likes 0 ·
ccastro avatar image ccastro 1807605288 ♦ commented ·

PlayFabSandbox

Title ID: 2E5

0 Likes 0 ·

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.