private static IList <SerializingMember> PrepareCore(SerializationContext context, Type targetType) { var entries = GetTargetMembers(targetType).OrderBy(item => item.Contract.Id).ToArray(); if (entries.Length == 0) { throw SerializationExceptions.NewNoSerializableFieldsException(targetType); } if (entries.All(item => item.Contract.Id == DataMemberContract.UnspecifiedId)) { // Alphabetical order. return(entries.OrderBy(item => item.Contract.Name).ToArray()); } // ID order. #if DEBUG && !UNITY Contract.Assert(entries[0].Contract.Id >= 0); #endif // DEBUG && !UNITY if (context.CompatibilityOptions.OneBoundDataMemberOrder && entries[0].Contract.Id == 0) { throw new NotSupportedException("Cannot specify order value 0 on DataMemberAttribute when SerializationContext.CompatibilityOptions.OneBoundDataMemberOrder is set to true."); } var maxId = entries.Max(item => item.Contract.Id); var result = new List <SerializingMember>(maxId + 1); for (int source = 0, destination = context.CompatibilityOptions.OneBoundDataMemberOrder ? 1 : 0; source < entries.Length; source++, destination++) { #if DEBUG && !UNITY Contract.Assert(entries[source].Contract.Id >= 0); #endif // DEBUG && !UNITY if (entries[source].Contract.Id < destination) { throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The member ID '{0}' is duplicated in the '{1}' elementType.", entries[source].Contract.Id, targetType)); } while (entries[source].Contract.Id > destination) { result.Add(new SerializingMember()); destination++; } result.Add(entries[source]); } return(result); }
/// <summary> /// Creates serializer for <typeparamref name="TObject"/>. /// </summary> /// <returns> /// <see cref="MessagePackSerializer{T}"/>. This value will not be <c>null</c>. /// </returns> public MessagePackSerializer <TObject> CreateSerializer() { Contract.Ensures(Contract.Result <MessagePackSerializer <TObject> >() != null); var entries = GetTargetMembers().OrderBy(item => item.Contract.Id).ToArray(); if (entries.Length == 0) { throw SerializationExceptions.NewNoSerializableFieldsException(typeof(TObject)); } if (entries.All(item => item.Contract.Id == DataMemberContract.UnspecifiedId)) { // Alphabetical order. return(this.CreateSerializer(entries.OrderBy(item => item.Contract.Name).ToArray())); } else { // ID order. Contract.Assert(entries[0].Contract.Id >= 0); if (this.Context.CompatibilityOptions.OneBoundDataMemberOrder && entries[0].Contract.Id == 0) { throw new NotSupportedException("Cannot specify order value 0 on DataMemberAttribute when SerializationContext.CompatibilityOptions.OneBoundDataMemberOrder is set to true."); } var maxId = entries.Max(item => item.Contract.Id); var result = new List <SerializingMember>(maxId + 1); for (int source = 0, destination = this.Context.CompatibilityOptions.OneBoundDataMemberOrder ? 1 : 0; source < entries.Length; source++, destination++) { Contract.Assert(entries[source].Contract.Id >= 0); if (entries[source].Contract.Id < destination) { throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The member ID '{0}' is duplicated in the '{1}' type.", entries[source].Contract.Id, typeof(TObject))); } while (entries[source].Contract.Id > destination) { result.Add(new SerializingMember()); destination++; } result.Add(entries[source]); } return(this.CreateSerializer(result.ToArray())); } }
public MessagePackSerializer <TObject> CreateSerializer() { SerializingMember[] source = (from item in SerializerBuilder <TObject> .GetTargetMembers() orderby item.Contract.Id select item).ToArray <SerializingMember>(); if (source.Length == 0) { throw SerializationExceptions.NewNoSerializableFieldsException(typeof(TObject)); } if (source.All <SerializingMember>(item => item.Contract.Id == -1)) { return(this.CreateSerializer((from item in source orderby item.Contract.Name select item).ToArray <SerializingMember>())); } Contract.Assert(source[0].Contract.Id >= 0); if (this.Context.CompatibilityOptions.OneBoundDataMemberOrder && (source[0].Contract.Id == 0)) { throw new NotSupportedException("Cannot specify order value 0 on DataMemberAttribute when SerializationContext.CompatibilityOptions.OneBoundDataMemberOrder is set to true."); } List <SerializingMember> list = new List <SerializingMember>(source.Max <SerializingMember>(item => item.Contract.Id) + 1); int index = 0; for (int i = this.Context.CompatibilityOptions.OneBoundDataMemberOrder ? 1 : 0; index < source.Length; i++) { Contract.Assert(source[index].Contract.Id >= 0); if (source[index].Contract.Id < i) { throw new SerializationException(string.Format(CultureInfo.CurrentCulture, "The member ID '{0}' is duplicated in the '{1}' type.", new object[] { source[index].Contract.Id, typeof(TObject) })); } while (source[index].Contract.Id > i) { SerializingMember member = new SerializingMember(); list.Add(member); i++; } list.Add(source[index]); index++; } return(this.CreateSerializer(list.ToArray())); }