public static void Serialize(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, T o) { if (s_serializer == null) { var t = typeof(T); MethodInfo g = null; if (t.IsArray) { var mi = typeof(JsonArrayValidator).GetMethod("ArraySerializer", BindingFlags.Static | BindingFlags.NonPublic); g = mi.MakeGenericMethod(t.GetElementType()); } else if (t.GetIsGenericList()) { // ToDo: IList var mi = typeof(JsonArrayValidator).GetMethod("ListSerializer", BindingFlags.Static | BindingFlags.NonPublic); g = mi.MakeGenericMethod(t.GetGenericArguments()); } else { throw new NotImplementedException(); } var compiled = (Action < IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, T>) GenericInvokeCallFactory.StaticAction < IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, T>(g); s_serializer = new Serializer(compiled); } s_serializer(v, f, c, o); }
private static Action <IFormatter, T> GetSerializer() { var t = typeof(T); // object if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } try { // primitive var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { return(GenericInvokeCallFactory.OpenAction <IFormatter, T>(mi)); } } catch (AmbiguousMatchException) { // do nothing } { // dictionary var idictionary = t.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary <,>) && x.GetGenericArguments()[0] == typeof(string) ); if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // object[] if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // list var ienumerable = t.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>) ); if (ienumerable != null) { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // reflection var schema = JsonSchema.FromType <T>(); return((IFormatter f, T value) => { var c = new JsonSchemaValidationContext(value) { EnableDiagnosisForNotRequiredFields = true }; schema.Serialize(f, value, c); }); } //throw new NotImplementedException(); }