Пример #1
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="json"></param>
        /// <param name="jsonpCallbackFunction"></param>
        public void SendJson(IResponse response, object json, string jsonpCallbackFunction)
        {
            if(response == null) throw new ArgumentNullException("response");
            if(json == null) throw new ArgumentNullException("json");

            response.AddHeader("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate");

            var type = json.GetType();
            JsonSerialiser serialiser;
            lock(_SyncLock) {
                if(!_JsonSerialiserMap.TryGetValue(type, out serialiser)) {
                    serialiser = new JsonSerialiser();
                    serialiser.Initialise(type);
                    _JsonSerialiserMap.Add(type, serialiser);
                }
            }

            string text;
            using(MemoryStream stream = new MemoryStream()) {
                serialiser.WriteObject(stream, json);
                text = Encoding.UTF8.GetString(stream.ToArray());
            }

            if(!String.IsNullOrEmpty(jsonpCallbackFunction)) text = String.Format("{0}({1})", jsonpCallbackFunction, text);

            SendText(response, text, Encoding.UTF8, MimeType.Json);
        }
 public void TestInitialise()
 {
     _JsonSerialiser = new JsonSerialiser();
     _Stream = new MemoryStream();
 }