This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, Mindscape team I would like to know if it's possible to help me? I've got a Web Api project which I use to expose different api's for my project. When I request XML I get the < Id > value but some of the other fields are missing like CreatedOn, LockVersion, etc... So I switched to JSON.Net and now I get all the required fields except the Id field. Any ideas on how to fix this? Kind regards, Johan |
|
|
You will need to use a custom contract resolver within JSON.NET to cover this. I am assuming you already have DataContract attributes applied in your model since you are getting out the bulk of your properties, so here is a quick example that "works on my machine" which should give you what you are after :) Note that EntityInfo is coming from the Mindscape.LightSpeed.MetaData assembly.
I applied this by using the following within my JsonNetFormatter class which was applied to the WebAPI configuration
Where the serializer is subsequently used in the JsonSerializer serializer = JsonSerializer.Create(serializerSettings) calls for serialize/deserialize. If you happened to not be using the DataContract attributes then your best bet would be to not use the underlying base contract (or fetch it and scrub it of all properties) and then create each property manually found in the entityInfo.Fields collection.
|
|
|
Thank you Jeremy. It works on my machine as well :) Kind regards, Johan |
|
|
Jeremy, it's working on my machine via Fiddler the last problem which I still have is that when I deserialise the Json the Lightspeed entity's Id is not set. Any idea how I can resolve this? Cheers |
|
|
Id is not settable (just as it is not in code) so you would need to wire this up against the underlying "IdInternal" property to allow it to be settable. Something like this should be what you are after (I have not tested this though):
|
|
|
Using the above EntityContractResolver class, here is how I was able to return the correct JSON from a WebApi2 controller. Notes: 1) vwItemMaster is the Entity from the lightspeed model, which corresponds to a View in the database. 2) I added ID column in the View (since my PK is not called ID), so I had to comment out the "contract.Properties.Add(new JsonProperty()" line in the above EntityContractResolver class.
HTH |
|