~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ServiceModel/Serialization/JsonDataContractSerializer.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.IO;
 
3
using System.Runtime.Serialization;
 
4
using ServiceStack.DesignPatterns.Serialization;
 
5
using ServiceStack.Text;
 
6
 
 
7
namespace ServiceStack.ServiceModel.Serialization
 
8
{
 
9
    public class JsonDataContractSerializer 
 
10
    {
 
11
        public static JsonDataContractSerializer Instance = new JsonDataContractSerializer();
 
12
 
 
13
        public ITextSerializer TextSerializer { get; set; }
 
14
 
 
15
        public static void UseSerializer(ITextSerializer textSerializer)
 
16
        {
 
17
            Instance.TextSerializer = textSerializer;
 
18
            JsonDataContractDeserializer.Instance.TextSerializer = textSerializer;
 
19
        }
 
20
 
 
21
        public bool UseBcl { get; set; }
 
22
 
 
23
        public string SerializeToString<T>(T obj)
 
24
        {
 
25
            if (TextSerializer != null)
 
26
                return TextSerializer.SerializeToString(obj);
 
27
 
 
28
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
29
            if (!UseBcl)
 
30
                return JsonSerializer.SerializeToString(obj);
 
31
 
 
32
            if (obj == null) return null;
 
33
            var type = obj.GetType();
 
34
            try
 
35
            {
 
36
                using (var ms = new MemoryStream())
 
37
                {
 
38
                    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
 
39
                    serializer.WriteObject(ms, obj);
 
40
                    ms.Position = 0;
 
41
                    using (var sr = new StreamReader(ms))
 
42
                    {
 
43
                        return sr.ReadToEnd();
 
44
                    }
 
45
                }
 
46
            }
 
47
            catch (Exception ex)
 
48
            {
 
49
                throw new SerializationException("JsonDataContractSerializer: Error converting type: " + ex.Message, ex);
 
50
            }
 
51
#else
 
52
                return JsonSerializer.SerializeToString(obj);
 
53
#endif
 
54
        }
 
55
 
 
56
        public void SerializeToStream<T>(T obj, Stream stream)
 
57
        {
 
58
            if (TextSerializer != null)
 
59
            {
 
60
                TextSerializer.SerializeToStream(obj, stream);
 
61
            }
 
62
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
63
            else if (UseBcl)
 
64
            {
 
65
                var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
 
66
                serializer.WriteObject(stream, obj);
 
67
            }
 
68
#endif
 
69
            else
 
70
            {
 
71
                JsonSerializer.SerializeToStream(obj, stream);
 
72
            }
 
73
        }
 
74
    }
 
75
}