2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
26
#if !(NET35 || NET20 || PORTABLE)
28
using System.Collections.Generic;
29
using System.ComponentModel;
34
using System.Runtime.Serialization;
36
using System.Threading.Tasks;
37
using Newtonsoft.Json.Converters;
38
using Newtonsoft.Json.Linq;
40
using NUnit.Framework;
42
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
43
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
44
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
46
using Newtonsoft.Json.Serialization;
47
using Newtonsoft.Json.Tests.TestObjects;
48
using Newtonsoft.Json.Utilities;
49
using System.Globalization;
51
namespace Newtonsoft.Json.Tests.Documentation
53
public class HttpClient
55
public Task<string> GetStringAsync(string requestUri)
60
public Task<Stream> GetStreamAsync(string requestUri)
66
#region JsonConverterAttribute
67
[JsonConverter(typeof(PersonConverter))]
72
Likes = new List<string>();
75
public string Name { get; set; }
76
public IList<string> Likes { get; private set; }
80
#region JsonConverterContractResolver
81
public class ConverterContractResolver : DefaultContractResolver
83
public new static readonly ConverterContractResolver Instance = new ConverterContractResolver();
85
protected override JsonContract CreateContract(Type objectType)
87
JsonContract contract = base.CreateContract(objectType);
89
// this will only be called once and then cached
90
if (objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset))
91
contract.Converter = new JavaScriptDateTimeConverter();
98
public class PersonConverter : JsonConverter
100
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
104
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
109
public override bool CanConvert(Type objectType)
111
return (objectType == typeof(Person));
116
public class PerformanceTests : TestFixtureBase
119
public void ConverterContractResolverTest()
121
string json = JsonConvert.SerializeObject(new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc), new JsonSerializerSettings
123
ContractResolver = ConverterContractResolver.Instance
126
Console.WriteLine(json);
129
public void DeserializeString()
131
#region DeserializeString
132
HttpClient client = new HttpClient();
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;
138
Person p = JsonConvert.DeserializeObject<Person>(json);
142
public void DeserializeStream()
144
#region DeserializeStream
145
HttpClient client = new HttpClient();
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))
151
JsonSerializer serializer = new JsonSerializer();
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);
161
public static class PersonWriter
164
public static string ToJson(this Person p)
166
StringWriter sw = new StringWriter();
167
JsonTextWriter writer = new JsonTextWriter(sw);
170
writer.WriteStartObject();
173
writer.WritePropertyName("name");
174
writer.WriteValue(p.Name);
176
// "likes": ["Comedy", "Superman"]
177
writer.WritePropertyName("likes");
178
writer.WriteStartArray();
179
foreach (string like in p.Likes)
181
writer.WriteValue(like);
183
writer.WriteEndArray();
186
writer.WriteEndObject();
188
return sw.ToString();
192
public static Person ToPerson(this string s)
194
StringReader sr = new StringReader(s);
195
JsonTextReader reader = new JsonTextReader(sr);
197
Person p = new Person();
204
p.Name = reader.ReadAsString();
209
// "Comedy", "Superman", ]
210
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
212
p.Likes.Add((string)reader.Value);
b'\\ No newline at end of file'