Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JSON empty string deserializes to null at the server side?

I'm sending empty string through $.post and it deserializes to null. How to differentiate if the string was empty or null at the client side ?

Regards

UPDATE What I'm actually doing is:

$.post("Controller/Action", $.param({Name: ""}, true), null, "json");

at the server:

public Container
{
   public string Name;
}

public void Action(Container container)
{
    bool c = container.Name == null;   // c is true, why ?     
}
like image 206
jwaliszko Avatar asked Oct 01 '10 12:10

jwaliszko


2 Answers

A variable with empty value is written in JSON as:

{ "var" : "" }

An empty string is parsed as null as there is no object defined in it.

like image 72
frisco Avatar answered Sep 19 '22 10:09

frisco


What do you mean by "empty string" ?

The JSON representation of an empty string is "", not an empty string. An empty string actually means "nothing", so null

like image 26
Philippe Leybaert Avatar answered Sep 19 '22 10:09

Philippe Leybaert