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

« back to all changes in this revision

Viewing changes to lib/ServiceStack.Text/src/ServiceStack.Text/JsonSerializer.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.IO;
 
15
using System.Text;
 
16
using ServiceStack.Text.Common;
 
17
using ServiceStack.Text.Json;
 
18
 
 
19
namespace ServiceStack.Text
 
20
{
 
21
        public class JsonSerializer<T> : ITypeSerializer<T>
 
22
        {
 
23
                public bool CanCreateFromString(Type type)
 
24
                {
 
25
                        return JsonReader.GetParseFn(type) != null;
 
26
                }
 
27
 
 
28
                /// <summary>
 
29
                /// Parses the specified value.
 
30
                /// </summary>
 
31
                /// <param name="value">The value.</param>
 
32
                /// <returns></returns>
 
33
                public T DeserializeFromString(string value)
 
34
                {
 
35
                        if (string.IsNullOrEmpty(value)) return default(T);
 
36
                        return (T)JsonReader<T>.Parse(value);
 
37
                }
 
38
 
 
39
                public T DeserializeFromReader(TextReader reader)
 
40
                {
 
41
                        return DeserializeFromString(reader.ReadToEnd());
 
42
                }
 
43
 
 
44
                public string SerializeToString(T value)
 
45
                {
 
46
                        if (value == null) return null;
 
47
                        if (typeof(T) == typeof(string)) return value as string;
 
48
            if (typeof(T) == typeof(object) || typeof(T).IsAbstract || typeof(T).IsInterface)
 
49
            {
 
50
                if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = true;
 
51
                var result = JsonSerializer.SerializeToString(value, value.GetType());
 
52
                if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = false;
 
53
                return result;
 
54
            }
 
55
 
 
56
                        var sb = new StringBuilder();
 
57
                        using (var writer = new StringWriter(sb))
 
58
                        {
 
59
                                JsonWriter<T>.WriteObject(writer, value);
 
60
                        }
 
61
                        return sb.ToString();
 
62
                }
 
63
 
 
64
                public void SerializeToWriter(T value, TextWriter writer)
 
65
                {
 
66
                        if (value == null) return;
 
67
                        if (typeof(T) == typeof(string))
 
68
                        {
 
69
                                writer.Write(value);
 
70
                                return;
 
71
                        }
 
72
            if (typeof(T) == typeof(object) || typeof(T).IsAbstract || typeof(T).IsInterface)
 
73
            {
 
74
                if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = true;
 
75
                JsonSerializer.SerializeToWriter(value, value.GetType(), writer);
 
76
                if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = false;
 
77
                return;
 
78
            }
 
79
           
 
80
            JsonWriter<T>.WriteObject(writer, value);
 
81
                }
 
82
        }
 
83
}
 
 
b'\\ No newline at end of file'