简体   繁体   English

在Dynamics CRM 2013/2015插件中序列化/反序列化JSON

[英]Serializing/Deserializing JSON in Dynamics CRM 2013/2015 Plugin

I am developing a plugin for Dynamics 2015. The plugin requires some setup information which I thought would be a good idea to use a json object in the unsecure configuration. 我正在为Dynamics 2015开发一个插件。该插件需要一些设置信息,我认为在不安全的配置中使用json对象是个好主意。 Also, there's an external system which is writing some json into a field in the entity that this plugin responds to which I need to extract information from. 此外,有一个外部系统正在将一些json写入该插件响应的实体中的一个字段,我需要从中提取信息。

I have tried to use JavascriptSerializer object to deserialize the json but I get a System.MethodAccessException, upon doing some research, I have found out that I can't use the JavascriptSerializer in sandbox mode. 我试图使用JavascriptSerializer对象反序列化json,但我得到一个System.MethodAccessException,经过一些研究,我发现我不能在沙盒模式下使用JavascriptSerializer。

I don't want to have to ILMerge Json.Net into my assembly, so is there any other way I can serialize/deserialize json in my plugin code? 我不想将ILMerge Json.Net放到我的程序集中,所以有没有其他方法可以在我的插件代码中序列化/反序列化json?

So here's how I solved this one 所以这就是我解决这个问题的方法

Given 特定

 string wsData = string.Empty;
 plItem.GetType() == typeof(CategoryInfo);

and

 [DataContract]
 public class CategoryInfo{
    [DataMember]
    public string AllPropertiesToSerialize{ get; set; }
 }  

then 然后

using (var ms = new MemoryStream())
{
     var js = new DataContractJsonSerializer(typeof(CategoryInfo));
     js.WriteObject(ms, plItem);
     ms.Position = 0;
     var sr = new StreamReader(ms);
     wsData = sr.ReadToEnd();
}

Thanks to @dbc and @Guido Preite for the pointers 感谢@dbc和@Guido Preite的指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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