public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) { if (value == null) { writer.WriteNull(); return; } else { writer.WriteBeginObject(); var propInfos = value.OptionalPropertyInfos; var e = value.BackingFields.GetEnumerator(); var args = new object[3]; args[2] = formatterResolver; try { if (e.MoveNext()) { var item = e.Current; var propInfo = propInfos[item.Key]; writer.WriteString(propInfo.JsonPropertyName ?? item.Key); writer.WriteNameSeparator(); var formatterType = typeof(IJsonFormatter <>).MakeGenericType(propInfo.PropertyType); var formatter = propInfo.Formatter ?? formatterResolver.GetFormatterDynamic(propInfo.PropertyType); args[0] = writer; args[1] = item.Value; formatterType.InvokeMember("Serialize", BindingFlags.InvokeMethod, Type.DefaultBinder, formatter, args); writer = (JsonWriter)args[0]; } else { goto END; } while (e.MoveNext()) { writer.WriteValueSeparator(); var item = e.Current; var propInfo = propInfos[item.Key]; writer.WriteString(propInfo.JsonPropertyName ?? item.Key); writer.WriteNameSeparator(); var formatterType = typeof(IJsonFormatter <>).MakeGenericType(propInfo.PropertyType); var formatter = propInfo.Formatter ?? formatterResolver.GetFormatterDynamic(propInfo.PropertyType); args[0] = writer; args[1] = item.Value; formatterType.InvokeMember("Serialize", BindingFlags.InvokeMethod, Type.DefaultBinder, formatter, args); writer = (JsonWriter)args[0]; } } finally { e.Dispose(); } END: writer.WriteEndObject(); } }
public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) { return(default(T)); } else { reader.ReadIsBeginObjectWithVerify(); var obj = new T(); var propInfos = obj.OptionalPropertyInfosByJsonPropertyName; var backingFields = obj.BackingFields; var i = 0; var args = new object[2]; args[1] = formatterResolver; while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref i)) { var key = reader.ReadPropertyName(); var propInfo = propInfos[key]; var formatterType = typeof(IJsonFormatter <>).MakeGenericType(propInfo.PropertyType); var formatter = propInfo.Formatter ?? formatterResolver.GetFormatterDynamic(propInfo.PropertyType); args[0] = reader; var val = formatterType.InvokeMember("Deserialize", BindingFlags.InvokeMethod, Type.DefaultBinder, formatter, args); backingFields[propInfo.PropertyName] = val; reader = (JsonReader)args[0]; } return(obj); } }
public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) { if (value == null) { writer.WriteNull(); } else { writer.WriteBeginObject(); var propInfos = value.OptionalPropertyInfos; var args = new object[3]; args[2] = formatterResolver; var first = true; foreach (var item in value.BackingFields) { var propInfo = propInfos[item.Key]; if (propInfo.ShouldSerialize != null && ((Func <T, bool>)propInfo.ShouldSerialize)(value) == false) { continue; } if (first) { first = false; } else { writer.WriteValueSeparator(); } writer.WriteString(propInfo.JsonPropertyName ?? item.Key); writer.WriteNameSeparator(); var formatterType = typeof(IJsonFormatter <>).MakeGenericType(propInfo.PropertyType); var formatter = propInfo.Formatter ?? formatterResolver.GetFormatterDynamic(propInfo.PropertyType); args[0] = writer; args[1] = item.Value; formatterType.InvokeMember("Serialize", BindingFlags.InvokeMethod, Type.DefaultBinder, formatter, args); writer = (JsonWriter)args[0]; } writer.WriteEndObject(); } }
public static void WriteInnerSettings(ref JsonWriter writer, ContainerSetting value, IJsonFormatterResolver formatterResolver) { foreach (var inner in value.InnerSettings) { dynamic genericFormatter; if (inner is HardCodedSetting) { genericFormatter = formatterResolver.GetFormatterWithVerify <StringSetting>(); } else if (inner is HexadecimalSetting) { new TypedFormatter <uint>().Serialize(ref writer, (HexadecimalSetting)inner, formatterResolver); continue; } else if (inner is UnsignedNumberArraySetting) { var fmt = formatterResolver.GetFormatterWithVerify <UnsignedNumberArraySetting>(); fmt.Serialize(ref writer, (UnsignedNumberArraySetting)inner, formatterResolver); continue; } else if (inner is SignedNumberArraySetting) { var fmt = formatterResolver.GetFormatterWithVerify <SignedNumberArraySetting>(); fmt.Serialize(ref writer, (SignedNumberArraySetting)inner, formatterResolver); continue; } else if (inner is StringArraySetting) { var fmt = formatterResolver.GetFormatterWithVerify <StringArraySetting>(); fmt.Serialize(ref writer, (StringArraySetting)inner, formatterResolver); continue; } else if (inner is RegexSetting) { new StringSettingFormatter().Serialize(ref writer, (RegexSetting)inner, formatterResolver); continue; } else if (inner is BoolSetting) { new BoolSettingFormatter().Serialize(ref writer, (BoolSetting)inner, formatterResolver); continue; } else { genericFormatter = formatterResolver.GetFormatterDynamic(inner.GetType()); } if (inner is StringSetting typed) { genericFormatter.Serialize(ref writer, typed, formatterResolver); } else if (inner is NumberSetting num) { genericFormatter.Serialize(ref writer, num, formatterResolver); } else if (inner is UnsignedNumberSetting unum) { genericFormatter.Serialize(ref writer, unum, formatterResolver); } else if (inner is AdditiveContainerSetting adt) { genericFormatter.Serialize(ref writer, adt, formatterResolver); } else if (inner is MultipleAdditiveContainerSetting mlt) { genericFormatter.Serialize(ref writer, mlt, formatterResolver); } else if (inner is SettingsTemplate tmp) { genericFormatter.Serialize(ref writer, tmp, formatterResolver); } else if (inner is ContainerSetting cnt) { genericFormatter.Serialize(ref writer, cnt, formatterResolver); } else if (inner is EnumSetting en) { genericFormatter.Serialize(ref writer, en, formatterResolver); } else { genericFormatter.Serialize(ref writer, inner, formatterResolver); } } }