2013-05-29 04:49:17 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-05-29 02:44:29 +02:00
|
|
|
|
using NzbDrone.Common.Reflection;
|
|
|
|
|
using NzbDrone.Core.Annotations;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.ClientSchema
|
|
|
|
|
{
|
|
|
|
|
public static class SchemaDeserializer
|
|
|
|
|
{
|
2013-05-29 08:24:45 +02:00
|
|
|
|
public static T DeserializeSchema<T>(T model, List<Field> fields)
|
2013-05-29 02:44:29 +02:00
|
|
|
|
{
|
|
|
|
|
var properties = model.GetType().GetSimpleProperties();
|
|
|
|
|
|
|
|
|
|
foreach (var propertyInfo in properties)
|
|
|
|
|
{
|
|
|
|
|
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
|
|
|
|
|
|
|
|
|
|
if (fieldAttribute != null)
|
|
|
|
|
{
|
|
|
|
|
var field = fields.Find(f => f.Name == propertyInfo.Name);
|
2013-05-29 04:49:17 +02:00
|
|
|
|
|
|
|
|
|
if (propertyInfo.PropertyType == typeof (Int32))
|
|
|
|
|
{
|
|
|
|
|
var intValue = Convert.ToInt32(field.Value);
|
|
|
|
|
propertyInfo.SetValue(model, intValue, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
propertyInfo.SetValue(model, field.Value, null);
|
|
|
|
|
}
|
2013-05-29 02:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|