简体   繁体   中英

Serialization issue using JsonConvert.SerializeObject (properties missing on server)

I am getting pretty weird behaviour using JsonConvert.SerializeObject.

I have a method looking like the following:

public class ServiceController : ApiController
{
    public object GetJSONConnectedResponse(object input)
    {
        return JsonConvert.SerializeObject(input, Formatting.Indented,
        new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Serialize
        });
    }


    public object GetMediaGallery(int? id)
    {
        try
        {
            return GetJSONConnectedResponse(GalleryBLL.GetMediaGallery(id));
        }
        catch (Exception exc)
        {
            LogBLL.LogException(exc, HttpContext.Current.Request.RawUrl);
            return null;
        }
    }
}

As you can see, I am using MVC Web API as a service application from which I query ajax-type of data using javascript on the client side.

The method GetMediaGallery returns a holder class of the following structure:

public class MediaGalleryHolder
{
    public List<DB_Image> Images { get; set; }
    public List<string> SpinFrames { get; set; }
    public string FlyoverVideo { get; set; }
}

DB_Images is a complex entity which I populate in a business logic method by calling a stored procedure. On this complex entity, I have added 2 extra properties.

public partial class DB_Image
{
    public string FullPath { get; set; }
    public string ThumbPath { get; set; }
}

For some reason, on my local machine, the result serializes correctly (adding both the 2 extra properties), but on the server those extra properties are not added to the result (I know they are populated, but never serialized).

Is this some sort of JSON.NET or MVC Web API versioning issue, how can I go about to troubleshoot or resolve this?

Any help would be greatly appreciated!

Best,

tribe84

Have you tried with annotations on each class? you can try adding [Serializable]

  [Serializable] 
  public partial class DB_Image
  {
        public string FullPath { get; set; }
        public string ThumbPath { get; set; }
  }

  [Serializable]
  public class MediaGalleryHolder
  {
       public List<DB_Image> Images { get; set; }
       public List<string> SpinFrames { get; set; }
       public string FlyoverVideo { get; set; }
  }

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM