Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ServiceStack to format Guids with dashes when using JSON?

Using ServiceStack to return a simple User object from a Post.

The user object is pretty simple:

public class User
{
    public Guid Id { get; set; }
    public string Username { get; set; }
}

The post method:

public override object OnPost(User user)
{
    User newUser;
    // Do stuff to user...
    return newUser;
}

My issue is that ServiceStack formats the Guid without dashes, so the JSON response looks e.g. like this:

{"id":"c884ce020ec94c65a8788c2ddc500434","username":"new user name"}

The client is an Android/Java client and the UUID.fromString() method does get upset about this format.

Is there any way to force ServiceStack to return a Guid formatted with dashes?

like image 736
Torben Koch Pløen Avatar asked Mar 19 '12 16:03

Torben Koch Pløen


1 Answers

you can override your Guid serializer as

ServiceStack.Text.JsConfig<Guid>.SerializeFn = guid => guid.ToString();
like image 182
agirma Avatar answered Oct 19 '22 19:10

agirma