关于JavaScriptSerializer一些问题 [英] Some questions regarding JavaScriptSerializer

查看:151
本文介绍了关于JavaScriptSerializer一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 在使用JavaScriptSerializer做到系列化,可以将某些类的领域被忽略?

  2. 在使用JavaScriptSerializer做序列化,我们才能改变字段的名称? 例如,该字段为字符串is_OK,但我想它映射到isOK?

解决方案

对于最大的灵活性(因为你提到的名字也一样),理想的是调用 RegisterConverters JavaScriptSerializer 对象,注册一个或多个 JavaScriptConverter 实现(也许在一个数组或迭代器块)。

您可以再实施序列化添加(或没有),并在任何名称值,通过添加键/值对您返回的字典。如果数据是双向的,你还需要一个匹配的反序列化,但往往(阿贾克斯服务器)这不是必需的。

完整的示例:

 使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
类Foo
{
    公共字符串名称{;组; }
    公共BOOL ImAHappyCamper {获得;组; }
    私有类FooConverter:JavaScriptConverter
    {
        公众覆盖对象反序列化(System.Collections.Generic.IDictionary<字符串,对象>的字典,System.Type的类型,JavaScriptSerializer串行)
        {
            抛出新的NotImplementedException();
        }
        公众覆盖System.Collections.Generic.IEnumerable< System.Type的> SupportedTypes
        {
            获得{收益回报的typeof(美孚); }
        }
        公众覆盖System.Collections.Generic.IDictionary<字符串,对象>序列化(obj对象,JavaScriptSerializer串行)
        {
            VAR数据=新字典<字符串,对象>();
            富富=(美孚)目标文件;
            如果(foo.ImAHappyCamper)data.Add(isOk,foo.ImAHappyCamper);
            如果data.Add(姓名,foo.Name)(string.IsNullOrEmpty(foo.Name)!);
            返回的数据;
        }
    }
    私有静态JavaScriptSerializer串;
    公共静态JavaScriptSerializer串行{
        得到 {
            如果(串行== NULL){
                VAR TMP =新JavaScriptSerializer();
                tmp.RegisterConverters(新[] {新FooConverter()});
                串行= TMP;
            }
            返回序列化;
        }
    }
}
静态类节目{
    静态无效的主要()
    {
        VAR OBJ =新的Foo {ImAHappyCamper = TRUE,名称=弗雷德};
        字符串s = Foo.Serializer.Serialize(OBJ);
    }
}
 

  1. When using JavaScriptSerializer to do serialization, can some of the field of the class be ignored?

  2. When using JavaScriptSerializer to do serialization, can we changes the name of the field? For example, the field is string is_OK, but I want it be mapped to isOK?

解决方案

For the most flexibility (since you mention names as well), the ideal thing is to call RegisterConverters on the JavaScriptSerializer object, registering one or more JavaScriptConverter implementations (perhaps in an array or iterator block).

You can then implement Serialize to add (or not) and values under any names, by adding key/value pairs to the dictionary that you return. If the data is bidirectional you will also need a matching Deserialize, but often (for ajax servers) this is not required.

Full example:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
class Foo
{
    public string Name { get; set; }
    public bool ImAHappyCamper { get; set; }
    private class FooConverter : JavaScriptConverter
    {
        public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }
        public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes
        {
            get { yield return typeof(Foo); }
        }
        public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var data = new Dictionary<string, object>();
            Foo foo = (Foo)obj;
            if (foo.ImAHappyCamper) data.Add("isOk", foo.ImAHappyCamper);
            if(!string.IsNullOrEmpty(foo.Name)) data.Add("name", foo.Name);
            return data;
        }
    }
    private static JavaScriptSerializer serializer;
    public static JavaScriptSerializer Serializer {
        get {
            if(serializer == null) {
                var tmp = new JavaScriptSerializer();
                tmp.RegisterConverters(new [] {new FooConverter()});
                serializer = tmp;
            }
            return serializer;
        }
    }
}
static class Program {
    static void Main()
    {
        var obj = new Foo { ImAHappyCamper = true, Name = "Fred" };
        string s = Foo.Serializer.Serialize(obj);
    }
}

这篇关于关于JavaScriptSerializer一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆