ChrisP
ChrisP

Reputation: 10116

Why does this JSON deserialization not work in MVC3 compared to MVC

I have an MVC (v1) application that uses the following custom model binder to deserialize inbound JSON data.

  public class JsonModelBinder : DefaultModelBinder
  {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      if (!IsJSONRequest(controllerContext))
      {
        return base.BindModel(controllerContext, bindingContext);
      }

      // Get the JSON data that's been posted
      var request = controllerContext.HttpContext.Request;
      var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();

      // Use the built-in serializer to do the work for us
      return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
    }

    private static bool IsJSONRequest(ControllerContext controllerContext)
    {
      var contentType = controllerContext.HttpContext.Request.ContentType;
      return contentType.Contains("application/json");
    }
  }
}

The following DataItemColleciton class and contained DataItem class are deserialized properly in MVC (v1).


public class DataItemCollection
{

  #region Constructors

  public DataItemCollection()
  {
    this.dataItems = new List<DataItem>();
  }

  public DataItemCollection(string UserName, string UserInitials, int JobNum, int ObjectVersion, int StationID)
  {
    this.userName = UserName;
    this.userInitials = UserInitials;
    this.jobNum = JobNum;
    this.objectVersion = ObjectVersion;
    this.stationID = StationID;
  }

  public DataItemCollection(string UserName, string UserInitials, int JobNum, int ObjectVersion, int StationID, List<DataItem> DataItems)
  : this(UserName, UserInitials, JobNum, ObjectVersion, StationID)
  {
    this.dataItems = DataItems;
  }

  #endregion

  #region Properties

  public string userName { get; set; }
  public string userInitials { get; set; }
  public int jobNum { get; set; }
  public int objectVersion { get; set; }
  public int stationID { get; set; }
  public List<DataItem> dataItems { get; set; }

  #endregion

}

}


public class DataItem
{

  #region Enums

  public enum DataItemType
  {
    SignOff = 1,
    Material = 2,
    Task = 3,
    ShippingInfo = 4,
    Count = 5
  }

  #endregion

  #region Constructors

  public DataItem() { }

  public DataItem(DataItemType ItemType, int DataLength)
  {
    this.itemType = ItemType;
    //Creates array of specified item type for easier access later if necessary...
    switch (ItemType)
    {
      case DataItemType.SignOff:
        this.itemData = new ViewModels.JobTracking.SignOff[DataLength];
        break;
      case DataItemType.Material:
        this.itemData = new ViewModels.JobTracking.Material[DataLength];
        break;
      case DataItemType.Task:
        this.itemData = new ViewModels.JobTracking.Task[DataLength];
        break;
      case DataItemType.ShippingInfo:
        this.itemData = new ViewModels.JobTracking.ShippingInfo[DataLength];
        break;
      case DataItemType.Count:
        this.itemData = new ViewModels.JobTracking.Count[DataLength];
        break;
    }
  }

  #endregion

  #region Properties

  public DataItemType itemType { get; set; }
  public object[] itemData { get; set; }

  #endregion

  }
}

In MVC3 the deserialized JSON has the properties in DataItemCollection but it appears the DataItem objects were not deserialized properly.

DataItemCollection
  .userName
  .userInitials
  .jobNum
  .objectVersion
  .stationID
  .dataItems

The .dataItems property has a collection of generic objects w/ nothing in them.

What is different in the MVC3 deserialization compared to the original MVC which would cause this not to work? Also, any thoughts on how to fix it w/ the least impact on the existing code?

Upvotes: 2

Views: 381

Answers (1)

Michael
Michael

Reputation: 3426

I'm not sure what is the difference between MVC 1 and MVC 3 in this case but I usually use JSON.NET which is much more powerful yet very lightweight to serialize and de-serialize to json.

http://json.codeplex.com/

Upvotes: 1

Related Questions