Return only filled fields in the web service

Asked

Viewed 948 times

0

I made a REST with WCF. In a method it returns me an object. It turns out that I don’t fill in all the fields of that object and it shows them all in the reply xml. How do I show only the fields that have value, ie the ones I fill?

  • 1

    I believe this is how it serializes. What is the motivation for this? Just decrease the size of the request/response? If this is the case I advise using JSON.

  • Json is what the partner doesn’t want. The programmer there had problems with it and asked it to be xml. The whole issue is in learning and also in the xml size, that’s all. As this is working.

  • The solification found would be to build a class that has only the attributes that will be returned. Then I return this object and that’s it. The only thing, but it doesn’t impact, is that if there are null attributes, it will return as nil or 0, depending on the type.

  • But that wouldn’t take up the problem of "showing only the fields that have value, that is, the ones I fill in"?

  • I understand, it turns out that if the object has many attributes and I want only a few, I eliminate this part because I could not solve as I would like, ie, show everything that is not null or greater than zero(for Integer and Float)

1 answer

2

To prevent object fields from being serialized in XML (or JSON), you can use the property EmitDefaultValue of the attribute [DataMember]. If she has the value false, numbers with value 0, bool worthwhile false or objects with value null shall not be included in the reply.

public class PT_StackOverflow_17297
{
    [DataContract(Name = "MinhaClasse", Namespace = "")]
    public class MinhaClasse
    {
        [DataMember(EmitDefaultValue = false)]
        public string Nome { get; set; }

        [DataMember(EmitDefaultValue = false)]
        public int Valor { get; set; }

        [DataMember(EmitDefaultValue = false)]
        public bool Flag { get; set; }
    }
    [ServiceContract]
    public class Service
    {
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]
        public MinhaClasse Get()
        {
            var rndGen = new Random();
            var result = new MinhaClasse();
            result.Nome = rndGen.Next(2) == 0 ? "Alguma coisa" : null;
            result.Valor = rndGen.Next(2) == 0 ? 123 : 0;
            result.Flag = rndGen.Next(2) == 0;
            return result;
        }
    }
    public static void Test()
    {
        var baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        var host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        var c = new WebClient();
        for (var i = 0; i < 5; i++)
        {
            Console.WriteLine(c.DownloadString(baseAddress + "/Get"));
            Thread.Sleep(50);
        }

        Console.ReadLine();
        host.Close();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.