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

« back to all changes in this revision

Viewing changes to lib/ServiceStack.Text/src/ServiceStack.Text/Jsv/JsvWriter.Generic.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
//
 
2
// http://code.google.com/p/servicestack/wiki/TypeSerializer
 
3
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
 
4
//
 
5
// Authors:
 
6
//   Demis Bellot (demis.bellot@gmail.com)
 
7
//
 
8
// Copyright 2011 Liquidbit Ltd.
 
9
//
 
10
// Licensed under the same terms of ServiceStack: new BSD license.
 
11
//
 
12
 
 
13
using System;
 
14
using System.Collections.Generic;
 
15
using System.IO;
 
16
using System.Reflection;
 
17
using System.Threading;
 
18
using ServiceStack.Text.Common;
 
19
 
 
20
namespace ServiceStack.Text.Jsv
 
21
{
 
22
        internal static class JsvWriter
 
23
        {
 
24
                public static readonly JsWriter<JsvTypeSerializer> Instance = new JsWriter<JsvTypeSerializer>();
 
25
 
 
26
        private static Dictionary<Type, WriteObjectDelegate> WriteFnCache = new Dictionary<Type, WriteObjectDelegate>();
 
27
 
 
28
                public static WriteObjectDelegate GetWriteFn(Type type)
 
29
                {
 
30
                        try
 
31
                        {
 
32
                WriteObjectDelegate writeFn;
 
33
                if (WriteFnCache.TryGetValue(type, out writeFn)) return writeFn;
 
34
 
 
35
                var genericType = typeof(JsvWriter<>).MakeGenericType(type);
 
36
                var mi = genericType.GetMethod("WriteFn", BindingFlags.Public | BindingFlags.Static);
 
37
                var writeFactoryFn = (Func<WriteObjectDelegate>)Delegate.CreateDelegate(typeof(Func<WriteObjectDelegate>), mi);
 
38
                writeFn = writeFactoryFn();
 
39
 
 
40
                Dictionary<Type, WriteObjectDelegate> snapshot, newCache;
 
41
                do
 
42
                {
 
43
                    snapshot = WriteFnCache;
 
44
                    newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache);
 
45
                    newCache[type] = writeFn;
 
46
 
 
47
                } while (!ReferenceEquals(
 
48
                    Interlocked.CompareExchange(ref WriteFnCache, newCache, snapshot), snapshot));
 
49
 
 
50
                return writeFn;
 
51
                        }
 
52
                        catch (Exception ex)
 
53
                        {
 
54
                                Tracer.Instance.WriteError(ex);
 
55
                                throw;
 
56
                        }
 
57
                }
 
58
 
 
59
                public static void WriteLateBoundObject(TextWriter writer, object value)
 
60
                {
 
61
                        if (value == null) return;
 
62
                        var writeFn = GetWriteFn(value.GetType());
 
63
 
 
64
                        var prevState = JsState.IsWritingDynamic;
 
65
                        JsState.IsWritingDynamic = true;
 
66
                        writeFn(writer, value);
 
67
                        JsState.IsWritingDynamic = prevState;
 
68
                }
 
69
 
 
70
                public static WriteObjectDelegate GetValueTypeToStringMethod(Type type)
 
71
                {
 
72
                        return Instance.GetValueTypeToStringMethod(type);
 
73
                }
 
74
        }
 
75
 
 
76
        /// <summary>
 
77
        /// Implement the serializer using a more static approach
 
78
        /// </summary>
 
79
        /// <typeparam name="T"></typeparam>
 
80
        internal static class JsvWriter<T>
 
81
        {
 
82
                private static readonly WriteObjectDelegate CacheFn;
 
83
 
 
84
                public static WriteObjectDelegate WriteFn()
 
85
                {
 
86
                        return CacheFn ?? WriteObject;
 
87
                }
 
88
 
 
89
                static JsvWriter()
 
90
                {
 
91
                    CacheFn = typeof(T) == typeof(object) 
 
92
                ? JsvWriter.WriteLateBoundObject 
 
93
                : JsvWriter.Instance.GetWriteFn<T>();
 
94
                }
 
95
 
 
96
            public static void WriteObject(TextWriter writer, object value)
 
97
                {
 
98
                        CacheFn(writer, value);
 
99
                }
 
100
 
 
101
        }
 
102
}
 
 
b'\\ No newline at end of file'