WebApi and Json Serialization

Posts   
 
    
usschad
User
Posts: 77
Joined: 11-Sep-2008
# Posted on: 12-Mar-2015 03:25:20   

I'm having that same problem as the guys in this following threads:

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22349 http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22583

There's enough information in both of those threads for a workaround from the normal way it's supposed to work. But I'd like to point out a few things.

I followed the instructions at: http://www.llblgen.com/documentation/4.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Adapter/Distributed%20systems/gencode_webservices.htm#decorating

I have the WebApiConfig.cs set up. I decorated my Entities using the designer with DataContract and DataMember.

I create a method in my ApiController That returns an IEntity2 object and it works. But... I like to wrap my data in a an object that also has other properties in case something goes wrong (to be able to display errors, additional information, etc).

When I change the return type to the JsonWrapper with my PaymentEntity as a property within, that's when it starts opting to use the XmlFormatter and gives the following error:

Type 'XXX.XXX.Data.EntityClasses.PaymentEntity' cannot be IXmlSerializable and have DataContractAttribute attribute.

Here's my class. Originally, it wasn't decorated with DataContractAttribute or DataMemberAttribute, but I added it to try to resolve the error. Also, the Data property was just type 'object' at first, but I changed it to match the PaymentEntity to figure out why it wasn't working... no luck:

    
        [DataContract]
    public class JsonResult
    {

        [DataMember]
        public int? Status { get; set; }

        [DataMember]
        public List<string> InfoMessages { get; set; }

        [DataMember]
        public List<string> ErrorMessages { get; set; }

        [DataMember]
        public List<PaymentEntity> Data { get; set; }

    }

Controller:


        public JsonResult Get(int id)
        {
            JsonResult result = new JsonResult();

            PaymentEntity payment = new PaymentEntity(id);
            if (new PaymentDAO(AppContext.Current).Fetch(payment))
            {
                result.Data.Add(payment);
                result.Status = 0;
            }
            else
            {
                result.Status = -1;
                result.ErrorMessages.Add(string.Format("Payment with Id {{0}} could not be found.", id));
            }

            return result;
        }

So if all I change here is the return type to PaymentEntity, and return payment instead of result, it works.

I could probably also get this to work if I follow those first links above and set my return type to HttpResponseMessage. Malarkey. I didn't upgrade my application to use WebApi to have to manually convert my objects. It should be magic wink

On both of those threads, the last post was someone suggesting to clear out all the Formatters and add back the json formatter. That made me nervous, I don't know what that will break later down the road. And I'm likely to forget I did that and switch jobs.

I set breakpoint and found the following 4 Formatters in there: {System.Net.Http.Formatting.JsonMediaTypeFormatter} {System.Net.Http.Formatting.XmlMediaTypeFormatter} {System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter} {System.Web.Http.ModelBinding.JQueryMvcFormUrlEncodedFormatter}

I'm still nervous about removing anything, but instead of clearing the whole thing, perhaps removing the obvious one ({System.Net.Http.Formatting.XmlMediaTypeFormatter})?

I added this code to the WebApiConfig.cs:

config.Formatters.Remove(config.Formatters.XmlFormatter);

And it works! Hopefully, I won't need the XmlFormatter for anything. It would be nice to not have to remove it. And I also thought maybe there was something I could do with my JsonResult class to make it use the JsonFormatter like the PaymentEntity by itself does. I changed the JsonResult.Data property to a type of object, and in the controller, I set the Data to just a dynamic object with a string property. That worked too. For some reason, it seems when the Entity is nested within another object, it chokes.

I just wanted to share my findings for others having the same problem; and I wouldn't mind if someone knows how to let that JsonMediaTypeFormatter take precedence over XmlMediaTypeFormatter so that it doesn't have to be removed.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Mar-2015 07:12:07   

Hi usschad. Thanks for the feedback and contribution!

David Elizondo | LLBLGen Support Team