question

Matt avatar image
Matt asked

Deserializing Json object in Azure is failing.

I feel like an idiot, I know I'm doing something stupid, but I need someone to point it out for me. As the title suggests, my azure function is failing specifically on an attempt to deserialize the argument I pass in so I can use it.

// this is the request that is sent from client to cloudscript
public class CompleteLogin_Request
{
    public int ClientVersion = -1;
    public int Language = -1;
}

// this is a portion of the call from client to cloud
{
	...
	PlayFabCloudScriptAPI.ExecuteFunction( new ExecuteFunctionRequest()
	{
		Entity = new PlayFab.CloudScriptModels.EntityKey()
		{
			Id = PlayFabSettings.staticPlayer.EntityId,
			Type = PlayFabSettings.staticPlayer.EntityType
		},
		FunctionName = "CompleteLogin",
		FunctionParameter = new Dictionary<string, object>() { { "Request", PlayFabSimpleJson.SerializeObject( loginRequest ) } },
		GeneratePlayStreamEvent = true
	},
	...
}

// this is my azure function with the same class definition for the request
{
	FunctionExecutionContext<dynamic> functionContext = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            string currentPlayerId = functionContext.CallerEntityProfile.Lineage.MasterPlayerAccountId;
            dynamic args = functionContext.FunctionArgument;

	CompleteLogin_Result clientReturn = new CompleteLogin_Result() { ServerTime = new DateTime() };


	if( args != null && args[ "Request" ] != null )
	{
		// THIS LINE CRASHES THE FUNCTION
		CompleteLogin_Request request = JsonConvert.DeserializeObject<CompleteLogin_Request>(args["Request"]);

	}	
}

I'm not sure why, but trying to deserialize the "Request" key from the args dictionary causes problems. It's so silly, I've done this a million times, I can't tell what's wrong.

Any ideas?

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.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

JsonConvert.DeserializeObject requires string as parameter while args["Request"] in your code is an object. Please replace line 37 by

CompleteLogin_Request request = JsonConvert.DeserializeObject<CompleteLogin_Request>(args["Request"].ToString());
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.

Matt avatar image Matt commented ·

Ah hah, perfect, thanks so much. I guess I didn't realize because I was assigning it to a string variable and outputting it to debug with no problem.

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.