-Added MetadataTypeAttribute support when searching for attributes

-Tests around serializing/deserializing LINQ to SQL classes
This commit is contained in:
JamesNK
2009-04-26 07:11:30 +00:00
parent affb9ec906
commit 6e205383c7
13 changed files with 1149 additions and 4 deletions
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.LinqToSql
{
[MetadataType(typeof(DepartmentMetadata))]
public partial class Department
{
[JsonConverter(typeof(DepartmentConverter))]
public class DepartmentMetadata
{
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.LinqToSql
{
public class DepartmentConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value)
{
Department department = (Department)value;
JObject o = new JObject();
o["DepartmentId"] = new JValue(department.DepartmentId.ToString());
o["Name"] = new JValue(new string(department.Name.Reverse().ToArray()));
o.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType)
{
JObject o = JObject.Load(reader);
Department department = new Department();
department.DepartmentId = new Guid((string)o["DepartmentId"]);
department.Name = new string(((string) o["Name"]).Reverse().ToArray());
return department;
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof (Department));
}
}
}
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.LinqToSql
{
public class GuidByteArrayConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value)
{
Guid guid = (Guid) value;
writer.WriteValue(Convert.ToBase64String(guid.ToByteArray()));
}
public override object ReadJson(JsonReader reader, Type objectType)
{
string encodedData = (string) reader.Value;
byte[] data = Convert.FromBase64String(encodedData);
return new Guid(data);
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof (Guid));
}
}
}
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Database Class="LinqToSqlClassesDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
<Table Name="" Member="Persons">
<Type Name="Person">
<Column Name="FirstName" Type="System.String" CanBeNull="false" />
<Column Name="LastName" Type="System.String" CanBeNull="false" />
<Column Name="PersonId" Type="System.Guid" IsPrimaryKey="true" CanBeNull="false" />
<Column Member="DepartmentId" Type="System.Guid" CanBeNull="false" />
<Association Name="Person_PersonRole" Member="PersonRoles" ThisKey="PersonId" OtherKey="PersonId" Type="PersonRole" />
<Association Name="Department_Person" Member="Department" ThisKey="DepartmentId" OtherKey="DepartmentId" Type="Department" IsForeignKey="true" />
</Type>
</Table>
<Table Name="" Member="Roles">
<Type Name="Role">
<Column Name="Name" Type="System.String" CanBeNull="false" />
<Column Name="RoleId" Type="System.Guid" IsPrimaryKey="true" CanBeNull="false" />
<Association Name="Role_PersonRole" Member="PersonRoles" ThisKey="RoleId" OtherKey="RoleId" Type="PersonRole" />
</Type>
</Table>
<Table Name="" Member="PersonRoles">
<Type Name="PersonRole">
<Column Name="PersonId" Type="System.Guid" CanBeNull="false" />
<Column Name="RoleId" Type="System.Guid" CanBeNull="false" />
<Column Name="PersonRoleId" Type="System.Guid" IsPrimaryKey="true" CanBeNull="false" />
<Association Name="Person_PersonRole" Member="Person" ThisKey="PersonId" OtherKey="PersonId" Type="Person" IsForeignKey="true" />
<Association Name="Role_PersonRole" Member="Role" ThisKey="RoleId" OtherKey="RoleId" Type="Role" IsForeignKey="true" />
</Type>
</Table>
<Table Name="" Member="Departments">
<Type Name="Department">
<Column Name="DepartmentId" Type="System.Guid" IsPrimaryKey="true" CanBeNull="false" />
<Column Member="Name" Type="System.String" CanBeNull="false" />
<Association Name="Department_Person" Member="Persons" ThisKey="DepartmentId" OtherKey="DepartmentId" Type="Person" />
</Type>
</Table>
</Database>
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<ordesignerObjectsDiagram dslVersion="1.0.0.0" absoluteBounds="0, 0, 11, 8.5" name="LinqToSqlClasses">
<DataContextMoniker Name="/LinqToSqlClassesDataContext" />
<nestedChildShapes>
<classShape Id="be91ec15-4a64-4976-9358-2e01309ef584" absoluteBounds="0.875, 1.875, 2, 1.5785953776041666">
<DataClassMoniker Name="/LinqToSqlClassesDataContext/Person" />
<nestedChildShapes>
<elementListCompartment Id="e16e186c-b29e-45ca-a2ab-93cb3de05143" absoluteBounds="0.8899999999999999, 2.335, 1.9700000000000002, 1.0185953776041665" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
</nestedChildShapes>
</classShape>
<classShape Id="b1b861bb-382c-4c65-bdb1-7a3e2df873a2" absoluteBounds="3.625, 4.625, 2, 1.1939925130208333">
<DataClassMoniker Name="/LinqToSqlClassesDataContext/Role" />
<nestedChildShapes>
<elementListCompartment Id="11d870c0-0ec3-4fba-bb69-43a96fa05670" absoluteBounds="3.6399999999999997, 5.085, 1.9700000000000002, 0.63399251302083326" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
</nestedChildShapes>
</classShape>
<classShape Id="1eb138a4-9f12-4a20-b6b7-d1e23984b857" absoluteBounds="3.75, 2.25, 2, 1.3862939453125">
<DataClassMoniker Name="/LinqToSqlClassesDataContext/PersonRole" />
<nestedChildShapes>
<elementListCompartment Id="6865be45-c748-47ff-be37-534064fc2e80" absoluteBounds="3.7649999999999997, 2.71, 1.9700000000000002, 0.8262939453125" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
</nestedChildShapes>
</classShape>
<associationConnector edgePoints="[(2.875 : 2.85179768880208); (3.75 : 2.85179768880208)]" fixedFrom="Algorithm" fixedTo="Algorithm">
<AssociationMoniker Name="/LinqToSqlClassesDataContext/Person/Person_PersonRole" />
<nodes>
<classShapeMoniker Id="be91ec15-4a64-4976-9358-2e01309ef584" />
<classShapeMoniker Id="1eb138a4-9f12-4a20-b6b7-d1e23984b857" />
</nodes>
</associationConnector>
<associationConnector edgePoints="[(4.6875 : 4.625); (4.6875 : 3.6362939453125)]" fixedFrom="NotFixed" fixedTo="NotFixed">
<AssociationMoniker Name="/LinqToSqlClassesDataContext/Role/Role_PersonRole" />
<nodes>
<classShapeMoniker Id="b1b861bb-382c-4c65-bdb1-7a3e2df873a2" />
<classShapeMoniker Id="1eb138a4-9f12-4a20-b6b7-d1e23984b857" />
</nodes>
</associationConnector>
<classShape Id="b96fa86b-7f23-489f-8eb2-44ca2af1551b" absoluteBounds="0.875, 4.5, 2, 1.1939925130208327">
<DataClassMoniker Name="/LinqToSqlClassesDataContext/Department" />
<nestedChildShapes>
<elementListCompartment Id="46cdd558-04c5-4c77-82ce-fe92cda427fe" absoluteBounds="0.89, 4.96, 1.9700000000000002, 0.63399251302083326" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
</nestedChildShapes>
</classShape>
<associationConnector edgePoints="[(1.875 : 4.5); (1.875 : 3.45359537760417)]" fixedFrom="Algorithm" fixedTo="Algorithm">
<AssociationMoniker Name="/LinqToSqlClassesDataContext/Department/Department_Person" />
<nodes>
<classShapeMoniker Id="b96fa86b-7f23-489f-8eb2-44ca2af1551b" />
<classShapeMoniker Id="be91ec15-4a64-4976-9358-2e01309ef584" />
</nodes>
</associationConnector>
</nestedChildShapes>
</ordesignerObjectsDiagram>
@@ -0,0 +1,726 @@
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3074
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Newtonsoft.Json.Tests.LinqToSql
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
public partial class LinqToSqlClassesDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertPerson(Person instance);
partial void UpdatePerson(Person instance);
partial void DeletePerson(Person instance);
partial void InsertRole(Role instance);
partial void UpdateRole(Role instance);
partial void DeleteRole(Role instance);
partial void InsertPersonRole(PersonRole instance);
partial void UpdatePersonRole(PersonRole instance);
partial void DeletePersonRole(PersonRole instance);
partial void InsertDepartment(Department instance);
partial void UpdateDepartment(Department instance);
partial void DeleteDepartment(Department instance);
#endregion
public LinqToSqlClassesDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public LinqToSqlClassesDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public LinqToSqlClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public LinqToSqlClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Person> Persons
{
get
{
return this.GetTable<Person>();
}
}
public System.Data.Linq.Table<Role> Roles
{
get
{
return this.GetTable<Role>();
}
}
public System.Data.Linq.Table<PersonRole> PersonRoles
{
get
{
return this.GetTable<PersonRole>();
}
}
public System.Data.Linq.Table<Department> Departments
{
get
{
return this.GetTable<Department>();
}
}
}
[Table(Name="")]
public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private string _FirstName;
private string _LastName;
private System.Guid _PersonId;
private System.Guid _DepartmentId;
private EntitySet<PersonRole> _PersonRoles;
private EntityRef<Department> _Department;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnFirstNameChanging(string value);
partial void OnFirstNameChanged();
partial void OnLastNameChanging(string value);
partial void OnLastNameChanged();
partial void OnPersonIdChanging(System.Guid value);
partial void OnPersonIdChanged();
partial void OnDepartmentIdChanging(System.Guid value);
partial void OnDepartmentIdChanged();
#endregion
public Person()
{
this._PersonRoles = new EntitySet<PersonRole>(new Action<PersonRole>(this.attach_PersonRoles), new Action<PersonRole>(this.detach_PersonRoles));
this._Department = default(EntityRef<Department>);
OnCreated();
}
[Column(Storage="_FirstName", CanBeNull=false)]
public string FirstName
{
get
{
return this._FirstName;
}
set
{
if ((this._FirstName != value))
{
this.OnFirstNameChanging(value);
this.SendPropertyChanging();
this._FirstName = value;
this.SendPropertyChanged("FirstName");
this.OnFirstNameChanged();
}
}
}
[Column(Storage="_LastName", CanBeNull=false)]
public string LastName
{
get
{
return this._LastName;
}
set
{
if ((this._LastName != value))
{
this.OnLastNameChanging(value);
this.SendPropertyChanging();
this._LastName = value;
this.SendPropertyChanged("LastName");
this.OnLastNameChanged();
}
}
}
[Column(Storage="_PersonId", IsPrimaryKey=true)]
public System.Guid PersonId
{
get
{
return this._PersonId;
}
set
{
if ((this._PersonId != value))
{
this.OnPersonIdChanging(value);
this.SendPropertyChanging();
this._PersonId = value;
this.SendPropertyChanged("PersonId");
this.OnPersonIdChanged();
}
}
}
[Column(Storage="_DepartmentId")]
public System.Guid DepartmentId
{
get
{
return this._DepartmentId;
}
set
{
if ((this._DepartmentId != value))
{
if (this._Department.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnDepartmentIdChanging(value);
this.SendPropertyChanging();
this._DepartmentId = value;
this.SendPropertyChanged("DepartmentId");
this.OnDepartmentIdChanged();
}
}
}
[Association(Name="Person_PersonRole", Storage="_PersonRoles", ThisKey="PersonId", OtherKey="PersonId")]
public EntitySet<PersonRole> PersonRoles
{
get
{
return this._PersonRoles;
}
set
{
this._PersonRoles.Assign(value);
}
}
[Association(Name="Department_Person", Storage="_Department", ThisKey="DepartmentId", OtherKey="DepartmentId", IsForeignKey=true)]
public Department Department
{
get
{
return this._Department.Entity;
}
set
{
Department previousValue = this._Department.Entity;
if (((previousValue != value)
|| (this._Department.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Department.Entity = null;
previousValue.Persons.Remove(this);
}
this._Department.Entity = value;
if ((value != null))
{
value.Persons.Add(this);
this._DepartmentId = value.DepartmentId;
}
else
{
this._DepartmentId = default(System.Guid);
}
this.SendPropertyChanged("Department");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_PersonRoles(PersonRole entity)
{
this.SendPropertyChanging();
entity.Person = this;
}
private void detach_PersonRoles(PersonRole entity)
{
this.SendPropertyChanging();
entity.Person = null;
}
}
[Table(Name="")]
public partial class Role : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private string _Name;
private System.Guid _RoleId;
private EntitySet<PersonRole> _PersonRoles;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnNameChanging(string value);
partial void OnNameChanged();
partial void OnRoleIdChanging(System.Guid value);
partial void OnRoleIdChanged();
#endregion
public Role()
{
this._PersonRoles = new EntitySet<PersonRole>(new Action<PersonRole>(this.attach_PersonRoles), new Action<PersonRole>(this.detach_PersonRoles));
OnCreated();
}
[Column(Storage="_Name", CanBeNull=false)]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name != value))
{
this.OnNameChanging(value);
this.SendPropertyChanging();
this._Name = value;
this.SendPropertyChanged("Name");
this.OnNameChanged();
}
}
}
[Column(Storage="_RoleId", IsPrimaryKey=true)]
public System.Guid RoleId
{
get
{
return this._RoleId;
}
set
{
if ((this._RoleId != value))
{
this.OnRoleIdChanging(value);
this.SendPropertyChanging();
this._RoleId = value;
this.SendPropertyChanged("RoleId");
this.OnRoleIdChanged();
}
}
}
[Association(Name="Role_PersonRole", Storage="_PersonRoles", ThisKey="RoleId", OtherKey="RoleId")]
public EntitySet<PersonRole> PersonRoles
{
get
{
return this._PersonRoles;
}
set
{
this._PersonRoles.Assign(value);
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_PersonRoles(PersonRole entity)
{
this.SendPropertyChanging();
entity.Role = this;
}
private void detach_PersonRoles(PersonRole entity)
{
this.SendPropertyChanging();
entity.Role = null;
}
}
[Table(Name="")]
public partial class PersonRole : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Guid _PersonId;
private System.Guid _RoleId;
private System.Guid _PersonRoleId;
private EntityRef<Person> _Person;
private EntityRef<Role> _Role;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnPersonIdChanging(System.Guid value);
partial void OnPersonIdChanged();
partial void OnRoleIdChanging(System.Guid value);
partial void OnRoleIdChanged();
partial void OnPersonRoleIdChanging(System.Guid value);
partial void OnPersonRoleIdChanged();
#endregion
public PersonRole()
{
this._Person = default(EntityRef<Person>);
this._Role = default(EntityRef<Role>);
OnCreated();
}
[Column(Storage="_PersonId")]
public System.Guid PersonId
{
get
{
return this._PersonId;
}
set
{
if ((this._PersonId != value))
{
if (this._Person.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnPersonIdChanging(value);
this.SendPropertyChanging();
this._PersonId = value;
this.SendPropertyChanged("PersonId");
this.OnPersonIdChanged();
}
}
}
[Column(Storage="_RoleId")]
public System.Guid RoleId
{
get
{
return this._RoleId;
}
set
{
if ((this._RoleId != value))
{
if (this._Role.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnRoleIdChanging(value);
this.SendPropertyChanging();
this._RoleId = value;
this.SendPropertyChanged("RoleId");
this.OnRoleIdChanged();
}
}
}
[Column(Storage="_PersonRoleId", IsPrimaryKey=true)]
public System.Guid PersonRoleId
{
get
{
return this._PersonRoleId;
}
set
{
if ((this._PersonRoleId != value))
{
this.OnPersonRoleIdChanging(value);
this.SendPropertyChanging();
this._PersonRoleId = value;
this.SendPropertyChanged("PersonRoleId");
this.OnPersonRoleIdChanged();
}
}
}
[Association(Name="Person_PersonRole", Storage="_Person", ThisKey="PersonId", OtherKey="PersonId", IsForeignKey=true)]
public Person Person
{
get
{
return this._Person.Entity;
}
set
{
Person previousValue = this._Person.Entity;
if (((previousValue != value)
|| (this._Person.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Person.Entity = null;
previousValue.PersonRoles.Remove(this);
}
this._Person.Entity = value;
if ((value != null))
{
value.PersonRoles.Add(this);
this._PersonId = value.PersonId;
}
else
{
this._PersonId = default(System.Guid);
}
this.SendPropertyChanged("Person");
}
}
}
[Association(Name="Role_PersonRole", Storage="_Role", ThisKey="RoleId", OtherKey="RoleId", IsForeignKey=true)]
public Role Role
{
get
{
return this._Role.Entity;
}
set
{
Role previousValue = this._Role.Entity;
if (((previousValue != value)
|| (this._Role.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Role.Entity = null;
previousValue.PersonRoles.Remove(this);
}
this._Role.Entity = value;
if ((value != null))
{
value.PersonRoles.Add(this);
this._RoleId = value.RoleId;
}
else
{
this._RoleId = default(System.Guid);
}
this.SendPropertyChanged("Role");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
[Table(Name="")]
public partial class Department : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Guid _DepartmentId;
private string _Name;
private EntitySet<Person> _Persons;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnDepartmentIdChanging(System.Guid value);
partial void OnDepartmentIdChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
#endregion
public Department()
{
this._Persons = new EntitySet<Person>(new Action<Person>(this.attach_Persons), new Action<Person>(this.detach_Persons));
OnCreated();
}
[Column(Storage="_DepartmentId", IsPrimaryKey=true)]
public System.Guid DepartmentId
{
get
{
return this._DepartmentId;
}
set
{
if ((this._DepartmentId != value))
{
this.OnDepartmentIdChanging(value);
this.SendPropertyChanging();
this._DepartmentId = value;
this.SendPropertyChanged("DepartmentId");
this.OnDepartmentIdChanged();
}
}
}
[Column(Storage="_Name", CanBeNull=false)]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name != value))
{
this.OnNameChanging(value);
this.SendPropertyChanging();
this._Name = value;
this.SendPropertyChanged("Name");
this.OnNameChanged();
}
}
}
[Association(Name="Department_Person", Storage="_Persons", ThisKey="DepartmentId", OtherKey="DepartmentId")]
public EntitySet<Person> Persons
{
get
{
return this._Persons;
}
set
{
this._Persons.Assign(value);
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Persons(Person entity)
{
this.SendPropertyChanging();
entity.Department = this;
}
private void detach_Persons(Person entity)
{
this.SendPropertyChanging();
entity.Department = null;
}
}
}
#pragma warning restore 1591
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Tests.LinqToSql;
using NUnit.Framework;
using System.Reflection;
using System.ComponentModel;
namespace Newtonsoft.Json.Tests.LinqToSql
{
public class LinqToSqlClassesSerializationTests : TestFixtureBase
{
[Test]
public void Serialize()
{
Role role = new Role();
role.Name = "Role1";
role.RoleId = new Guid("67EA92B7-4BD3-4718-BD75-3C7EDF800B34");
Person person = new Person();
person.FirstName = "FirstName!";
person.LastName = "LastName!";
person.PersonId = new Guid("7AA027AA-C995-4986-908D-999D8063599F");
person.PersonRoles.Add(new PersonRole
{
PersonRoleId = new Guid("B012DD41-71DF-4839-B8D5-D1333FB886BC"),
Role = role
});
person.Department = new Department
{
DepartmentId = new Guid("08F68BF9-929B-4434-BC47-C9489D22112B"),
Name = "Name!"
};
string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Assert.AreEqual(@"{
""first_name"": ""FirstName!"",
""LastName"": ""LastName!"",
""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
""PersonRoles"": [
{
""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
""RoleId"": ""67ea92b7-4bd3-4718-bd75-3c7edf800b34"",
""PersonRoleId"": ""b012dd41-71df-4839-b8d5-d1333fb886bc"",
""Role"": {
""Name"": ""Role1"",
""RoleId"": ""t5LqZ9NLGEe9dTx+34ALNA==""
}
}
],
""Department"": {
""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
""Name"": ""!emaN""
}
}", json);
}
[Test]
public void Deserialize()
{
string json = @"{
""first_name"": ""FirstName!"",
""LastName"": ""LastName!"",
""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
""PersonRoles"": [
{
""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
""RoleId"": ""67ea92b7-4bd3-4718-bd75-3c7edf800b34"",
""PersonRoleId"": ""b012dd41-71df-4839-b8d5-d1333fb886bc"",
""Role"": {
""Name"": ""Role1"",
""RoleId"": ""t5LqZ9NLGEe9dTx+34ALNA==""
}
}
],
""Department"": {
""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
""Name"": ""!emaN""
}
}";
Person person = JsonConvert.DeserializeObject<Person>(json);
Assert.IsNotNull(person);
Assert.AreEqual(new Guid("7AA027AA-C995-4986-908D-999D8063599F"), person.PersonId);
Assert.AreEqual("FirstName!", person.FirstName);
Assert.AreEqual("LastName!", person.LastName);
Assert.AreEqual(1, person.PersonRoles.Count);
Assert.AreEqual(person.PersonId, person.PersonRoles[0].PersonId);
Assert.AreEqual(new Guid("67EA92B7-4BD3-4718-BD75-3C7EDF800B34"), person.PersonRoles[0].RoleId);
Assert.IsNotNull(person.PersonRoles[0].Role);
Assert.AreEqual(1, person.PersonRoles[0].Role.PersonRoles.Count);
Assert.AreEqual("Name!", person.Department.Name);
}
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.LinqToSql
{
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
public class PersonMetadata
{
[JsonProperty("first_name")]
public string FirstName { get; set; }
}
}
}
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.LinqToSql
{
[MetadataType(typeof(RoleMetadata))]
public partial class Role
{
public class RoleMetadata
{
[JsonConverter(typeof(GuidByteArrayConverter))]
public Guid RoleId { get; set; }
[JsonIgnore]
public EntitySet<PersonRole> PersonRoles { get; set; }
}
}
}
@@ -50,6 +50,9 @@
<HintPath>..\Lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
@@ -83,6 +86,17 @@
<Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
<Compile Include="LinqToSql\Department.cs" />
<Compile Include="LinqToSql\DepartmentConverter.cs" />
<Compile Include="LinqToSql\GuidByteArrayConverter.cs" />
<Compile Include="LinqToSql\Role.cs" />
<Compile Include="LinqToSql\LinqToSqlClasses.designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>LinqToSqlClasses.dbml</DependentUpon>
</Compile>
<Compile Include="LinqToSql\LinqToSqlClassesSerializationTests.cs" />
<Compile Include="LinqToSql\Person.cs" />
<Compile Include="Schema\ExtensionsTests.cs" />
<Compile Include="Schema\JsonSchemaModelBuilderTests.cs" />
<Compile Include="Schema\JsonSchemaNodeTests.cs" />
@@ -166,6 +180,21 @@
<Name>Newtonsoft.Json</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="LinqToSql\LinqToSqlClasses.dbml">
<Generator>MSLinqToSQLGenerator</Generator>
<LastGenOutput>LinqToSqlClasses.designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
</ItemGroup>
<ItemGroup>
<None Include="LinqToSql\LinqToSqlClasses.dbml.layout">
<DependentUpon>LinqToSqlClasses.dbml</DependentUpon>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -65,6 +65,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
@@ -93,8 +93,8 @@ namespace Newtonsoft.Json.Serialization
protected virtual JsonMemberMapping CreateMemberMapping(MemberSerialization memberSerialization, MemberInfo member)
{
JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute<JsonPropertyAttribute>(member, true);
bool hasIgnoreAttribute = member.IsDefined(typeof(JsonIgnoreAttribute), true);
JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member);
bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(member) != null);
string mappedName = (propertyAttribute != null && propertyAttribute.PropertyName != null)
? propertyAttribute.PropertyName
@@ -112,7 +112,7 @@ namespace Newtonsoft.Json.Serialization
JsonConverter memberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));
DefaultValueAttribute defaultValueAttribute = ReflectionUtils.GetAttribute<DefaultValueAttribute>(member, true);
DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(member);
object defaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;
NullValueHandling? nullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
@@ -25,7 +25,11 @@
using System;
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC
using System.ComponentModel.DataAnnotations;
#endif
using System.Globalization;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Utilities;
@@ -35,6 +39,7 @@ namespace Newtonsoft.Json.Serialization
{
private static readonly Dictionary<ICustomAttributeProvider, Type> ConverterTypeCache = new Dictionary<ICustomAttributeProvider, Type>();
private static readonly Dictionary<Type, JsonContainerAttribute> TypeContainerAttributeCache = new Dictionary<Type, JsonContainerAttribute>();
private static readonly Dictionary<Type, Type> AssociatedMetadataTypesCache = new Dictionary<Type, Type>();
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
{
@@ -79,7 +84,7 @@ namespace Newtonsoft.Json.Serialization
if (ConverterTypeCache.TryGetValue(attributeProvider, out converterType))
return converterType;
JsonConverterAttribute converterAttribute = ReflectionUtils.GetAttribute<JsonConverterAttribute>(attributeProvider, true);
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
converterType = (converterAttribute != null)
? converterAttribute.ConverterType
: null;
@@ -106,5 +111,75 @@ namespace Newtonsoft.Json.Serialization
return null;
}
#if !SILVERLIGHT && !PocketPC
private static Type GetAssociatedMetadataType(Type type)
{
Type associatedMetadataType;
if (AssociatedMetadataTypesCache.TryGetValue(type, out associatedMetadataType))
return associatedMetadataType;
// double check locking to avoid threading issues
lock (AssociatedMetadataTypesCache)
{
if (AssociatedMetadataTypesCache.TryGetValue(type, out associatedMetadataType))
return associatedMetadataType;
MetadataTypeAttribute metadataTypeAttribute = ReflectionUtils.GetAttribute<MetadataTypeAttribute>(type, true);
if (metadataTypeAttribute != null)
associatedMetadataType = metadataTypeAttribute.MetadataClassType;
AssociatedMetadataTypesCache[type] = associatedMetadataType;
return associatedMetadataType;
}
}
private static T GetAttribute<T>(Type type) where T : Attribute
{
Type metadataType = GetAssociatedMetadataType(type);
if (metadataType != null)
return ReflectionUtils.GetAttribute<T>(metadataType, true);
return ReflectionUtils.GetAttribute<T>(type, true);
}
private static T GetAttribute<T>(MemberInfo memberInfo) where T : Attribute
{
Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType);
if (metadataType != null)
{
MemberInfo metadataTypeMemberInfo = metadataType.GetMember(memberInfo.Name,
memberInfo.MemberType,
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).SingleOrDefault();
if (metadataTypeMemberInfo != null)
return ReflectionUtils.GetAttribute<T>(metadataTypeMemberInfo, true);
}
return ReflectionUtils.GetAttribute<T>(memberInfo, true);
}
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
{
Type type = attributeProvider as Type;
if (type != null)
return GetAttribute<T>(type);
MemberInfo memberInfo = attributeProvider as MemberInfo;
if (memberInfo != null)
return GetAttribute<T>(memberInfo);
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
}
#else
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
{
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
}
#endif
}
}