~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Documentation/PerformanceTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
#if !(NET35 || NET20 || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using System.Dynamic;
 
31
using System.IO;
 
32
using System.Linq;
 
33
using System.Net;
 
34
using System.Runtime.Serialization;
 
35
using System.Text;
 
36
using System.Threading.Tasks;
 
37
using Newtonsoft.Json.Converters;
 
38
using Newtonsoft.Json.Linq;
 
39
#if !NETFX_CORE
 
40
using NUnit.Framework;
 
41
#else
 
42
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
43
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
44
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
45
#endif
 
46
using Newtonsoft.Json.Serialization;
 
47
using Newtonsoft.Json.Tests.TestObjects;
 
48
using Newtonsoft.Json.Utilities;
 
49
using System.Globalization;
 
50
 
 
51
namespace Newtonsoft.Json.Tests.Documentation
 
52
{
 
53
  public class HttpClient
 
54
  {
 
55
    public Task<string> GetStringAsync(string requestUri)
 
56
    {
 
57
      return null;
 
58
    }
 
59
 
 
60
    public Task<Stream> GetStreamAsync(string requestUri)
 
61
    {
 
62
      return null;
 
63
    }
 
64
  }
 
65
 
 
66
  #region JsonConverterAttribute
 
67
  [JsonConverter(typeof(PersonConverter))]
 
68
  public class Person
 
69
  {
 
70
    public Person()
 
71
    {
 
72
      Likes = new List<string>();
 
73
    }
 
74
 
 
75
    public string Name { get; set; }
 
76
    public IList<string> Likes { get; private set; }
 
77
  }
 
78
  #endregion
 
79
 
 
80
  #region JsonConverterContractResolver
 
81
  public class ConverterContractResolver : DefaultContractResolver
 
82
  {
 
83
    public new static readonly ConverterContractResolver Instance = new ConverterContractResolver();
 
84
 
 
85
    protected override JsonContract CreateContract(Type objectType)
 
86
    {
 
87
      JsonContract contract = base.CreateContract(objectType);
 
88
 
 
89
      // this will only be called once and then cached
 
90
      if (objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset))
 
91
        contract.Converter = new JavaScriptDateTimeConverter();
 
92
 
 
93
      return contract;
 
94
    }
 
95
  }
 
96
  #endregion
 
97
 
 
98
  public class PersonConverter : JsonConverter
 
99
  {
 
100
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 
101
    {
 
102
    }
 
103
 
 
104
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 
105
    {
 
106
      return null;
 
107
    }
 
108
 
 
109
    public override bool CanConvert(Type objectType)
 
110
    {
 
111
      return (objectType == typeof(Person));
 
112
    }
 
113
  }
 
114
 
 
115
  [TestFixture]
 
116
  public class PerformanceTests : TestFixtureBase
 
117
  {
 
118
    [Test]
 
119
    public void ConverterContractResolverTest()
 
120
    {
 
121
      string json = JsonConvert.SerializeObject(new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc), new JsonSerializerSettings
 
122
        {
 
123
          ContractResolver = ConverterContractResolver.Instance
 
124
        });
 
125
 
 
126
      Console.WriteLine(json);
 
127
    }
 
128
 
 
129
    public void DeserializeString()
 
130
    {
 
131
      #region DeserializeString
 
132
      HttpClient client = new HttpClient();
 
133
 
 
134
      // read the json into a string
 
135
      // string could potentially be very large and cause memory problems
 
136
      string json = client.GetStringAsync("http://www.test.com/large.json").Result;
 
137
 
 
138
      Person p = JsonConvert.DeserializeObject<Person>(json);
 
139
      #endregion
 
140
    }
 
141
 
 
142
    public void DeserializeStream()
 
143
    {
 
144
      #region DeserializeStream
 
145
      HttpClient client = new HttpClient();
 
146
 
 
147
      using (Stream s = client.GetStreamAsync("http://www.test.com/large.json").Result)
 
148
      using (StreamReader sr = new StreamReader(s))
 
149
      using (JsonReader reader = new JsonTextReader(sr))
 
150
      {
 
151
        JsonSerializer serializer = new JsonSerializer();
 
152
 
 
153
        // read the json from a stream
 
154
        // json size doesn't matter because only a small piece is read at a time from the HTTP request
 
155
        Person p = serializer.Deserialize<Person>(reader);
 
156
      }
 
157
      #endregion
 
158
    }
 
159
  }
 
160
 
 
161
  public static class PersonWriter
 
162
  {
 
163
    #region ReaderWriter
 
164
    public static string ToJson(this Person p)
 
165
    {
 
166
      StringWriter sw = new StringWriter();
 
167
      JsonTextWriter writer = new JsonTextWriter(sw);
 
168
 
 
169
      // {
 
170
      writer.WriteStartObject();
 
171
 
 
172
      // "name" : "Jerry"
 
173
      writer.WritePropertyName("name");
 
174
      writer.WriteValue(p.Name);
 
175
 
 
176
      // "likes": ["Comedy", "Superman"]
 
177
      writer.WritePropertyName("likes");
 
178
      writer.WriteStartArray();
 
179
      foreach (string like in p.Likes)
 
180
      {
 
181
        writer.WriteValue(like);
 
182
      }
 
183
      writer.WriteEndArray();
 
184
 
 
185
      // }
 
186
      writer.WriteEndObject();
 
187
 
 
188
      return sw.ToString();
 
189
    }
 
190
    #endregion
 
191
 
 
192
    public static Person ToPerson(this string s)
 
193
    {
 
194
      StringReader sr = new StringReader(s);
 
195
      JsonTextReader reader = new JsonTextReader(sr);
 
196
 
 
197
      Person p = new Person();
 
198
      
 
199
      // {
 
200
      reader.Read();
 
201
      // "name"
 
202
      reader.Read();
 
203
      // "Jerry"
 
204
      p.Name = reader.ReadAsString();
 
205
      // "likes"
 
206
      reader.Read();
 
207
      // [
 
208
      reader.Read();
 
209
      // "Comedy", "Superman", ]
 
210
      while (reader.Read() && reader.TokenType != JsonToken.EndArray)
 
211
      {
 
212
        p.Likes.Add((string)reader.Value);
 
213
      }
 
214
      // }
 
215
      reader.Read();
 
216
 
 
217
      return p;
 
218
    }
 
219
  }
 
220
}
 
221
#endif
 
 
b'\\ No newline at end of file'