Cosmonaut(CosmosDb) - problem with Serialization - c#

I have a POCO class as it has been shown below. What is that IFooInterface? it is an interface that each object whose wants to be added to Cosmosdb, needs to inherit from that. (The main reason back to this fact that we have a different type of Results)
[CosmosCollection("foo")]
public class FooClass
{
[CosmosPartitionKey]
[JsonProperty("foo")] public string Foo{ get; set; }
[JsonProperty("id")] public string Id{ get; set; }
[JsonProperty("reportSet")] public IFooInterface FooResult { get; set; }
}
public interface IFooInterface {}
and I have a repo who helps to get those results back. My problem is that in the method below, I got an error. Here is the method in a repo
public async Task<IEnumerable<objectClass>> GetAsync(GetObjects query)
{
try
{
var testOne= await _reportStore
.Query(new FeedOptions { PartitionKey = new PartitionKey(query.foo) })
.ToListAsync();
return result;
}
catch (Exception e)
{
throw e;
}
}
And error is
Message = "Could not create an instance of type IFooInterface.
Type is an interface or abstract class and cannot be instantiated."
StackTrace = " at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract,
JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)\r\n
at ...
I wonder how can I solve this issue? Does anyone have any suggestions?

'The deserializer is not capable to resolve interfaces oder abstract classes. However a solution could be to add the $type property to your FooResult and use this for deserialization.
[JsonProperty(PropertyName = "reportSet", TypeNameHandling = TypeNameHandling.Objects)]
public IFooInterface FooResult { get; set; }
Your Json object of FooResult has now to contain the $type property like
{ "$type" : "someNamespace.FooInterfaceImplementation, assemblyOfTheClass" }

Related

Assign the same JsonProperty to two properties on class

When deserializing a JSON property from a request I want to use it for two different properties on my object. e.g;
public class Example
{
[JsonProperty(PropertyName = "favoriteColor")]
public string favoriteColor { get; set; }
[JsonProperty(PropertyName = "favoriteColor")]
public string oldFavoriteColor { get; set; }
}
However this causes an error:
A member with the name 'favoriteColor' already exists on 'Example'. Use the JsonPropertyAttribute to specify another name.
How do I do this when that's exactly what I intend?
I think you could modify the set method for one of the properties so that whenever it is set, it also sets the other property
e.g.
public class Example
{
[JsonProperty(PropertyName = "favoriteColor")]
public string favoriteColor {
get { return favoriteColor; }
set
{
favoriteColor = value;
if (oldFavoriteColor == null) {
oldFavoriteColor = value;
}
}
}
public string? oldFavoriteColor { get; set; }
}
There may be a better answer that involves taking a step back from the problem and seeing whether there's a totally different approach. In particular, why do you need a class that has two properties with the same value? Other than deserializing from JSON, will every other consumer of this class know that these two properties need to be kept in sync, and why?
But this answers the immediate question.
The [JsonProperty] attribute is used for both serializing and deserializing. If you had two properties with the same [JsonProperty] attribute then when you serialized the object to JSON you would have two properties with the same name.
You can create a custom JSON serializer like this. As you can see, after an Example is deserialized it will populate the favoriteColor property with the value of the oldFavoriteColor property.
public class ExampleConverter : CustomCreationConverter<Example>
{
public override Example Create(Type objectType)
{
return new Example();
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
var result = (Example)base.ReadJson(reader, objectType, existingValue,
serializer);
result.favoriteColor = result.oldFavoriteColor;
return result;
}
}
In order for this to work you also have to tell the serializer not to attempt to deserialize the favoriteColor property. Even without the [JsonProperty] attribute there's still a conflict between [JsonProperty(PropertyName = "favoriteColor")] and another property actually named "favoriteColor."
public class Example
{
[JsonIgnore]
public string favoriteColor { get; set; }
[JsonProperty(PropertyName = "favoriteColor")]
public string oldFavoriteColor { get; set; }
}
A unit test to confirm:
public void DeserializerPopulatesFavoriteColorFromOldFavoriteColor()
{
var json = #"{ favoriteColor: ""Green""}";
var deserialized = JsonConvert.DeserializeObject<Example>(json, new ExampleConverter());
Assert.AreEqual("Green", deserialized.oldFavoriteColor);
Assert.AreEqual(deserialized.oldFavoriteColor, deserialized.favoriteColor);
}

Returning a generic object without knowing the type?

I'm still fairly new to programming and have been tasked with creating a WebHook consumer that takes in a raw JSON string, parses the JSON into an object, which will be passed into a handler for processing. The JSON is coming in like this:
{
"id":"1",
"created_at":"2017-09-19T20:41:23.093Z",
"type":"person.created",
"object":{
"id":"person1",
"created_at":"2017-09-19T20:41:23.076Z",
"updated_at":"2017-09-19T20:41:23.076Z",
"firstname":"First",
...
}
}
The inner object can be any object so I thought this would be a great opportunity to use generics and built my class as follows:
public class WebHookModel<T> where T : class, new()
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "created_at")]
public DateTime CreatedAt { get; set; }
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }
[JsonProperty(PropertyName = "object")]
public T Object { get; set; }
[JsonIgnore]
public string WebHookAction
{
get
{
return string.IsNullOrEmpty(Type) ? string.Empty : Type.Split('.').Last();
}
}
}
Then created the following interface:
public interface IWebHookModelFactory<T> where T : class, new()
{
WebHookModel<T> GetWebHookModel(string type, string jsonPayload);
}
What I'm failing to understand is how am I supposed to implement the Factory class without knowing what the type is at compile time?
Playing around with the Model a bit, I changed it to an abstract class with an abstract T object so that it could be defined by a derived class.
public abstract class WebHookModel<T> where T : class, new()
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "created_at")]
public DateTime CreatedAt { get; set; }
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }
[JsonProperty(PropertyName = "object")]
public abstract T Object { get; set; }
[JsonIgnore]
public string WebHookAction
{
get
{
return string.IsNullOrEmpty(Type) ? string.Empty : Type.Split('.').Last();
}
}
}
public PersonWebHookModel : WebHookModel<Person>
{
public override Person Object { get; set; }
}
But I still run into the same issue of trying to implement an interface in which I don't know the type at runtime. From what I've found online, this is an example of covariance, but I haven't found any articles that explain how to resolve this issue. Is it best to skip generics and create a massive
case statement?
public interface IWebHookFactory<TModel, TJsonObject>
where TJsonObject : class, new()
where TModel : WebHookModel<TJsonObject>
{
TModel GetWebHookModel(string type, string jsonPayload);
}
I'm a bit partial to using the abstract class approach because it lets me define individual handlers based on which model I'm passing into my Service.
public interface IWebHookService<TModel, TJsonObject>
where TJsonObject : class, new()
where TModel : WebHookModel<TJsonObject>
{
void CompleteAction(TModel webHookModel);
}
public abstract class BaseWebhookService<TModel, TJsonObject> : IWebHookService<TModel, TJsonObject>
where TJsonObject : class, new()
where TModel : WebHookModel<TJsonObject>
{
public void CompleteAction(TModel webHookModel)
{
var self = this.GetType();
var bitWise = System.Reflection.BindingFlags.IgnoreCase
| System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic;
var methodToCall = self.GetMethod(jsonObject.WebHookAction, bitWise);
methodToCall.Invoke(this, new[] { jsonObject });
}
protected abstract void Created(TModel webHookObject);
protected abstract void Updated(TModel webHookObject);
protected abstract void Destroyed(TModel webHookObject);
}
public class PersonWebHookService : BaseWebHookService<PersonWebHookModel, Person>
{
protected override void Created(PersonWebHookModel webHookModel)
{
throw new NotImplementedException();
}
protected override void Updated(PersonWebHookModel webHookModel)
{
throw new NotImplementedException();
}
protected override void Destroyed(PersonWebHookModel webHookModel)
{
throw new NotImplementedException();
}
}
Key points for the solution:
1. There needs to be some virtual call in there somewhere.
2. Somehow you need to map from your type tag in your JSON payload to your actual C# class.
IE, "person.created"," --> 'Person'.
If you control the serialization format, JSON.Net can inject its own type tag and do this for you. Assuming you can't go that route ...
So you'll need something like a Dictionary to contain the mapping.
Assuming your definitions is like:
abstract class WebhookPayload // Note this base class is not generic!
{
// Common base properties here
public abstract void DoWork();
}
abstract class PersonPayload : WebhookPayload
{
public override void DoWork()
{
// your derived impl here
}
}
And then you can deserialize like:
static Dictionary<string, Type> _map = new Dictionary<string, Type>
{
{ "person.created", typeof(PersonPayload)}
}; // Add more entries here
public static WebhookPayload Deserialize(string json)
{
// 1. only parse once!
var jobj = JObject.Parse(json);
// 2. get the c# type
var strType = jobj["type"].ToString();
Type type;
if (!_map.TryGetValue(strType, out type))
{
// Error! Unrecognized type
}
// 3. Now deserialize
var obj = (WebhookPayload) jobj.ToObject(type);
return obj;
}

Deserializing JSON response which differ in each response

I have complicated JSON respone which containt about 100 attributes/objects/arrays and have difrrent responses in terms of object/array.
Firstly I have structure like that (when object exists)
{
'att1': 'desc',
'att2': '83482',
'att3': null,
'test': {
'object_array1': [
100
],
'object_array2': [
'desc'
]
}
}
public class Root
{
//fields here
public Test test { get; set; }
}
public class Test
{
public List<int> object_array1 { get; set; }
public List<string> object_array2 { get; set; }
}
The issue I have is when this objects is empty.
After that resposne is changing and returning empty array.
So it is chaning to this:
{
'att1': 'desc',
'att2': '83482',
'att3': null,
'test': [
]
}
And beacause of that I have standard error:
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Test' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I was trying to write customconverter with something like that:
private bool IsArray(string fieldName, JObject jObject)
{
return jObject[fieldName].Type == JTokenType.Array;
}
I'm using JSON.NET
But I failed miserably. Any help would be much appreciated
Use custom converter which would check token type that has been read in the ReadJson method and substitute some default value for test when the token is of type JArray (assuming it can only be an array when the test object is "empty"):
public class Root
{
[JsonConverter(typeof(TestIgnoreEmptyListConverter))]
public Test test { get; set; }
}
// .................
public class TestIgnoreEmptyListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Test).IsAssignableFrom(objectType) || objectType.IsArray;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token is JArray)
return default(Test);
else
return token.ToObject<Test>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Demo: https://dotnetfiddle.net/Q3S5hX
Your JSON propert name does not match object's name. Serializer tries to serialize/deserialize using property name or attributes.
Change this to the right name
public Test test { get; set; }
Or use attribute to set json name for a property.
[JsonProperty("test")]
public Test objects { get; set; }
Or even better, refactor your code to respect C# conventions. (I suggest to rename fields to be more descriptive)
public class Root
{
[JsonProperty("test")]
public Test Test { get; set; }
}
public class Test
{
[JsonProperty("object_array1")]
public List<int> IntArray { get; set; }
[JsonProperty("object_array2")]
public List<string> StringArray { get; set; }
}

Deserialise IList<Interface> in JSON.NET

I have some json that I want to Deserialise. It's very easy to deserialise back to an List However I am working with interfaces, and this is what I need the deserialisation to end up at:
So Let's start with some JSON:
[{"TimeGenerated":"2017-05-30T19:21:06.5331472+10:00","OuterValue":1.08,"Identifier":{"IdentifierName":"BLA","IdentifierNumber":1},"Locale":{"LocaleName":"LOCALE NAME","LocaleType":1,"LocaleNumber":1,"StartTime":"2017-05-30T19:20:00+10:00"},"InnerValue":1.08,"InnerType":0,"InnerId":"InnerI","Type":"MyType","Id":"11111"}]
This is the data that is Serialised on the server side (without issue of course) and it's object type is :
IList<IRootObject>
So effectively, I want to deserialise the other side as:
IList<IRootObject> MyDeserialisedObject = JsonConvert.Deserialise<IList<IRootObject>>(JsonString);
Obviously the pickle here is the old interface/abstract type error of:
'Could not create an instance of type BLA. Type is an interface or abstract class and cannot be instantiated'
Fine, I create some custom converters and decorate as follows:
Classes are:
[JsonObject(MemberSerialization.OptIn)]
public class Identifier
{
[JsonProperty]
public string IdentifierName { get; set; }
[JsonProperty]
public int IdentifierNumber { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class Locale
{
[JsonProperty]
public string LocaleName { get; set; }
[JsonProperty]
public int LocaleType { get; set; }
[JsonProperty]
public int LocaleNumber { get; set; }
[JsonProperty]
public string StartTime { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class RootObject:IRootObject
{
[JsonProperty]
public string TimeGenerated { get; set; }
[JsonProperty]
public double OuterValue { get; set; }
[JsonProperty]
[JsonConverter(typeof(ConcreteConverter<Identifier>))]
public IIdentifier Identifier { get; set; }
[JsonProperty]
[JsonConverter(typeof(ConcreteConverter<Locale>))]
public ILocale Locale { get; set; }
[JsonProperty]
public double InnerValue { get; set; }
[JsonProperty]
public int InnerType { get; set; }
[JsonProperty]
public string InnerId { get; set; }
[JsonProperty]
public string Type { get; set; }
[JsonProperty]
public string Id { get; set; }
}
Interface is:
public interface IRootObject
{
string TimeGenerated { get; set; }
double OuterValue { get; set; }
Identifier Identifier { get; set; }
Locale Locale { get; set; }
double InnerValue { get; set; }
int InnerType { get; set; }
string InnerId { get; set; }
string Type { get; set; }
string Id { get; set; }
}
Concrete Converter is as follows:
public class ConcreteConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
var s = serializer.Deserialize<T>(reader);
return s;
}
catch (Exception ex)
{
return null;
}
// return serializer.Deserialize<T>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
I also have a list converter which I have made (seeing this is a list albeit this is unused, but here JIC it is needed):
public class ConcreteListConverter<TInterface, TImplementation> : JsonConverter where TImplementation : TInterface
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (existingValue == null)
return null;
var res = serializer.Deserialize<List<TImplementation>>(reader);
return res.ConvertAll(x => (TInterface)x);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
So the issues are:
When I run:
IList<IRootObject> MyDeserialisedObject = JsonConvert.Deserialise<IList<IRootObject>>(JsonString);
I always get a null returned.
When i put breakpoints on my ConcreteConverter CanConvert/ReadJson/WriteJson methods - they are never hit.
What am I missing, and how do i get the magic of:
IList<IRootObject> MyDeserialisedObject = JsonConvert.Deserialise<IList<IRootObject>>(JsonString);
to work properly.
I must note, I DO NOT want to just typnamehandling.objects (i think this is it) where it adds the ASSEMBLYNAME.TYPE etc into the json, thus bloating it. Just assume that the json above is what we have to work with, and the classes represented above, with their interfaces is what it must be deserialised to.
Regards,
Chud
PS - I have changed the class names/properties etc away from the actual real world implementation of what I am doing. So if they have come out with an error, I do apologise. But hopefully the majority is all ok, and the point/ultimate goal is understood :)
The Problem
You cannot create instances of an interface, that is not allowed in .NET. You cannot do this:
var items = new IList<int>(); // Not allowed
Your error is clearly stating that:
Could not create an instance of type BLA. Type is an interface or abstract class and cannot be instantiated'
The Solution
Deserialize it to type List but assign it to type IList. Please note the types on the right side are not interfaces in code below except at the casting stage.
IList<IRootObject> MyDeserialisedObject =
JsonConvert.DeserializeObject<List<RootObject>>(File.ReadAllText(JsonString))
.Cast<IRootObject>().ToList();
Some More Belabor
You may ask "Why do I need to cast" and why can I not do this instead?
IList<IRootObject> MyDeserialisedObject =
JsonConvert.DeserializeObject<List<RootObject>>(File.ReadAllText(JsonString));
That is not allowed in order to preserve reference identity.
You are trying to get JsonConvert to instantiate interfaces, which is not possible.
The problem is here:
IList<IRootObject> MyDeserializedObject = JsonConvert.Deserialize<IList<IRootObject>>(JsonString);
JsonConvert will process the json data you provide it.
Because you ask to convert it to an object of type IList<IRootObject>, JsonConvert will iteratively build a list of IRootObject objects.
For every element found in the list (inside the json data string), JsonConvert will create a new IRootObject() and populate its properties with the data it finds in the json data.
And that's the problem: new IRootObject() is not possible, because you cannot instantiate an interface. You can only instantiate classes.
Solution:
You already have a class RootObject. It already contains the same fields that your interface contains. However, RootObject is currently not implementing IRootObject.
Add the interface implementation to the class:
public class RootObject : IRootObject
{
//make sure the implementation is correct
}
And then you can ask JsonConvert to give you a list of RootObject (not IRootObject!)
List<RootObject> MyDeserializedRootObjects = JsonConvert.Deserialize<List<RootObject>>(JsonString);
And if you want to turn the List<RootObject> into a IList<IRootObject>:
IList<IRootObject> MyDeserializedIRootObjects = MyDeserializedRootObjects.ToList<IRootObject>();
In case you are using .NET 3.5 or lower, you can cast the list as follows:
IList<IRootObject> MyDeserializedIRootObjects = MyDeserializedRootObjects .Cast<IRootObject>().ToList();
edit I may have done it a bit sneakily, but for the same reason that you cannot instantiate an IRootObject, you also cannot instantiate an IList. Both of these are interfaces.
The solution is the same for both: instantiate an object of a class that implements the interface (List and RootObject), and then cast them to their interfaces (IList and IRootObject).
I believe you are never using your custom converter. According to the Newtonsoft.Json documentation (here), you should be using something like this:
IList<IRootObject> MyDeserialisedObject = JsonConvert.Deserialise<IList<IRootObject>>(JsonString, new ConcreteListConverter());
Ignore the i++ that's just my iteration through my database columns. My reader here was reading a JSON object of type Student from the database and I used an IList like so and it worked. If it provides more clarity, IList is an interface and List is a concrete class or better yet List is a concrete type that implements the IList interface.
course.Student = reader.DeserializeObject<IList<Student>>(i++);

Why is Json Dot Net not even trying to evaluate my interface properties?

I have an object where I'm trying to intercept the deserialization process for two properties which are interfaces. I'm using Json Dot Net.
The issue that I'm facing is that Json Dot Net doesn't even try to convert the two IDataStore properties. In other words, 'CanConvert' is never even run for those types.
I've tried adding JsonProperty and JsonConverter attributes and still no dice.
Any insight would be appreciated.
Edit (and Answer) After being asked by Ron Beyer to post the JSON, it became clear that Json Dot Net doesn't even consider operating on a property if the source JSON doesn't have it. When adding '"SourceDataStore":{}' it tries to convert that property. I was thinking it would look at the current object type and iterate over that but there's obviously a matching process beforehand
JSON
{
"Name":"My First Definition",
"SourceDataStoreType":"SqlDataStore",
"DestinationDataStoreType":"MongoDataStore"
}
Class
internal class Definition
{
public string Name { get; set; }
public DataStoreTypes SourceDataStoreType { get; set; }
public DataStoreTypes DestinationDataStoreType { get; set; }
public IDataStore SourceDataStore { get; set; }
public IDataStore DestinationDataStore { get; set; }
public Definition()
{
}
}
Converter
public class DataStoreConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
var b = objectType == typeof(IDataStore);
return b;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return new SqlDataStore();
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Code
var definition = JsonConvert.DeserializeObject<Definition>(definitionsJson, new JsonSerializerSettings
{
Converters = new[] { new DataStoreConverter() }
});
return definition;
(Comments to an answer)
I asked for the JSON example to verify that 1) the JSON was well formatted and 2) included the properties in question.
The issue, it turns out that you figured out, is that the JSON deserializer will not attempt to run the conversion on any properties it does not find in the JSON. This is because the deserializer parses the JSON and looks for appropriate properties in the object to fill in. If the property doesn't exist in the JSON, it won't attempt to build that property from nothing.
There are two solutions to this, either you can add a null (blank) property for those in the JSON, or you can edit your object to create them when you run the constructor, something like:
internal class Definition
{
public string Name { get; set; }
public DataStoreTypes SourceDataStoreType { get; set; }
public DataStoreTypes DestinationDataStoreType { get; set; }
public IDataStore SourceDataStore { get; set; }
public IDataStore DestinationDataStore { get; set; }
public Definition()
{
var container = YourIOCContainerHere.Instance;
SourceDataStore = container.Resolve<IDataStore>();
DestinationDataStore = container.Resolve<IDataStore>();
//Or, without using IOC/DI
SourceDataStore = new SqlDataStore();
DestinationDataStore = new SqlDataStore();
}
}

Categories