0

I am working on ServiceStack's partial response on AutoQuery, code snippets as follow:

public class SalesOrderServices : MyService
    {
        Utilities.RequestUtilities utilities = new Utilities.RequestUtilities();

        public object Any(SalesOrdersGet request)
        {
            String qString = GetSelectString(base.Request);

            var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());

            if (!qString.IsNullOrEmpty())
            {
                q.Select(qString);
            }

            return AutoQuery.Execute(request, q);
        }

which inherits MyService

namespace WombatWS.ServiceInterface.Sales
{
    public class MyService : Service
    {
        public IAutoQuery AutoQuery { get; set; }

        public String GetSelectString(IRequest request)
        {
            String qString = "";

            if (!request.QueryString["fields"].IsNullOrEmpty())
            {
                String sFields = request.QueryString["fields"].ToString();
                String[] properties = sFields.Split(',');

                foreach (String s in properties)
                {
                    if (!s.Equals(properties.Last().ToString()))
                    {
                        qString += s + ",";
                    }
                    else
                    {
                        qString += s;
                    }
                }
            }
            return qString;
        }
    }
}

I noticed besides the interesting fields I put into ?field={field1},{field2}..., unwanted DateTime would be returned as what it is, as well as all the int, GUID types will be also returned as 0, 0000-000000000-0000-00000 something. How to get rid of them? Thanks.

Isley
  • 197
  • 15

1 Answers1

2

A Guid (like an integer) is a value type and every value type must have a value, the default value type of a Guid is new Guid() which is 00000000-0000-0000-0000-000000000000.

If you don't want value types to emit values then you need to use a nullable Guid?. Or you could configure ServiceStack Text serializers not to emit value types with default values with:

JsConifg.ExcludeDefaultValues = true;

Another option is to customize the serialization by implementing ShouldSerialize to ignore fields with default values, e.g:

class Poco
{
     public Guid Guid { get; set; }

    public bool? ShouldSerialize(string fieldName)
    {
        return fieldName == "Guid" ? Guid != default(Guid) : true;
    }
}
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390