~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/ReadingAndWritingJsonTests.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.Runtime.Serialization;
 
34
using System.Text;
 
35
using Newtonsoft.Json.Converters;
 
36
using Newtonsoft.Json.Linq;
 
37
#if !NETFX_CORE
 
38
using NUnit.Framework;
 
39
#else
 
40
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
41
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
42
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
43
#endif
 
44
using Newtonsoft.Json.Serialization;
 
45
using Newtonsoft.Json.Tests.TestObjects;
 
46
using Newtonsoft.Json.Utilities;
 
47
using System.Globalization;
 
48
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
 
49
 
 
50
namespace Newtonsoft.Json.Tests.Documentation
 
51
{
 
52
  [TestFixture]
 
53
  public class ReadingAndWritingJsonTests : TestFixtureBase
 
54
  {
 
55
    public void ReadingAndWritingJsonText()
 
56
    {
 
57
      #region ReadingAndWritingJsonText
 
58
      StringBuilder sb = new StringBuilder();
 
59
      StringWriter sw = new StringWriter(sb);
 
60
 
 
61
      using (JsonWriter writer = new JsonTextWriter(sw))
 
62
      {
 
63
        writer.Formatting = Formatting.Indented;
 
64
 
 
65
        writer.WriteStartObject();
 
66
        writer.WritePropertyName("CPU");
 
67
        writer.WriteValue("Intel");
 
68
        writer.WritePropertyName("PSU");
 
69
        writer.WriteValue("500W");
 
70
        writer.WritePropertyName("Drives");
 
71
        writer.WriteStartArray();
 
72
        writer.WriteValue("DVD read/writer");
 
73
        writer.WriteComment("(broken)");
 
74
        writer.WriteValue("500 gigabyte hard drive");
 
75
        writer.WriteValue("200 gigabype hard drive");
 
76
        writer.WriteEnd();
 
77
        writer.WriteEndObject();
 
78
      }
 
79
 
 
80
      // {
 
81
      //   "CPU": "Intel",
 
82
      //   "PSU": "500W",
 
83
      //   "Drives": [
 
84
      //     "DVD read/writer"
 
85
      //     /*(broken)*/,
 
86
      //     "500 gigabyte hard drive",
 
87
      //     "200 gigabype hard drive"
 
88
      //   ]
 
89
      // }
 
90
      #endregion
 
91
    }
 
92
 
 
93
    [Test]
 
94
    public void ReadingJsonText()
 
95
    {
 
96
      #region ReadingJsonText
 
97
      string json = @"{
 
98
         'CPU': 'Intel',
 
99
         'PSU': '500W',
 
100
         'Drives': [
 
101
           'DVD read/writer'
 
102
           /*(broken)*/,
 
103
           '500 gigabyte hard drive',
 
104
           '200 gigabype hard drive'
 
105
         ]
 
106
      }";
 
107
 
 
108
      JsonTextReader reader = new JsonTextReader(new StringReader(json));
 
109
      while (reader.Read())
 
110
      {
 
111
        if (reader.Value != null)
 
112
          Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
 
113
        else
 
114
          Console.WriteLine("Token: {0}", reader.TokenType);
 
115
      }
 
116
 
 
117
      // Token: StartObject
 
118
      // Token: PropertyName, Value: CPU
 
119
      // Token: String, Value: Intel
 
120
      // Token: PropertyName, Value: PSU
 
121
      // Token: String, Value: 500W
 
122
      // Token: PropertyName, Value: Drives
 
123
      // Token: StartArray
 
124
      // Token: String, Value: DVD read/writer
 
125
      // Token: Comment, Value: (broken)
 
126
      // Token: String, Value: 500 gigabyte hard drive
 
127
      // Token: String, Value: 200 gigabype hard drive
 
128
      // Token: EndArray
 
129
      // Token: EndObject
 
130
      #endregion
 
131
    }
 
132
 
 
133
    public void ReadingAndWritingJsonLinq()
 
134
    {
 
135
      #region ReadingAndWritingJsonLinq
 
136
      JObject o = new JObject(
 
137
        new JProperty("Name", "John Smith"),
 
138
        new JProperty("BirthDate", new DateTime(1983, 3, 20))
 
139
        );
 
140
 
 
141
      JsonSerializer serializer = new JsonSerializer();
 
142
      Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person));
 
143
 
 
144
      Console.WriteLine(p.Name);
 
145
      // John Smith
 
146
      #endregion
 
147
    }
 
148
  }
 
149
}
 
150
#endif
 
 
b'\\ No newline at end of file'