I was wondering if I have an object that contains a field which has its deserialization process dependant on another field, how can I deserialize the parent object?
Container
class Container
{
public int Id { get; set; }
public object Data { get; set; } //deserialization depends on first field
}
Hierarchy
class FieldType1
{
public string Value { get; set; }
}
class FieldType2
{
public int Numbers { get; set; }
}
Given the example above if I have a Dictionary<int,Type> how can I deserialize an object that comes as a string like the one below?:
var container = new Container { Data = new FieldType1 { Value = "sata" }};
var str = JsonConvert.SerializeObject(container);
var clone = JsonConvert.DeserializeObject<Container>(str);//has dependant field on another field
As you can see in my example above I always have the same container type.but one property differs.
Update
After some answers here could it be possible to keep only one type of parent object and instead have a base type for the second field ?
[JsonSubTypes.KnownSubType(typeof(Child1),1)]
[JsonSubTypes.KnownSubType(typeof(Child2),2)]
public abstract Child
{
}
public class Parent{
public int Id;
public Child child;
}
Can i decorate somehow the parent to know how to deserialize its second field (similar to JsonSubTypes)?
Summing it up i do not want to have P,P1,P2..Pn types for parent.
I want to have one type P for parent with F1,F2...Fn types for its second field.So that when i deserialize i would just say JsonConvert.DeserializeObject<P> while the converter takes care of which concrete type is the second field:
Parent c1=new P{ id=1,child=new Child1()};
Parent c2=new P{ id=2,child=newChild2()};
List<Parent> items=new List<Parent>{c1,c2};
var str=JsonConvert.SerializeObject(items);
var clone=JsonConvert.DeserializeObject<List<Parent>>(str);
At a first glance, I'd simply use a simple function that you could put into a SomeNameParser/Converter class.
Pesudo C# code, something like the following:
var jObject = JObject.Parse(obj.Data);
switch (jObject["firstField"])
{
case "fieldType1":
return JsonConvert.DeserializeObject<string>(str);
case "fieldType2":
return JsonConvert.DeserializeObject<int>(str);
default:
Throw new Exception( make this meaningful)
}
Improvements
You could make the parsing of the firstField do a lookup to return a System.Type, then pass the type to JsonConvert.Deserialize(obj.Data, type) which would save the repetitive JsonConvert.
Hopefully you can see the general pattern.
Related
I have a requirement where I need to set default value to the below complex property Instances using JsonProperty and DefaultValue.
I know we can achieve this for primitive properties as mentioned in the below link, but need to know how we can do it for complex properties.
Default value for missing properties with JSON.net
Below is the default Instances value I need to set using DefaultValue(). Please let me know how to achieve this.
Default value to be set to Instances property:
Instance instance = new Instance();
instance.Name = "XYZ";
instance.MyProperty = 11;
List<Instance> Instances = new List<Instance>();
Instances.Add(instance);
Code snippet:
public class DataSettings
{
public DataSettings()
{
Instances = new List<Instance>();
}
[DefaultValue()] //How can I mention the above default value here ?
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public List<Instance> Instances { get; set; }
}
public class Instance
{
public string Name { get; set; }
public int MyProperty { get; set; }
}
As you've seen, attributes only support constant values, so you cannot set a complex value in an attribute. If you want to set a default value for a complex property during deserialization, a good approach is to use a serialization callback method, as shown below.
The idea is to add a method to your class which the serializer will call after deserialization is complete for the object. The callback must be a void method that accepts a StreamingContext as its only parameter, and it must be marked with an [OnDeserialized] attribute. The name of the method does not matter.
Inside the callback method you can check whether the Instances list was populated, and if not, you can set the default value as you require.
public class DataSettings
{
public DataSettings()
{
Instances = new List<Instance>();
}
public List<Instance> Instances { get; set; }
[OnDeserialized]
internal void SetDefaultValuesAfterDeserialization(StreamingContext context)
{
if (Instances == null || !Instances.Any())
{
Instances = new List<Instance>
{
new Instance { Name = "XYZ", MyProperty = 11 }
};
}
}
}
Here is a working fiddle to demonstrate the concept: https://dotnetfiddle.net/uCGP5X
I've got two objects which (Domain and Data) which in this case have the same property (let's presume Name). I've got an Action<DomItem> which I would like to cast to Action<DataItem>.
public class DomItem {
public string Name { get; set; }
}
public class DataItem {
public string Name { get; set; }
}
public class Program {
public Program() {
Action<DomItem> domAction = new Action<DomItem>(x=>x.Name = "Test");
// Something Casted To Action<DataItem>(x=>x.Name = "Test");
}
}
Of course this is just a basic example. It's by design that I can NOT use a common interface. I do not care about the DataItem might not be having the same property.
I've been looking into Expressions and several other solutions but I just can't figure out how to create the Cast (or get the "x=>x.Name =..." part from the method).
Any help would be really appreciated!
You can't directly or indirectly cast a Action<DomItem> to an Action<DataItem>, but you could wrap the action with a converter that converts the input from a DataItem to a DomItem and runs the original action on the copy:
public Action<DataItem> Convert(Action<DomItem> action)
{
return new Action<DataItem>(o => action(Map(o)));
}
public DomItem Map(DataItem dataItem)
{
return new DomItem{Name = dataItem.Name};
}
The obvious downside is that the action will be applied to a copy of the original object and not the original object itself. Without knowing exactly what the action is I don't know of a way to "cast" the action without a common base type.
I am new to Interfaces.
I have a lot of objects that I pass as DTOs through my layers to the UI. Some of them are quite complex (Quite a few properties), but I only want to use them, in certain circumstances, in DropDown lists. These DTOs all have an int Id, and a string Description.
I would like to create a static function that takes a List<> of one of these objects, and returns a List<SelectListItem>
So, I am trying to use Interfaces for the first time.
I created an Interface:
public interface IListableItem
{
int Id { get; set; }
string Description { get; set; }
}
And then, I assigned that interface to one of my DTO objects I am trying to convert:
public class CategoryDto : BaseDto , IListableItem
{
public int PortfolioId { get; set; }
public string Description { get; set; }
public List<ExtendedSubCategoryDto> SubCategories { get; set; }
public bool IsExpenseCategory { get; set; }
public CategoryDto()
{
SubCategories = new List<ExtendedSubCategoryDto>();
}
}
Then, I created my generic method that takes a list of the category dtos, and will hopefully return a list
public static List<SelectListItem> TranslateToSelectList(List<IListableItem> source)
{
var reply = source.Select(item => new SelectListItem
{
Value = item.Id.ToString(CultureInfo.InvariantCulture), Text = item.Description
}).ToList();
return reply;
}
But, when I attempt to use this method, passing it a List, it fails.
model.Categories =
Translator.TranslateToSelectList(MyService.GetCategoriesByPortfolioId());
GetCategoriesByPortfolioId returns a List.
It's failing with the error:
CategoryDto is not assignable to IListableItem
It's probably a basic Interface understanding issue on my part, but what am I doing wrong, and how can I fix it?
If your method expects List<IListableItem>, you can't pass List<CategoryDTO>.
If that would be possible, you could Add different instances of elements that implement the interface IListableItem into a collection that is holding CategoryDTO elements, and read them.
Ultimately, that wouldn't make sense.
You can fix it, if you use IEnumerable interface. That allows covariance(going from higher type to lower type in generic type parameter).
The reason it works, is that IEnumerable is a "read-only view" of collection, thus you can't really add anything to it - plus what's important, the type parameter is marked as covariant.
public static List<SelectListItem> TranslateToSelectList(
IEnumerable<IListableItem> source)
{..}
I'm trying to make a text writer according to my classes properties in the following pattern:
MyClass
ID 1
Name MyName
AnotherProperty SomeValue
ThisIsAnotherClass
AnotherClassProperties 1
//Example class
public class MyClass
{
public int ID { get; set; }
public string Name { get; set; }
public string AnotherProperty { get; set; }
public AnotherClass ThisIsAnotherClass { get; set; }
}
So I'm taking each property name, writing it, a blank space, then it's value (if there is any).
Now I'm trying to implement support for lists and anything array-like for something like this:
MyClass
ArrayTest
1
2
3
If it's a class, I'll have a recursive for the function so I can display all values inside the list/array in this pattern. (it's for a webservice)
My question is, how can I find if a specific property is something list-able?
I've tried:
Type type = myObject.GetType();
PropertyInfo[] properties = type.GetProperties();
for(int i = 0; i < properties.Length; i++)
{
if(properties[i].PropertyType.IsGeneric) //Possible List/Collection/Dictionary
{
//Here is my issue
Type subType = properties[i].PropertyType.GetGenericTypeDefinition();
bool isAssignable = subType.IsAssignableFrom(typeof(ICollection<>)); //Always false
bool isSubclass = subType.IsSubclassOf(typeof(ICollection<>)); //Always false
//How can I figure if it inherits ICollection/IEnumerable so I can use it's interface to loop through it's elements?
}
else if(properties[i].PropertyType.IsArray) //Array
{
}
else if(properties[i].PropertyType.IsClass && !properties[i].PropertyType.Equals(typeof(String)))
{
//Non-string Subclasses, recursive here
}
else
{
//Value types, write the text + value
}
}
Like mentioned in the comments: use Json as a way to format objects, it will save a lot of time.
If you have reason not to do this, you can check if the type is enumerable: This also covers the Type.IsArray case.
typeof(IEnumerable).IsAssignableFrom(properties[i].PropertyType)
As an added notice of caution: maybe you do not want to enumerate String and byte[] type objects.
I have two lists of different objects, one from a third party API and one from my database - and I'm trying to link the two as a relationship. Ideally with a similar effect of how DBML's create relationships for tables with foreign keys (Customer.Orders).
From third party:
class ApiObject {
public string ID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
... 30 other properties ...
}
From my database:
class DbmlObject {
public int ID { get; set; }
public string ApiID { get; set; }
public string OtherString { get; set; }
}
They are related through ApiObject.ID == DbmlObject.ApiID
I do not want to merge these, nor join them into some anonymous object (and explicitly list 30+ properties) - but rather to make the DbmlObject a linked property of ApiObject. i.e.: addressable as:
apiObject.DbmlObjects.First().OtherString or ideally apiObject.DbmlObject.OtherString since it is a 1 to 1 relationship.
In controller:
List<ApiObject> apiObjects = _thirdParty.GetObjects();
DbmlDataContext model = new DbmlDataContext();
List<DbmlObject> dbmlObjects = model.GetAllDbmlObjects();
// relate them here
foreach (var apiObject in apiObjects)
Console.Write(apiObject.DbmlObject.OtherString)
// NOTE: ideally this foreach loop should not make a DBML query on each iteration, just the single GetAllDbmlObjects query above.
It sounds like a join:
var combined = from api in apiObjects
join dbml in dbmlObjects on api.ID equals dbml.ApiID
select new { api, dbml }
In order to get DbmlObject "in" the ApiObject, you will need to either inherit ApiObject and construct a new one of that class, which includes the Dbml property, or create a entirely new class to return. If you need static typing this is the best you can do - of course you could (mis)use dynamic to get what you want.
In this case, you are mentioning (in comments) that the ApiObject class is from a third party library that you can't change - in this case I would probably choose to create a new type which takes an instance of both objects in the constructor and exposes the properties you need - a decorator. Yes, it looks like a lot of code, but it is not complex, good tools will autogenerate it for you - and you get the class that you need for your code to be succinct.
In case you want to go further with returning an IEnumerable<dynamic>, you could build a "combining dynamic" object based on DynamicObject that then responds to all the properties of ApiObject and DbmlObject - or just adds DbmlObject as a property. I am not saying this is the right way to go, it depends on what you need it for - remember you are losing type safety. Here is a simple example:
void Main()
{
dynamic dyn = new CombiningDynamic(new Foo { X = 3 }, new Bar { Y = 42 });
Console.WriteLine(dyn.X);
Console.WriteLine(dyn.Y);
}
public class Foo
{
public int X {get;set;}
}
public class Bar
{
public int Y { get;set;}
}
public class CombiningDynamic : DynamicObject
{
private object [] innerObjects;
public CombiningDynamic(params object [] innerObjects)
{
this.innerObjects = innerObjects;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
foreach(var instance in innerObjects)
{
Type t = instance.GetType();
PropertyInfo prop = t.GetProperty(binder.Name);
if (prop != null && prop.CanRead)
{
result = prop.GetValue(instance, null);
return true;
}
}
result = null;
return false;
}
}
Remember, this is example code. If you really go this way, you would want to perhaps override some more of the methods (TrySetMember, ...), and you most definetely would want to cache the reflection results so you don't need to walk the types each time - reflection is (comparatively) slow.