WCF的JSON响应转义名称值

时间:2013-05-21 15:46:38

标签: json wcf serialization

我正在尝试从WCF REST服务返回一个字典作为JSON对象。我希望格式化 {“核心价值”} 所以我创建了自己的类,如herehere所述。

服务方法有效。问题是密钥名称是转义的。例如,如果我的字典包含“Hello world”:100,我就知道了 { “Hello_x0020_world”:100}。它也逃脱了其他字符,如%等。

有什么方法可以告诉序列化不要以这种方式逃避名称?它几乎就像是使用不一定适用于JSON的xml规则。

我的可序列化类:

[Serializable]
public class JsonDictionary : ISerializable
{
    private Dictionary<string, object> _Dictionary;
    public JsonDictionary()
    {
        _Dictionary = new Dictionary<string, object>();
    }
    public JsonDictionary(SerializationInfo info, StreamingContext context)
    {
        _Dictionary = new Dictionary<string, object>();
        SerializationInfoEnumerator enumerator = info.GetEnumerator();
        while (enumerator.MoveNext())
        {
            _Dictionary.Add(enumerator.Name, enumerator.Value);
        }
    }
    public object this[string key]
    {
        get { return _Dictionary[key]; }
        set { _Dictionary[key] = value; }
    }
    public void Add(string key, object value)
    {
        _Dictionary.Add(key, value);
    }
    public bool ContainsKey(string key)
    {
        return _Dictionary.ContainsKey(key);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (string key in _Dictionary.Keys)
            info.AddValue(key, _Dictionary[key], _Dictionary[key] == null ? typeof(object) : _Dictionary[key].GetType());  
    }
}

我的服务功能定义:

[WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
    public JsonDictionary GetCollection()
    {
        JsonDictionary dict = new JsonDictionary();

        dict.Add("Hello world", 100);
        return dict;
    }

1 个答案:

答案 0 :(得分:1)

WCF中默认使用的DataContractJsonSerializer使用XML-to-JSON映射,这会导致一些问题,例如您在ISerializable类型中看到的问题。但是,您可以使用自定义格式化程序来更改响应的序列化方式。在下面的示例中,我正在使用JSON.NET“正确”处理ISerializable个对象。

public class StackOverflow_16674152
{
    [Serializable]
    public class JsonDictionary : ISerializable
    {
        private Dictionary<string, object> _Dictionary;
        public JsonDictionary()
        {
            _Dictionary = new Dictionary<string, object>();
        }
        public JsonDictionary(SerializationInfo info, StreamingContext context)
        {
            _Dictionary = new Dictionary<string, object>();
            SerializationInfoEnumerator enumerator = info.GetEnumerator();
            while (enumerator.MoveNext())
            {
                _Dictionary.Add(enumerator.Name, enumerator.Value);
            }
        }
        public object this[string key]
        {
            get { return _Dictionary[key]; }
            set { _Dictionary[key] = value; }
        }
        public void Add(string key, object value)
        {
            _Dictionary.Add(key, value);
        }
        public bool ContainsKey(string key)
        {
            return _Dictionary.ContainsKey(key);
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (string key in _Dictionary.Keys)
                info.AddValue(key, _Dictionary[key], _Dictionary[key] == null ? typeof(object) : _Dictionary[key].GetType());
        }
    }
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
        [MyISerializableResponseJsonBehavior]
        public JsonDictionary GetCollection()
        {
            JsonDictionary dict = new JsonDictionary();

            dict.Add("Hello world", 100);
            return dict;
        }
    }
    public class MyFormatter : IDispatchMessageFormatter
    {
        IDispatchMessageFormatter original;
        string replyAction;
        public MyFormatter(IDispatchMessageFormatter original, string replyAction)
        {
            this.original = original;
            this.replyAction = replyAction;
        }

        public void DeserializeRequest(Message message, object[] parameters)
        {
            this.original.DeserializeRequest(message, parameters);
        }

        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
        {
            ISerializable serializable = result as ISerializable;
            if (serializable != null)
            {
                string json = JsonConvert.SerializeObject(serializable);
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                var writer = new MyRawWriter(bytes);
                Message reply = Message.CreateMessage(messageVersion, replyAction, writer);
                reply.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
                return reply;
            }
            else
            {
                return this.original.SerializeReply(messageVersion, parameters, result);
            }
        }

        class MyRawWriter : BodyWriter
        {
            byte[] data;
            public MyRawWriter(byte[] data)
                : base(true)
            {
                this.data = data;
            }

            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
            {
                writer.WriteStartElement("Binary");
                writer.WriteBase64(data, 0, data.Length);
                writer.WriteEndElement();
            }
        }
    }
    public class MyISerializableResponseJsonBehaviorAttribute : Attribute, IOperationBehavior
    {
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            if (operationDescription.Messages.Count > 1)
            {
                dispatchOperation.Formatter = new MyFormatter(dispatchOperation.Formatter, operationDescription.Messages[1].Action);
            }
        }

        public void Validate(OperationDescription operationDescription)
        {
            if (operationDescription.Messages.Count > 1)
            {
                var respMessage = operationDescription.Messages[1];
                if (respMessage.Body.Parts.Count > 0)
                {
                    throw new InvalidOperationException("Cannot be used with out/ref parameters");
                }
            }

            var wga = operationDescription.Behaviors.Find<WebGetAttribute>();
            var wia = operationDescription.Behaviors.Find<WebInvokeAttribute>();
            WebMessageBodyStyle bodyStyle = WebMessageBodyStyle.Bare; // default
            if (wga != null && wga.IsBodyStyleSetExplicitly) {
                bodyStyle = wga.BodyStyle;
            }

            if (wia != null && wia.IsBodyStyleSetExplicitly) {
                bodyStyle = wia.BodyStyle;
            }

            if (bodyStyle == WebMessageBodyStyle.Wrapped || bodyStyle == WebMessageBodyStyle.WrappedResponse)
            {
                throw new InvalidOperationException("This behavior can only be used with bare response style");
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/"));
    }
}