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 || SILVERLIGHT || NETFX_CORE || PORTABLE)
28
using System.Collections.Generic;
32
using Newtonsoft.Json.Converters;
34
using NUnit.Framework;
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
41
namespace Newtonsoft.Json.Tests.Serialization
44
public class EntitiesSerializationTests : TestFixtureBase
47
public void SerializeEntity()
49
Folder rootFolder = CreateEntitiesTestData();
51
string json = JsonConvert.SerializeObject(rootFolder, Formatting.Indented, new IsoDateTimeConverter());
55
""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
56
""Name"": ""Root folder"",
57
""Description"": ""Description!"",
58
""CreatedDate"": ""2000-12-10T10:50:00Z"",
63
""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
64
""Name"": ""Child folder"",
65
""Description"": ""Description!"",
66
""CreatedDate"": ""2001-11-20T10:50:00Z"",
70
""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
72
""Description"": ""Description!"",
73
""CreatedDate"": ""2002-10-30T10:50:00Z"",
79
""EntitySetName"": ""File"",
80
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
81
""EntityKeyValues"": [
84
""Type"": ""System.Guid"",
85
""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
97
""EntitySetName"": ""Folder"",
98
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
99
""EntityKeyValues"": [
101
""Key"": ""FolderId"",
102
""Type"": ""System.Guid"",
103
""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
109
""ParentFolder"": null,
112
""EntitySetName"": ""Folder"",
113
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
114
""EntityKeyValues"": [
116
""Key"": ""FolderId"",
117
""Type"": ""System.Guid"",
118
""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
124
Assert.AreEqual(expected, json);
128
public void DeserializeEntity()
132
""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
133
""Name"": ""Root folder"",
134
""Description"": ""Description!"",
135
""CreatedDate"": ""2000-12-10T10:50:00Z"",
140
""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
141
""Name"": ""Child folder"",
142
""Description"": ""Description!"",
143
""CreatedDate"": ""2001-11-20T10:50:00Z"",
147
""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
148
""Name"": ""File 1"",
149
""Description"": ""Description!"",
150
""CreatedDate"": ""2002-10-30T10:50:00Z"",
156
""EntitySetName"": ""File"",
157
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
158
""EntityKeyValues"": [
161
""Type"": ""System.Guid"",
162
""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
168
""ChildFolders"": [],
174
""EntitySetName"": ""Folder"",
175
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
176
""EntityKeyValues"": [
178
""Key"": ""FolderId"",
179
""Type"": ""System.Guid"",
180
""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
186
""ParentFolder"": null,
189
""EntitySetName"": ""Folder"",
190
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
191
""EntityKeyValues"": [
193
""Key"": ""FolderId"",
194
""Type"": ""System.Guid"",
195
""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
201
Folder f = JsonConvert.DeserializeObject<Folder>(json, new IsoDateTimeConverter());
204
Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.FolderId);
205
Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
206
Assert.AreEqual("DataServicesTestDatabaseEntities", f.EntityKey.EntityContainerName);
207
Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
208
Assert.AreEqual(false, f.EntityKey.IsTemporary);
209
Assert.AreEqual(1, f.EntityKey.EntityKeyValues.Length);
210
Assert.AreEqual("FolderId", f.EntityKey.EntityKeyValues[0].Key);
211
Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.EntityKey.EntityKeyValues[0].Value);
212
Assert.AreEqual("Root folder", f.Name);
213
Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), f.CreatedDate);
214
Assert.AreEqual(null, f.ParentFolder);
215
Assert.AreEqual(1, f.ChildFolders.Count);
217
Folder childFolder = f.ChildFolders.ElementAt(0);
219
Assert.AreEqual("Child folder", childFolder.Name);
220
Assert.AreEqual("Description!", childFolder.Description);
221
Assert.AreEqual(f, childFolder.ParentFolder);
222
Assert.AreEqual(f, childFolder.ParentFolderReference.Value);
223
// is this a problem?
224
Assert.AreEqual(null, childFolder.ParentFolderReference.EntityKey);
228
public void SerializeMultiValueEntityKey()
230
EntityKey e = new EntityKey("DataServicesTestDatabaseEntities.Folder",
231
new List<EntityKeyMember>
233
new EntityKeyMember("GuidId", new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E")),
234
new EntityKeyMember("IntId", int.MaxValue),
235
new EntityKeyMember("LongId", long.MaxValue),
236
new EntityKeyMember("StringId", "String!"),
237
new EntityKeyMember("DateTimeId", new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc))
240
string json = JsonConvert.SerializeObject(e, Formatting.Indented);
244
""EntitySetName"": ""Folder"",
245
""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
246
""EntityKeyValues"": [
249
""Type"": ""System.Guid"",
250
""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
254
""Type"": ""System.Int32"",
255
""Value"": ""2147483647""
259
""Type"": ""System.Int64"",
260
""Value"": ""9223372036854775807""
263
""Key"": ""StringId"",
264
""Type"": ""System.String"",
265
""Value"": ""String!""
268
""Key"": ""DateTimeId"",
269
""Type"": ""System.DateTime"",
270
""Value"": ""12/10/2000 10:50:00""
275
EntityKey newKey = JsonConvert.DeserializeObject<EntityKey>(json);
276
Assert.IsFalse(ReferenceEquals(e, newKey));
278
Assert.AreEqual(5, newKey.EntityKeyValues.Length);
279
Assert.AreEqual("GuidId", newKey.EntityKeyValues[0].Key);
280
Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), newKey.EntityKeyValues[0].Value);
281
Assert.AreEqual("IntId", newKey.EntityKeyValues[1].Key);
282
Assert.AreEqual(int.MaxValue, newKey.EntityKeyValues[1].Value);
283
Assert.AreEqual("LongId", newKey.EntityKeyValues[2].Key);
284
Assert.AreEqual(long.MaxValue, newKey.EntityKeyValues[2].Value);
285
Assert.AreEqual("StringId", newKey.EntityKeyValues[3].Key);
286
Assert.AreEqual("String!", newKey.EntityKeyValues[3].Value);
287
Assert.AreEqual("DateTimeId", newKey.EntityKeyValues[4].Key);
288
Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), newKey.EntityKeyValues[4].Value);
291
private Folder CreateEntitiesTestData()
293
Folder folder = new Folder();
294
folder.FolderId = new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E");
295
folder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", folder.FolderId);
296
folder.Name = "Root folder";
297
folder.Description = "Description!";
298
folder.CreatedDate = new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc);
300
Folder childFolder = new Folder();
301
childFolder.FolderId = new Guid("484936E2-7CBB-4592-93FF-B2103E5705E4");
302
childFolder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", childFolder.FolderId);
303
childFolder.Name = "Child folder";
304
childFolder.Description = "Description!";
305
childFolder.CreatedDate = new DateTime(2001, 11, 20, 10, 50, 0, DateTimeKind.Utc);
307
folder.ChildFolders.Add(childFolder);
309
File file1 = new File();
310
file1.FileId = new Guid("CC76D734-49F1-4616-BB38-41514228AC6C");
311
file1.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.File", "FileId", file1.FileId);
312
file1.Name = "File 1";
313
file1.Description = "Description!";
314
file1.CreatedDate = new DateTime(2002, 10, 30, 10, 50, 0, DateTimeKind.Utc);
316
childFolder.Files.Add(file1);
b'\\ No newline at end of file'