I am currently facing the situation where I get a json file that I can't modify and I want the resulting deserialized class to be generic for design purpose.
First here are my interfaces :
public interface IJobModel
{
string ClientBaseURL { get; set; }
string UserEmail { get; set; }
ExportType Type { get; set; }
List<IItemModel> Items { get; set; }
}
public interface IItemModel
{
string Id { get; set; }
string ImageSize { get; set; }
string ImagePpi { get; set; }
List<ICamSettings> CamSettings { get; set; }
}
public interface ICamSettings
{
string FileName { get; set; }
}
Then here is the code I design to tackle my problem :
public class ThumbnailJobModel : IJobModel
{
[JsonProperty( "clientBaseURL" )]
public string ClientBaseURL { get; set; }
[JsonProperty( "userEmail" )]
public string UserEmail { get; set; }
[JsonProperty( "type" )]
[JsonConverter( typeof( TypeConverter ) )]
public ExportType Type { get; set; }
[JsonProperty( "items" )]
[JsonConverter( typeof( ConcreteConverter<List<IItemModel>, List<Item>>
) )]
public List<IItemModel> Items { get; set; }
public ThumbnailJobModel()
{
Type = ExportType.Thumbnails;
Items = new List<IItemModel>();
}
public class Item : IItemModel
{
[JsonProperty( "id" )]
public string Id { get; set; }
[JsonProperty( "imageSize" )]
public string ImageSize { get; set; }
[JsonProperty( "imagePpi" )]
public string ImagePpi { get; set; }
[JsonProperty( "shoots" )]
//[JsonConverter( typeof( CamSettingsConverter ) )]
[JsonConverter( typeof( ConcreteConverter<List<ICamSettings>,
List<ShootSettings>> ) )]
public List<ICamSettings> CamSettings { get; set; }
public Item()
{
CamSettings = new List<ICamSettings>();
}
}
public class ShootSettings : ICamSettings
{
[JsonProperty( "orientation" )]
[JsonConverter( typeof( OrientationConverter ) )]
public Orientation Orientation { get; set; }
[JsonProperty( "clothShape" )]
[JsonConverter( typeof( ClothShapeConverter ) )]
public Shape Shape { get; set; }
[JsonProperty( "fileName" )]
public string FileName { get; set; }
public ShootSettings()
{
Orientation = Orientation.Perspective;
Shape = Shape.Folded;
FileName = null;
}
}
public enum Orientation
{
Perspective = 0,
Oblique = 1,
Front = 2,
Back = 3,
Left = 4,
Right = 5,
Up = 6,
Down = 7
}
public enum Shape
{
Folded = 0,
Hanger = 1,
Mannequin = 2
}
public class ConcreteConverter<I, T> : JsonConverter
{
public override bool CanConvert( Type objectType )
{
return typeof( I ) == objectType;
}
public override object ReadJson( JsonReader reader,
Type objectType, object existingValue, JsonSerializer serializer )
{
return serializer.Deserialize<T>( reader );
}
public override void WriteJson( JsonWriter writer,
object value, JsonSerializer serializer )
{
throw new NotImplementedException();
}
}
public class OrientationConverter : JsonConverter
{
public override object ReadJson( JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer )
{
string enumString = (string)reader.Value;
return Enum.Parse( typeof( Orientation ), enumString, true );
}
public override bool CanConvert( Type objectType )
{
return objectType == typeof( string );
}
public override void WriteJson( JsonWriter writer, object value,
JsonSerializer serializer )
{
throw new NotImplementedException();
}
}
public class ClothShapeConverter : JsonConverter
{
public override object ReadJson( JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer )
{
var enumString = (string)reader.Value;
return Enum.Parse( typeof( Shape ), enumString, true );
}
public override bool CanConvert( Type objectType )
{
return objectType == typeof( string );
}
public override void WriteJson( JsonWriter writer, object value,
JsonSerializer serializer )
{
throw new NotImplementedException();
}
}
public class TypeConverter : JsonConverter
{
public override object ReadJson( JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer )
{
return ExportType.Thumbnails;
}
public override bool CanConvert( Type objectType )
{
return objectType == typeof( string );
}
public override void WriteJson( JsonWriter writer, object value,
JsonSerializer serializer )
{
throw new NotImplementedException();
}
}
public static void HandleDeserializationError( object sender,
ErrorEventArgs errorArgs )
{
errorArgs.ErrorContext.Handled = true;
var currentObj = errorArgs.CurrentObject as ShootSettings;
if ( currentObj == null ) return;
currentObj.Orientation = Orientation.Perspective;
currentObj.Shape = Shape.Folded;
}
}
As you can see there is a list of ICamSettings
in the IItemModel
interface.
I try to deserialize this json into my ThumbnailJobModel
class :
{
"clientBaseURL":"https://clientName.fr",
"userEmail":"myName@gmail.com",
"items":[
{
"id":"11913",
"imageSize":"1280,720",
"imagePpi":"72",
"shoots":[
{
"fileName":"front1.png",
"orientation":"front",
"clothShape":"hanger"
},
{
"fileName":"folded1.png",
"orientation":"front",
"clothShape":"folded"
},
{
"fileName":"right1.png",
"orientation":"right",
"clothShape":"hanger"
}
]
},
{
"id":"2988",
"imageSize":"1280,720",
"imagePpi":"",
"shoots":[
{
"fileName":"perspective1.png",
"orientation":"perspective"
}
]
}
]
}
I deserialize my json like that :
//Read the job config
string jobConfig = File.ReadAllText( jsonConfigPath );
IJobModel m_jobModel = JsonConvert.DeserializeObject<ThumbnailJobModel>(
jobConfig );
And the following exception is thrown:
Exception : Error setting value to 'CamSettings' on
'IWD.Screenshoter.Job.ThumbnailJobModel+Item'.
Stack :
at Newtonsoft.Json.Serialization.DynamicValueProvider.SetValue
(System.Object target, System.Object value) [0x00000] in <filename
unknown>:0
at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue
(Newtonsoft.Json.Serialization.JsonProperty property,
Newtonsoft.Json.JsonConverter propertyConverter,
Newtonsoft.Json.Serialization.JsonContainerContract containerContract,
Newtonsoft.Json.Serialization.JsonProperty containerProperty,
Newtonsoft.Json.JsonReader reader, System.Object target) [0x00000] in
<filename unknown>:0
at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject
(System.Object newObject, Newtonsoft.Json.JsonReader reader,
Newtonsoft.Json.Serialization.JsonObjectContract contract,
Newtonsoft.Json.Serialization.JsonProperty member, System.String id)
[0x00000] in <filename unknown>:0
I honestly don't understand what I am doing wrong, I hope someone will be able to throw some light on it.