0

I have create Wcf service point that returns json. And my question is how to return empty string in json instead null.

{
 "Avatar": null // send only -> ""
 "Email": null,
 "FirstName": "John",
 "ID": 1,
 "LastName": "Travolta",
 "NickName": null,
 "Password": null
}

-------EDIT---------

    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string NickName { get; set; }

    [DataMember]
    public string Email { get { return string.IsNullOrEmpty(_email) ? "" : _email; } set { _email = value; } }

    private string _email;

    [DataMember]
    public string Password { get; set; }

    [DataMember]
    public string Avatar { get; set; }

1 Answer 1

1
{
 "Avatar": EmptyIfNull(null),
 "Email": EmptyIfNull(null),
 "FirstName": "John",
 "ID": 1,
 "LastName": "Travolta",
 "NickName": EmptyIfNull(null),
 "Password": EmptyIfNull(null),
}

private string EmptyIfNull(string source)
{
    return source ?? String.Empty;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i have edit my question and add datacontract that i have. How you could see i have auto generated properties. And my question was if Wcf doesn't have some global property to make it? | In email field i have implemented your code for test.
No, of course not. That's a very unusual request (to say the very least). How about: if you don't have nulls in your source data, you won't have nulls in your JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.