~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/ConvertingJsonAndXmlTests.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 System.Xml;
 
36
using Newtonsoft.Json.Converters;
 
37
using Newtonsoft.Json.Linq;
 
38
#if !NETFX_CORE
 
39
using NUnit.Framework;
 
40
#else
 
41
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
42
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
43
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
44
#endif
 
45
using Newtonsoft.Json.Serialization;
 
46
using Newtonsoft.Json.Tests.TestObjects;
 
47
using Newtonsoft.Json.Utilities;
 
48
using System.Globalization;
 
49
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
 
50
 
 
51
namespace Newtonsoft.Json.Tests.Documentation
 
52
{
 
53
  public class ConvertingJsonAndXmlTests
 
54
  {
 
55
    public void SerializeXmlNode()
 
56
    {
 
57
      #region SerializeXmlNode
 
58
      string xml = @"<?xml version='1.0' standalone='no'?>
 
59
      <root>
 
60
        <person id='1'>
 
61
        <name>Alan</name>
 
62
        <url>http://www.google.com</url>
 
63
        </person>
 
64
        <person id='2'>
 
65
        <name>Louis</name>
 
66
        <url>http://www.yahoo.com</url>
 
67
        </person>
 
68
      </root>";
 
69
 
 
70
      XmlDocument doc = new XmlDocument();
 
71
      doc.LoadXml(xml);
 
72
 
 
73
      string jsonText = JsonConvert.SerializeXmlNode(doc);
 
74
      //{
 
75
      //  "?xml": {
 
76
      //    "@version": "1.0",
 
77
      //    "@standalone": "no"
 
78
      //  },
 
79
      //  "root": {
 
80
      //    "person": [
 
81
      //      {
 
82
      //        "@id": "1",
 
83
      //        "name": "Alan",
 
84
      //        "url": "http://www.google.com"
 
85
      //      },
 
86
      //      {
 
87
      //        "@id": "2",
 
88
      //        "name": "Louis",
 
89
      //        "url": "http://www.yahoo.com"
 
90
      //      }
 
91
      //    ]
 
92
      //  }
 
93
      //}
 
94
      #endregion
 
95
    }
 
96
 
 
97
    public void DeserializeXmlNode()
 
98
    {
 
99
      #region DeserializeXmlNode
 
100
      string json = @"{
 
101
        '?xml': {
 
102
          '@version': '1.0',
 
103
          '@standalone': 'no'
 
104
        },
 
105
        'root': {
 
106
          'person': [
 
107
            {
 
108
              '@id': '1',
 
109
              'name': 'Alan',
 
110
              'url': 'http://www.google.com'
 
111
            },
 
112
            {
 
113
              '@id': '2',
 
114
              'name': 'Louis',
 
115
              'url': 'http://www.yahoo.com'
 
116
            }
 
117
          ]
 
118
        }
 
119
      }";
 
120
 
 
121
      XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
 
122
      // <?xml version="1.0" standalone="no"?>
 
123
      // <root>
 
124
      //   <person id="1">
 
125
      //   <name>Alan</name>
 
126
      //   <url>http://www.google.com</url>
 
127
      //   </person>
 
128
      //   <person id="2">
 
129
      //   <name>Louis</name>
 
130
      //   <url>http://www.yahoo.com</url>
 
131
      //   </person>
 
132
      // </root>
 
133
      #endregion
 
134
    }
 
135
 
 
136
    public void ForceJsonArray()
 
137
    {
 
138
      #region ForceJsonArray
 
139
      string xml = @"<person id='1'>
 
140
                          <name>Alan</name>
 
141
                          <url>http://www.google.com</url>
 
142
                          <role>Admin1</role>
 
143
                        </person>";
 
144
 
 
145
      XmlDocument doc = new XmlDocument();
 
146
      doc.LoadXml(xml);
 
147
 
 
148
      string json = JsonConvert.SerializeXmlNode(doc);
 
149
      //{
 
150
      //  "person": {
 
151
      //    "@id": "1",
 
152
      //    "name": "Alan",
 
153
      //    "url": "http://www.google.com",
 
154
      //    "role": "Admin1"
 
155
      //  }
 
156
      //}
 
157
 
 
158
      xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
 
159
                          <name>Alan</name>
 
160
                          <url>http://www.google.com</url>
 
161
                          <role json:Array='true'>Admin</role>
 
162
                        </person>";
 
163
 
 
164
      doc = new XmlDocument();
 
165
      doc.LoadXml(xml);
 
166
 
 
167
      json = JsonConvert.SerializeXmlNode(doc);
 
168
      //{
 
169
      //  "person": {
 
170
      //    "@id": "1",
 
171
      //    "name": "Alan",
 
172
      //    "url": "http://www.google.com",
 
173
      //    "role": [
 
174
      //      "Admin"
 
175
      //    ]
 
176
      //  }
 
177
      //}
 
178
      #endregion
 
179
    }
 
180
 
 
181
  }
 
182
}
 
183
#endif
 
 
b'\\ No newline at end of file'