在MVC中使用Json.Net序列化和反序列化Json对象

简介:

在.Net的MVC开发中,经常会使用到Json对象,于是,系统提供了JsonResult这个对象,其本质是调用.Net系统自带的Json序列化类JavaScriptSerializer对数据对象进行序列化。但是这个系统自带的Json序列化对象方法没有Json.Net好用,于是打算有些时候用Json.Net替代默认的实现。

要实现有时候用Json.Net,有时候用默认实现,那么就要保证系统中两种实现并存。对于Server将对象序列化成Json传给Client很简单,我们只需要建立一个新的ActionResult,我们命名为JsonNetResult,然后在Get时,return这个JsonNetResult即可。JsonNetResult的代码实现为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcJsonNet
{
 using System.IO;
 using System.Web;
 using System.Web.Mvc;
 using Newtonsoft.Json;

 public class JsonNetResult : JsonResult
 {
 public JsonNetResult()
 {
 Settings = new JsonSerializerSettings
 {
 ReferenceLoopHandling = ReferenceLoopHandling.Error
 };
 }
 public JsonNetResult(object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet, string contentType=null, Encoding contentEncoding=null)
 {
 this.Data = data;
 this.JsonRequestBehavior = behavior;
 this.ContentEncoding = contentEncoding;
 this.ContentType = contentType;
 }

 public JsonSerializerSettings Settings { get; private set; }

 public override void ExecuteResult(ControllerContext context)
 {
 

 if (context == null)
 throw new ArgumentNullException("context");
 if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
 throw new InvalidOperationException("JSON GET is not allowed");

 HttpResponseBase response = context.HttpContext.Response;
 response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

 if (this.ContentEncoding != null)
 response.ContentEncoding = this.ContentEncoding;
 if (this.Data == null)
 return;

 var scriptSerializer = JsonSerializer.Create(this.Settings);

 using (var sw = new StringWriter())
 {
 scriptSerializer.Serialize(sw, this.Data);
 response.Write(sw.ToString());
 }
 }
 }
}

要返回一个Json.Net序号列后的对象,那么调用方法是:

[HttpGet]
public ActionResult GetJsonNet()
{
 var myClass = InitClass();
 return new JsonNetResult(myClass);
}

这是Get方法,但是对于ClientPost一个Json回Server,那么就比较麻烦了,需要修改好几处地方:

1,建立Json.Net的ValueProviderFactory,这个类主要就是用于Json字符串的反序列化。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcJsonNet
{
 using System.Collections;
 using System.Dynamic;
 using System.Globalization;
 using System.IO;
 using System.Web.Mvc;
 using System.Web.Script.Serialization;
 using Newtonsoft.Json;

 public class JsonNetValueProviderFactory : ValueProviderFactory
 {
 private void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value)
 {
 IDictionary<string, object> d = value as IDictionary<string, object>;
 if (d != null)
 {
 foreach (KeyValuePair<string, object> entry in d)
 {
 AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);
 }
 return;
 }

 IList l = value as IList;
 if (l != null)
 {
 for (int i = 0; i < l.Count; i++)
 {
 AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);
 }
 return;
 }

 // primitive
 backingStore[prefix] = value;
 }

 private object GetDeserializedObject(ControllerContext controllerContext)
 {
 if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.InvariantCultureIgnoreCase))
 {
 // not JSON request return null;
 }

 StreamReader reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
 string bodyText = reader.ReadToEnd();
 if (String.IsNullOrEmpty(bodyText))
 {
 // no JSON data return null;
 }
 //接下来的代码是关键,判断content type,如果是json.net,那么就使用Json.Net的反序列化方法,如果不是,那么就使用系统默认的反序列化方法 if (controllerContext.HttpContext.Request.ContentType.StartsWith("application/json.net", StringComparison.InvariantCultureIgnoreCase))
 {
 var jsonData = JsonConvert.DeserializeObject<ExpandoObject>(bodyText);
 return jsonData;
 }
 else
 {
 JavaScriptSerializer serializer = new JavaScriptSerializer();
 object jsonData = serializer.DeserializeObject(bodyText);
 return jsonData;
 }
 }

 public override IValueProvider GetValueProvider(ControllerContext controllerContext)
 {
 if (controllerContext == null)
 {
 throw new ArgumentNullException("controllerContext");
 }

 object jsonData = GetDeserializedObject(controllerContext);
 if (jsonData == null)
 {
 return null;
 }

 Dictionary<string, object> backingStore = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
 AddToBackingStore(backingStore, String.Empty, jsonData);
 return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture);
 }

 private string MakeArrayKey(string prefix, int index)
 {
 return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";
 }

 private string MakePropertyKey(string prefix, string propertyName)
 {
 return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName;
 }
 }
}

2,在初始化MVC时替换掉默认的JsonValueProviderFactory。
在Global.asax的Application_Start时,写入以下代码:

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());

3,建立新的ModelBinder,命名为JsonNetModelBinder。

namespace MvcJsonNet
{
 using System;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Globalization;
 using System.Linq;
 using System.Web.Mvc;
 using Newtonsoft.Json;

 public class JsonNetModelBinder : DefaultModelBinder
 {
 protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext,
 PropertyDescriptor propertyDescriptor)
 {
 Debug.WriteLine("BindProperty");
 if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json.net",
 StringComparison
 .InvariantCultureIgnoreCase))
 {
 //根据Content type来判断,只有json.net这种content type的才会使用该ModelBinder,否则使用默认的Binder base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
 return;
 }

 // need to skip properties that aren't part of the request, else we might hit a StackOverflowException string name = propertyDescriptor.Name;
 foreach (object attribute in propertyDescriptor.Attributes)
 {
 if (attribute is JsonPropertyAttribute)
 {
 var jp = attribute as JsonPropertyAttribute;
 name = jp.PropertyName;
 }
 }

 string fullPropertyKey = CreateSubPropertyName(bindingContext.ModelName, name);
 if (!bindingContext.ValueProvider.ContainsPrefix(fullPropertyKey))
 {
 return;
 }

 // call into the property's model binder
 IModelBinder propertyBinder = Binders.GetBinder(propertyDescriptor.PropertyType);
 object originalPropertyValue = propertyDescriptor.GetValue(bindingContext.Model);
 ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[propertyDescriptor.Name];
 propertyMetadata.Model = originalPropertyValue;
 var innerBindingContext = new ModelBindingContext
 {
 ModelMetadata = propertyMetadata,
 ModelName = fullPropertyKey,
 ModelState = bindingContext.ModelState,
 ValueProvider = bindingContext.ValueProvider
 };
 object newPropertyValue = GetPropertyValue(controllerContext, innerBindingContext, propertyDescriptor,
 propertyBinder);
 propertyMetadata.Model = newPropertyValue;

 // validation
 ModelState modelState = bindingContext.ModelState[fullPropertyKey];
 if (modelState == null || modelState.Errors.Count == 0)
 {
 if (OnPropertyValidating(controllerContext, bindingContext, propertyDescriptor, newPropertyValue))
 {
 SetProperty(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
 OnPropertyValidated(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
 }
 }
 else
 {
 SetProperty(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);

 // Convert FormatExceptions (type conversion failures) into InvalidValue messages foreach (
 ModelError error in
 modelState.Errors.Where(err => String.IsNullOrEmpty(err.ErrorMessage) && err.Exception != null)
 .ToList())
 {
 for (Exception exception = error.Exception; exception != null; exception = exception.InnerException)
 {
 if (exception is FormatException)
 {
 string displayName = propertyMetadata.GetDisplayName();
 string errorMessageTemplate = "The value '{0}' is not valid for {1}.";
 string errorMessage = String.Format(CultureInfo.CurrentCulture, errorMessageTemplate,
 modelState.Value.AttemptedValue, displayName);
 modelState.Errors.Remove(error);
 modelState.Errors.Add(errorMessage);
 break;
 }
 }
 }
 }
 }
 }
}

4,建立一个VModel的基类,为该基类添加Attribute,然后在Global中添加Model和Binder的映射。

[ModelBinder(typeof (JsonNetModelBinder))]
public abstract class VEntity
{
 public virtual long Id { get; set; }
}

Global.asax中Application_Start添加代码:

 ModelBinders.Binders.Add(typeof(VEntity), new JsonNetModelBinder());

5在前端Post Json时,指定content type为application/json.net

 function PostJsonNet() {
 var jsonstr = $("#jsonstring")[0].innerHTML;
 $.ajax({
 url: "MyTest/CreateFromJsonNet",
 type: "POST",
 data: jsonstr,
 contentType: "application/json.net",
 dataType: "json",
 success: function (data) {
 alert(data);
 
 }
 });
 }

我们这样处理后,Client在往Server传送Json数据时,如果指定了contentType是application/json,那么就使用系统默认的方法来反序列化对象,如果是application/json.net,那么就使用Json.Net来反序列化。

示例程序下载

目录
相关文章
|
3月前
|
JSON 数据格式 索引
Python中序列化/反序列化JSON格式的数据
【11月更文挑战第4天】本文介绍了 Python 中使用 `json` 模块进行序列化和反序列化的操作。序列化是指将 Python 对象(如字典、列表)转换为 JSON 字符串,主要使用 `json.dumps` 方法。示例包括基本的字典和列表序列化,以及自定义类的序列化。反序列化则是将 JSON 字符串转换回 Python 对象,使用 `json.loads` 方法。文中还提供了具体的代码示例,展示了如何处理不同类型的 Python 对象。
|
3月前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第22天】在Java的世界里,对象序列化和反序列化是数据持久化和网络传输的关键技术。本文将带你了解如何在Java中实现对象的序列化与反序列化,并探讨其背后的原理。通过实际代码示例,我们将一步步展示如何将复杂数据结构转换为字节流,以及如何将这些字节流还原为Java对象。文章还将讨论在使用序列化时应注意的安全性问题,以确保你的应用程序既高效又安全。
|
4月前
|
存储 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第9天】在Java的世界里,对象序列化是连接数据持久化与网络通信的桥梁。本文将深入探讨Java对象序列化的机制、实践方法及反序列化过程,通过代码示例揭示其背后的原理。从基础概念到高级应用,我们将一步步揭开序列化技术的神秘面纱,让读者能够掌握这一强大工具,以应对数据存储和传输的挑战。
|
4月前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第3天】在Java编程的世界里,对象序列化与反序列化是实现数据持久化和网络传输的关键技术。本文将深入探讨Java序列化的原理、应用场景以及如何通过代码示例实现对象的序列化与反序列化过程。从基础概念到实践操作,我们将一步步揭示这一技术的魅力所在。
|
3月前
|
存储 缓存 NoSQL
一篇搞懂!Java对象序列化与反序列化的底层逻辑
本文介绍了Java中的序列化与反序列化,包括基本概念、应用场景、实现方式及注意事项。序列化是将对象转换为字节流,便于存储和传输;反序列化则是将字节流还原为对象。文中详细讲解了实现序列化的步骤,以及常见的反序列化失败原因和最佳实践。通过实例和代码示例,帮助读者更好地理解和应用这一重要技术。
96 0
|
5月前
|
JSON fastjson Java
niubility!即使JavaBean没有默认无参构造器,fastjson也可以反序列化。- - - - 阿里Fastjson反序列化源码分析
本文详细分析了 Fastjson 反序列化对象的源码(版本 fastjson-1.2.60),揭示了即使 JavaBean 沲有默认无参构造器,Fastjson 仍能正常反序列化的技术内幕。文章通过案例展示了 Fastjson 在不同构造器情况下的行为,并深入探讨了 `ParserConfig#getDeserializer` 方法的核心逻辑。此外,还介绍了 ASM 字节码技术的应用及其在反序列化过程中的角色。
127 10
|
5月前
|
JSON 安全 编译器
扩展类实例的序列化和反序列化
扩展类实例的序列化和反序列化
58 1
|
5月前
|
存储 XML JSON
用示例说明序列化和反序列化
用示例说明序列化和反序列化
42 1
|
5月前
|
存储 Java 开发者
Java编程中的对象序列化与反序列化
【9月更文挑战第20天】在本文中,我们将探索Java编程中的一个核心概念——对象序列化与反序列化。通过简单易懂的语言和直观的代码示例,你将学会如何将对象状态保存为字节流,以及如何从字节流恢复对象状态。这不仅有助于理解Java中的I/O机制,还能提升你的数据持久化能力。准备好让你的Java技能更上一层楼了吗?让我们开始吧!
|
5月前
|
XML Dubbo Java
分布式-序列化,反序列化
分布式-序列化,反序列化

热门文章

最新文章