-Documentation
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
using Staff = Newtonsoft.Json.Tests.TestObjects.Employee;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DefaultSettings : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
// settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
Staff s = new Staff
|
||||
{
|
||||
FirstName = "Eric",
|
||||
LastName = "Example",
|
||||
BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
|
||||
Department = "IT",
|
||||
JobTitle = "Web Dude"
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(s);
|
||||
// {
|
||||
// "firstName": "Eric",
|
||||
// "lastName": "Example",
|
||||
// "birthDate": "1980-04-20T00:00:00Z",
|
||||
// "department": "IT",
|
||||
// "jobTitle": "Web Dude"
|
||||
// }
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""firstName"": ""Eric"",
|
||||
""lastName"": ""Example"",
|
||||
""birthDate"": ""1980-04-20T00:00:00Z"",
|
||||
""department"": ""IT"",
|
||||
""jobTitle"": ""Web Dude""
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeDateFormatString : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"[
|
||||
'7 December, 2009',
|
||||
'1 January, 2010',
|
||||
'10 February, 2010'
|
||||
]";
|
||||
|
||||
IList<DateTime> dateList = JsonConvert.DeserializeObject<IList<DateTime>>(json, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "d MMMM, yyyy"
|
||||
});
|
||||
|
||||
foreach (DateTime dateTime in dateList)
|
||||
{
|
||||
Console.WriteLine(dateTime.ToLongDateString());
|
||||
}
|
||||
// Monday, 07 December 2009
|
||||
// Friday, 01 January 2010
|
||||
// Wednesday, 10 February 2010
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(new DateTime(2009, 12, 7, 0, 0, 0, DateTimeKind.Utc), dateList[0]);
|
||||
Assert.AreEqual(new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Utc), dateList[1]);
|
||||
Assert.AreEqual(new DateTime(2010, 2, 10, 0, 0, 0, DateTimeKind.Utc), dateList[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeExtensionData : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class DirectoryAccount
|
||||
{
|
||||
// normal deserialization
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
// these properties are set in OnDeserialized
|
||||
public string UserName { get; set; }
|
||||
public string Domain { get; set; }
|
||||
|
||||
[JsonExtensionData]
|
||||
private IDictionary<string, JToken> _additionalData;
|
||||
|
||||
[OnDeserialized]
|
||||
private void OnDeserialized(StreamingContext context)
|
||||
{
|
||||
// SAMAccountName is not deserialized to any property
|
||||
// and so it is added to the extension data dictionary
|
||||
string samAccountName = (string)_additionalData["SAMAccountName"];
|
||||
|
||||
Domain = samAccountName.Split('\\')[0];
|
||||
UserName = samAccountName.Split('\\')[1];
|
||||
}
|
||||
|
||||
public DirectoryAccount()
|
||||
{
|
||||
_additionalData = new Dictionary<string, JToken>();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'DisplayName': 'John Smith',
|
||||
'SAMAccountName': 'contoso\\johns'
|
||||
}";
|
||||
|
||||
DirectoryAccount account = JsonConvert.DeserializeObject<DirectoryAccount>(json);
|
||||
|
||||
Console.WriteLine(account.DisplayName);
|
||||
// John Smith
|
||||
|
||||
Console.WriteLine(account.Domain);
|
||||
// contoso
|
||||
|
||||
Console.WriteLine(account.UserName);
|
||||
// johns
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual("John Smith", account.DisplayName);
|
||||
Assert.AreEqual("contoso", account.Domain);
|
||||
Assert.AreEqual("johns", account.UserName);
|
||||
}
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeImmutableCollections : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"[
|
||||
'One',
|
||||
'II',
|
||||
'3'
|
||||
]";
|
||||
|
||||
ImmutableList<string> l = JsonConvert.DeserializeObject<ImmutableList<string>>(json);
|
||||
|
||||
foreach (string s in l)
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
}
|
||||
// One
|
||||
// II
|
||||
// 3
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(3, l.Count);
|
||||
Assert.AreEqual("One", l[0]);
|
||||
Assert.AreEqual("II", l[1]);
|
||||
Assert.AreEqual("3", l[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using User = Newtonsoft.Json.Tests.Serialization.MetadataPropertyHandlingTests.User;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeMetadataPropertyHandling : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
try
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'Name': 'James',
|
||||
'Password': 'Password1',
|
||||
'$type': 'MyNamespace.User, MyAssembly'
|
||||
}";
|
||||
|
||||
object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
// $type no longer needs to be first
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
|
||||
User u = (User)o;
|
||||
|
||||
Console.WriteLine(u.Name);
|
||||
// James
|
||||
#endregion
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using LogService = Newtonsoft.Json.Tests.Serialization.LogManager;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeWithDependencyInjection : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class AutofacContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly IContainer _container;
|
||||
|
||||
public AutofacContractResolver(IContainer container)
|
||||
{
|
||||
_container = container;
|
||||
}
|
||||
|
||||
protected override JsonObjectContract CreateObjectContract(Type objectType)
|
||||
{
|
||||
JsonObjectContract contract = base.CreateObjectContract(objectType);
|
||||
|
||||
// use Autofac to create types that have been registered with it
|
||||
if (_container.IsRegistered(objectType))
|
||||
contract.DefaultCreator = () => _container.Resolve(objectType);
|
||||
|
||||
return contract;
|
||||
}
|
||||
}
|
||||
|
||||
public class TaskController
|
||||
{
|
||||
private readonly ITaskRepository _repository;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public TaskController(ITaskRepository repository, ILogger logger)
|
||||
{
|
||||
_repository = repository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public ITaskRepository Repository
|
||||
{
|
||||
get { return _repository; }
|
||||
}
|
||||
|
||||
public ILogger Logger
|
||||
{
|
||||
get { return _logger; }
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
builder.RegisterType<TaskRepository>().As<ITaskRepository>();
|
||||
builder.RegisterType<TaskController>();
|
||||
builder.Register(c => new LogService(new DateTime(2000, 12, 12))).As<ILogger>();
|
||||
|
||||
IContainer container = builder.Build();
|
||||
|
||||
AutofacContractResolver contractResolver = new AutofacContractResolver(container);
|
||||
|
||||
string json = @"{
|
||||
'Logger': {
|
||||
'Level':'Debug'
|
||||
}
|
||||
}";
|
||||
|
||||
// ITaskRespository and ILogger constructor parameters are injected by Autofac
|
||||
TaskController controller = JsonConvert.DeserializeObject<TaskController>(json, new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = contractResolver
|
||||
});
|
||||
|
||||
Console.WriteLine(controller.Repository.GetType().Name);
|
||||
// TaskRepository
|
||||
#endregion
|
||||
|
||||
Assert.IsNotNull(controller);
|
||||
Assert.IsNotNull(controller.Logger);
|
||||
|
||||
Assert.AreEqual(new DateTime(2000, 12, 12), controller.Logger.DateTime);
|
||||
Assert.AreEqual("Debug", controller.Logger.Level);
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeDateFormatString : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
IList<DateTime> dateList = new List<DateTime>
|
||||
{
|
||||
new DateTime(2009, 12, 7, 23, 10, 0, DateTimeKind.Utc),
|
||||
new DateTime(2010, 1, 1, 9, 0, 0, DateTimeKind.Utc),
|
||||
new DateTime(2010, 2, 10, 10, 0, 0, DateTimeKind.Utc)
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dateList, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "d MMMM, yyyy",
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
|
||||
Console.WriteLine(json);
|
||||
// [
|
||||
// "7 December, 2009",
|
||||
// "1 January, 2010",
|
||||
// "10 February, 2010"
|
||||
// ]
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
""7 December, 2009"",
|
||||
""1 January, 2010"",
|
||||
""10 February, 2010""
|
||||
]", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeExtensionData : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class CustomerInvoice
|
||||
{
|
||||
// we're only modifing the tax rate
|
||||
public decimal TaxRate { get; set; }
|
||||
|
||||
// everything else gets stored here
|
||||
[JsonExtensionData]
|
||||
private IDictionary<string, JToken> _additionalData;
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'HourlyRate': 150,
|
||||
'Hours': 40,
|
||||
'TaxRate': 0.125
|
||||
}";
|
||||
|
||||
CustomerInvoice invoice = JsonConvert.DeserializeObject<CustomerInvoice>(json);
|
||||
|
||||
// increase tax to 15%
|
||||
invoice.TaxRate = 0.15m;
|
||||
|
||||
string result = JsonConvert.SerializeObject(invoice);
|
||||
// {
|
||||
// "TaxRate": 0.15,
|
||||
// "HourlyRate": 150,
|
||||
// "Hours": 40
|
||||
// }
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{""TaxRate"":0.15,""HourlyRate"":150,""Hours"":40}", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeImmutableCollections : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
ImmutableList<string> l = ImmutableList.CreateRange(new List<string>
|
||||
{
|
||||
"One",
|
||||
"II",
|
||||
"3"
|
||||
});
|
||||
|
||||
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
|
||||
// [
|
||||
// "One",
|
||||
// "II",
|
||||
// "3"
|
||||
// ]
|
||||
#endregion
|
||||
|
||||
StringAssert.AreEqual(@"[
|
||||
""One"",
|
||||
""II"",
|
||||
""3""
|
||||
]", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user