This project has moved and is read-only. For the latest updates, please go here.

Support for strongly typed datasets

Sep 21, 2007 at 8:22 AM
Edited Sep 21, 2007 at 8:25 AM

Thanks a lot for sharing this library - it is fantastic!
I just added a small feature to it that I thought could be useful to other people. It is an attribute to prevent the serializer from descending into subclasses. This is useful for typed datasets, because I just wanted to serialize the MyTableDataRow object properties, and not the DataRow ones. Given that It's hard to try to add 'JsonIgnore' to the DataRow properties I creates a new simple JsonNoSubClassAttribute:

namespace Newtonsoft.Json
AttributeUsage(AttributeTargets.Class, AllowMultiple = false)
public sealed class JsonNoSubClassAttribute : Attribute

and then modified 'CreateMemberMappings' in JsonSerializer.cs:

private MemberMappingCollection CreateMemberMappings(Type objectType)
List<MemberInfo> members = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance);
MemberMappingCollection memberMappings = new MemberMappingCollection();

foreach (MemberInfo member in members)
string mappedName;

JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute<JsonPropertyAttribute>(member, true);
if (propertyAttribute != null)
mappedName = propertyAttribute.PropertyName;
mappedName = member.Name;

bool ignored = member.IsDefined(typeof (JsonIgnoreAttribute), true) ||
((objectType.IsDefined(typeof(JsonNoSubClassAttribute), false)) && (objectType != member.DeclaringType));
bool readable = ReflectionUtils.CanReadMemberValue(member);
bool writable = ReflectionUtils.CanSetMemberValue(member);
MemberMapping memberMapping = new MemberMapping(mappedName, member, ignored, readable, writable);


return memberMappings;

(note the change to the 'ignored' variable assignment.

Anyway, I hope this is helpful.

(PS: ugh no matter what I try, I can't prevent the code from being formatted by the wiki...}
Oct 11, 2007 at 5:09 AM
Good work