-Changed serializer to better work with ICollection<T> when deserializing

-Improved serializer caching of static type data
This commit is contained in:
JamesNK
2008-09-08 08:46:23 +00:00
parent 7b0f3a7ccc
commit d193ae37be
11 changed files with 896 additions and 25 deletions
+242
View File
@@ -0,0 +1,242 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[assembly: global::System.Data.Objects.DataClasses.EdmSchemaAttribute()]
[assembly: global::System.Data.Objects.DataClasses.EdmRelationshipAttribute("EntityDataModel", "PurchasePurchaseLine", "Purchase", global::System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(Newtonsoft.Json.Tests.Purchase), "PurchaseLine", global::System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(Newtonsoft.Json.Tests.PurchaseLine))]
// Original file name:
// Generation date: 7/09/2008 7:58:08 p.m.
namespace Newtonsoft.Json.Tests
{
/// <summary>
/// There are no comments for EntityDataModelContainer in the schema.
/// </summary>
public partial class EntityDataModelContainer : global::System.Data.Objects.ObjectContext
{
/// <summary>
/// Initializes a new EntityDataModelContainer object using the connection string found in the 'EntityDataModelContainer' section of the application configuration file.
/// </summary>
public EntityDataModelContainer() :
base("name=EntityDataModelContainer", "EntityDataModelContainer")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new EntityDataModelContainer object.
/// </summary>
public EntityDataModelContainer(string connectionString) :
base(connectionString, "EntityDataModelContainer")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new EntityDataModelContainer object.
/// </summary>
public EntityDataModelContainer(global::System.Data.EntityClient.EntityConnection connection) :
base(connection, "EntityDataModelContainer")
{
this.OnContextCreated();
}
partial void OnContextCreated();
/// <summary>
/// There are no comments for PurchaseSet in the schema.
/// </summary>
public global::System.Data.Objects.ObjectQuery<Purchase> PurchaseSet
{
get
{
if ((this._PurchaseSet == null))
{
this._PurchaseSet = base.CreateQuery<Purchase>("[PurchaseSet]");
}
return this._PurchaseSet;
}
}
private global::System.Data.Objects.ObjectQuery<Purchase> _PurchaseSet;
/// <summary>
/// There are no comments for PurchaseLineSet in the schema.
/// </summary>
public global::System.Data.Objects.ObjectQuery<PurchaseLine> PurchaseLineSet
{
get
{
if ((this._PurchaseLineSet == null))
{
this._PurchaseLineSet = base.CreateQuery<PurchaseLine>("[PurchaseLineSet]");
}
return this._PurchaseLineSet;
}
}
private global::System.Data.Objects.ObjectQuery<PurchaseLine> _PurchaseLineSet;
/// <summary>
/// There are no comments for PurchaseSet in the schema.
/// </summary>
public void AddToPurchaseSet(Purchase purchase)
{
base.AddObject("PurchaseSet", purchase);
}
/// <summary>
/// There are no comments for PurchaseLineSet in the schema.
/// </summary>
public void AddToPurchaseLineSet(PurchaseLine purchaseLine)
{
base.AddObject("PurchaseLineSet", purchaseLine);
}
}
/// <summary>
/// There are no comments for EntityDataModel.Purchase in the schema.
/// </summary>
/// <KeyProperties>
/// Id
/// </KeyProperties>
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="EntityDataModel", Name="Purchase")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class Purchase : global::System.Data.Objects.DataClasses.EntityObject
{
/// <summary>
/// Create a new Purchase object.
/// </summary>
/// <param name="id">Initial value of Id.</param>
public static Purchase CreatePurchase(int id)
{
Purchase purchase = new Purchase();
purchase.Id = id;
return purchase;
}
/// <summary>
/// There are no comments for Property Id in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int Id
{
get
{
return this._Id;
}
set
{
this.OnIdChanging(value);
this.ReportPropertyChanging("Id");
this._Id = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
this.ReportPropertyChanged("Id");
this.OnIdChanged();
}
}
private int _Id;
partial void OnIdChanging(int value);
partial void OnIdChanged();
/// <summary>
/// There are no comments for PurchaseLine in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("EntityDataModel", "PurchasePurchaseLine", "PurchaseLine")]
[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public global::System.Data.Objects.DataClasses.EntityCollection<PurchaseLine> PurchaseLine
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<PurchaseLine>("EntityDataModel.PurchasePurchaseLine", "PurchaseLine");
}
set
{
if ((value != null))
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<PurchaseLine>("EntityDataModel.PurchasePurchaseLine", "PurchaseLine", value);
}
}
}
}
/// <summary>
/// There are no comments for EntityDataModel.PurchaseLine in the schema.
/// </summary>
/// <KeyProperties>
/// Id
/// </KeyProperties>
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="EntityDataModel", Name="PurchaseLine")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class PurchaseLine : global::System.Data.Objects.DataClasses.EntityObject
{
/// <summary>
/// Create a new PurchaseLine object.
/// </summary>
/// <param name="id">Initial value of Id.</param>
public static PurchaseLine CreatePurchaseLine(int id)
{
PurchaseLine purchaseLine = new PurchaseLine();
purchaseLine.Id = id;
return purchaseLine;
}
/// <summary>
/// There are no comments for Property Id in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int Id
{
get
{
return this._Id;
}
set
{
this.OnIdChanging(value);
this.ReportPropertyChanging("Id");
this._Id = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
this.ReportPropertyChanged("Id");
this.OnIdChanged();
}
}
private int _Id;
partial void OnIdChanging(int value);
partial void OnIdChanged();
/// <summary>
/// There are no comments for Purchase in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("EntityDataModel", "PurchasePurchaseLine", "Purchase")]
[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public Purchase Purchase
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Purchase>("EntityDataModel.PurchasePurchaseLine", "Purchase").Value;
}
set
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Purchase>("EntityDataModel.PurchasePurchaseLine", "Purchase").Value = value;
}
}
/// <summary>
/// There are no comments for Purchase in the schema.
/// </summary>
[global::System.ComponentModel.BrowsableAttribute(false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public global::System.Data.Objects.DataClasses.EntityReference<Purchase> PurchaseReference
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Purchase>("EntityDataModel.PurchasePurchaseLine", "Purchase");
}
set
{
if ((value != null))
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference<Purchase>("EntityDataModel.PurchasePurchaseLine", "Purchase", value);
}
}
}
}
}
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl" Namespace="EntityDataModel.Target" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005">
<EntityContainer Name="EntityDataModelTargetContainer" >
</EntityContainer>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="EntityDataModel" Alias="Self">
<EntityContainer Name="EntityDataModelContainer" >
<EntitySet Name="PurchaseSet" EntityType="EntityDataModel.Purchase" />
<EntitySet Name="PurchaseLineSet" EntityType="EntityDataModel.PurchaseLine" />
<AssociationSet Name="PurchasePurchaseLine" Association="EntityDataModel.PurchasePurchaseLine">
<End Role="Purchase" EntitySet="PurchaseSet" />
<End Role="PurchaseLine" EntitySet="PurchaseLineSet" /></AssociationSet></EntityContainer>
<EntityType Name="Purchase">
<Key>
<PropertyRef Name="Id" /></Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<NavigationProperty Name="PurchaseLine" Relationship="EntityDataModel.PurchasePurchaseLine" FromRole="Purchase" ToRole="PurchaseLine" /></EntityType>
<EntityType Name="PurchaseLine">
<Key>
<PropertyRef Name="Id" /></Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<NavigationProperty Name="Purchase" Relationship="EntityDataModel.PurchasePurchaseLine" FromRole="PurchaseLine" ToRole="Purchase" /></EntityType>
<Association Name="PurchasePurchaseLine">
<End Type="EntityDataModel.Purchase" Role="Purchase" Multiplicity="1" />
<End Type="EntityDataModel.PurchaseLine" Role="PurchaseLine" Multiplicity="*" /></Association></Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
<Alias Key="Model" Value="EntityDataModel" />
<Alias Key="Target" Value="EntityDataModel.Target" />
<EntityContainerMapping CdmEntityContainer="EntityDataModelContainer" StorageEntityContainer="EntityDataModelTargetContainer">
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="EntityDataModel" >
<EntityTypeShape EntityType="EntityDataModel.Purchase" Width="1.5" PointX="2.125" PointY="0.875" Height="1.211080729166667" />
<EntityTypeShape EntityType="EntityDataModel.PurchaseLine" Width="1.5" PointX="3.625" PointY="3" Height="1.0187792968749996" />
<AssociationConnector Association="EntityDataModel.PurchasePurchaseLine">
<ConnectorPoint PointX="2.84375" PointY="2.086080729166667" />
<ConnectorPoint PointX="2.84375" PointY="3.6055403645833333" />
<ConnectorPoint PointX="3.625" PointY="3.6055403645833333" /></AssociationConnector></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
@@ -28,11 +28,77 @@ using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Utilities;
using NUnit.Framework;
using System.IO;
using System.Linq;
namespace Newtonsoft.Json.Tests
{
public class JavaScriptConvertTest : TestFixtureBase
{
#if Entities
[Test]
public void EntitiesTest()
{
Purchase purchase = new Purchase() { Id = 1 };
purchase.PurchaseLine.Add(new PurchaseLine() { Id = 1, Purchase = purchase });
purchase.PurchaseLine.Add(new PurchaseLine() { Id = 2, Purchase = purchase });
StringWriter sw = new StringWriter();
JsonSerializer serializer = new JsonSerializer();
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
serializer.Serialize(jw, purchase);
}
string json = sw.ToString();
Assert.AreEqual(@"{
""Id"": 1,
""PurchaseLine"": [
{
""Id"": 1,
""PurchaseReference"": {
""EntityKey"": null,
""RelationshipName"": ""EntityDataModel.PurchasePurchaseLine"",
""SourceRoleName"": ""PurchaseLine"",
""TargetRoleName"": ""Purchase"",
""RelationshipSet"": null,
""IsLoaded"": false
},
""EntityState"": 1,
""EntityKey"": null
},
{
""Id"": 2,
""PurchaseReference"": {
""EntityKey"": null,
""RelationshipName"": ""EntityDataModel.PurchasePurchaseLine"",
""SourceRoleName"": ""PurchaseLine"",
""TargetRoleName"": ""Purchase"",
""RelationshipSet"": null,
""IsLoaded"": false
},
""EntityState"": 1,
""EntityKey"": null
}
],
""EntityState"": 1,
""EntityKey"": null
}", json);
Purchase newPurchase = JavaScriptConvert.DeserializeObject<Purchase>(json);
Assert.AreEqual(1, newPurchase.Id);
Assert.AreEqual(2, newPurchase.PurchaseLine.Count);
Assert.AreEqual(1, newPurchase.PurchaseLine.ElementAt(0).Id);
Assert.AreEqual(2, newPurchase.PurchaseLine.ElementAt(1).Id);
}
#endif
[Test]
public void EscapeJavaScriptString()
{
@@ -51,9 +51,13 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Data.Entity">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
@@ -84,6 +88,7 @@
<Compile Include="Linq\JTokenTests.cs" />
<Compile Include="Linq\JValueTests.cs" />
<Compile Include="Linq\LinqToJsonTest.cs" />
<Compile Include="PerformanceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SilverlightTests.cs" />
<Compile Include="TestFixtureBase.cs" />
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using System.IO;
using System.Web.Script.Serialization;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
{
/// <summary>
/// Summary description for JsonSerializerTest
/// </summary>
public class PerformanceTests : TestFixtureBase
{
private DateTime BaseDate = DateTime.Parse("01/01/2000");
//JSONSerializer ser = null;
public string SerializeJsonNet(object value)
{
Type type = value.GetType();
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
StringWriter sw = new StringWriter();
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
if (false)
writer.Formatting = Formatting.Indented;
else
writer.Formatting = Formatting.None;
writer.QuoteChar = '"';
json.Serialize(writer, value);
string output = sw.ToString();
writer.Close();
return output;
}
public string SerializeWebExtensions(object value)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
List<JavaScriptConverter> converters = new List<JavaScriptConverter>();
return ser.Serialize(value);
}
[Test]
public void PerformanceTest2()
{
TestClass test = new TestClass();
test.Address1.Address = "fff Street";
test.Address1.Entered = DateTime.Now.AddDays(20);
test.BigNumber = 34123123123.121M;
test.Now = DateTime.Now.AddHours(1);
test.strings = new List<string>() { "Rick", "Markus egger ][, (2nd)", "Kevin McNeish" };
cAddress address = new cAddress();
address.Entered = DateTime.Now.AddDays(-1);
address.Address = "array address";
test.Addresses.Add(address);
address = new cAddress();
address.Entered = DateTime.Now.AddDays(-2);
address.Address = "array 2 address";
test.Addresses.Add(address);
Stopwatch timed = new Stopwatch();
timed.Start();
string json = null;
for (int x = 0; x < 10000; x++)
{
json = this.SerializeJsonNet(test);
//json = this.SerializeWebExtensions(test);
}
timed.Stop();
Console.WriteLine("{0}", timed.ElapsedMilliseconds + " ms");
Console.WriteLine("{0}", json);
}
}
public class TestClass
{
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Name = "Rick";
public DateTime Now
{
get { return _Now; }
set { _Now = value; }
}
private DateTime _Now = DateTime.Now;
public decimal BigNumber
{
get { return _BigNumber; }
set { _BigNumber = value; }
}
private decimal _BigNumber = 1212121.22M;
public cAddress Address1
{
get { return _Address1; }
set { _Address1 = value; }
}
private cAddress _Address1 = new cAddress();
public List<cAddress> Addresses
{
get { return _Addresses; }
set { _Addresses = value; }
}
private List<cAddress> _Addresses = new List<cAddress>();
public List<string> strings = new List<string>() { "Rick", "Markus", "Kevin" };
public Dictionary<string, int> dictionary = new Dictionary<string, int> { { "Val asd1", 1 }, { "Val2", 3 }, { "Val3", 4 } };
}
public class cAddress
{
public string Address
{
get { return _Address; }
set { _Address = value; }
}
private string _Address = "32 Kaiea";
public string Phone
{
get { return _Phone; }
set { _Phone = value; }
}
private string _Phone = "(503) 814-6335";
public DateTime Entered
{
get { return _Entered; }
set { _Entered = value; }
}
private DateTime _Entered = DateTime.Parse("01/01/2007", CultureInfo.CurrentCulture.DateTimeFormat);
}
}