I have the following JSON in the request body:
{
"tableName": "ApiTestTbl",
"columnsValues": {
"test1": "value1",
"column2": "value2"
}
}
And I have these 2 classes:
public class InsertIntoDbEntity
{
public string tableName { get; set; }
public InsertIntoDbColumnsValuesEntity columnsValues { get; set; }
}
public class InsertIntoDbColumnsValuesEntity
{
public Dictionary<string, string> columnsValues { get; set; }
}
And the controller function is the following:
[HttpPost]
[Route("app/singleinsert")]
public CrudReturnEntity InsertIntoDB(InsertIntoDbEntity entity)
{
return null;
}
The value of tableName is well assigned "ApiTestTbl", but columnsValues is always null.
I am using a Dictionary because the number of key-value pairs in columnValues is not defined, so I want to store them in a Dictionary for processing.
Thanks!
As #Knoop suggested in the comments, it's rather straightforward in your case, to have the Dictionary<string,string> directly as a member of your InsertIntoDbEntity model.
However in cases where you do not have this feasibility and have the requirement for a strict structure, following is the way to solve it. We are going to build a custom Json Converter for the inner property, making use of Newtonsoft.Json.
Creating a custom JSON converter for inner property
public class MyInternalConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if(reader.TokenType == JsonToken.StartObject)
{
JObject item = JObject.Load(reader);
// You may want to perform any sanity checks you want here
Dictionary<string, string> blah = JsonConvert.DeserializeObject<Dictionary<string, string>>(item.ToString());
return new InsertIntoDbColumnsValuesEntity
{
columnsValues = blah
};
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You also need to decorate the property within your model to leverage the custom converter written. This is done as shown below
public class InsertIntoDbEntity
{
public string tableName { get; set; }
[JsonConverter(typeof(MyInternalConverter))]
public InsertIntoDbColumnsValuesEntity columnsValues { get; set; }
}
Essentially, the same technique can be leveraged at the property level or class level depending on the need for a custom deserialization.
Related
From an external API I am receiving the below JSON response for the bank details of a customer.
{
"bankDetails":[
{
"ABC Bank":[
{
"sNo":1,
"acNo":"1235465",
"acBalance":"100.25"
},
{
"sNo":2,
"acNo":"1235467",
"acBalance":"50.25"
}
],
"bankName":"ABC Bank",
"totalAmount":"150.50"
},
{
"XYZ Bank":[
{
"sNo":1,
"acNo":"1248565",
"acBalance":"75.25"
}
],
"bankName":"XYZ Bank",
"totalAmount":"75.25"
},
{
"BCD Bank":[
{
"sNo":1,
"acNo":"145665",
"acBalance":"10.25"
},
{
"sNo":2,
"acNo":"195267",
"acBalance":"5.25"
}
],
"bankName":"BCD Bank",
"totalAmount":"15.50"
}
]
}
I need to deserialize this to a C# class using JSON.Net. What should be structure of the C# class as the first key is dynamic?. The first key with bank name returned will be different for each customer
The typical solution to dealing with dynamic keys is to use a Dictionary<string, T> in place of a regular class. See How can I deserialize a child object with dynamic (numeric) key names? for an example of this. However, that solution doesn't really work for your case, because there are other properties in the same object which do not have dynamic keys (the bankName and totalAmount), and the values of those properties are primitives whereas the value of dynamic property is an array of bank accounts. A better solution here is to use a JsonConverter.
Before we get to that, we need to set up a class structure to deserialize into. This is pretty straightforward:
class RootObject
{
public List<Bank> BankDetails { get; set; }
}
[JsonConverter(typeof(BankConverter))]
class Bank
{
public string BankName { get; set; }
public decimal TotalAmount { get; set; }
public List<Account> Accounts { get; set; }
}
class Account
{
[JsonProperty("sNo")]
public int SequenceNumber { get; set; }
[JsonProperty("acNo")]
public string AccountNumber { get; set; }
[JsonProperty("acBalance")]
public decimal Balance { get; set; }
}
You'll notice that I've added a few [JsonProperty] attributes in the Account class to map the shorthand property names in the JSON to friendlier property names in that class. And the [JsonConverter] attribute on the Bank class tells the serializer that we will be using a custom BankConverter to handle that class.
Here is the code for the BankConverter. It uses a JObject internally to make it easier to read and work with the JSON.
class BankConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Bank);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject obj = JObject.Load(reader);
Bank bank = new Bank();
// populate the known properties (bankName and totalAmount)
serializer.Populate(obj.CreateReader(), bank);
// now handle the dynamic key
bank.Accounts = obj[bank.BankName].ToObject<List<Account>>(serializer);
return bank;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
With these classes in place, you can deserialize the JSON like this:
var root = JsonConvert.DeserializeObject<RootObject>(json);
Here is a working demo: https://dotnetfiddle.net/knsRLv
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; }
}
Here is a simplified version of the model I have to work with:
class InputModel
{
public string Name { get; set; }
public object Value { get; set; }
}
And the relevant parts of the controller:
class Controller : ApiController
{
[HttpPut]
public async Task<IHttpActionResult> Update([FromBody]InputModel model)
{
//implementation
}
}
the Value property of the InputModel class could be of any type, and which type it will be is only known later on, in a piece of legacy code the model is sent to and that i have no control over.
The problem I have occurs with the following json in the request body:
{
"name": "X",
"value": "2001-10-17T13:55:11.123"
}
The default behavior is to parse this json so that the Value property gets converted to a DateTime. DateTimes however are handled very differently from strings in the legacy code and data is lost after handling it (for example: the millisecond part is removed when persisting to the database). So when the same value is later requested, the returned value is "2001-10-17T13:55:11" (milliseconds missing).
Of course I can fix this by globally setting this in my web api configuration:
httpConfiguration.Formatters.JsonFormatter.SerializationSettings.DateParseHandling = DateParseHandling.None;
But doing so disables parsing DateTimes also for models in other methods and controllers that have models for which the default behavior is wanted.
What I'm looking for is something like the following (imaginary) code:
class InputModel
{
public string Name { get; set; }
[JsonSerializerSettings(DateParseHandling = DateParseHandling.None)]
public object Value { get; set; }
}
But I can't find out how to achieve this. Any help would be greatly appreciated.
What one can do is to add a custom JsonConverter to the InputModel type to temporarily toggle JsonReader.DateParseHandling to None:
[JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)]
class InputModel
{
public string Name { get; set; }
public object Value { get; set; }
}
public class DateParseHandlingConverter : JsonConverter
{
readonly DateParseHandling dateParseHandling;
public DateParseHandlingConverter(DateParseHandling dateParseHandling)
{
this.dateParseHandling = dateParseHandling;
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var old = reader.DateParseHandling;
try
{
reader.DateParseHandling = dateParseHandling;
existingValue = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, existingValue);
return existingValue;
}
finally
{
reader.DateParseHandling = old;
}
}
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Note that, if the JSON to be deserialized contains nested arrays or objects, all recursively contained values will be parsed with DateParseHandling.None.
One might ask, why not add a converter directly to the property, like so?
class InputModel
{
public string Name { get; set; }
[JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)]
public object Value { get; set; }
}
It turns out that this does not work because, at the time JsonConverter.ReadJson() is called, the reader has already advanced to the date string and tokenized it as a DateTime. Thus the converter must be applied to the containing type.
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();
}
}
This is my json
{
"accessType":"Grant",
"spaces":[{"spaceId":"5c209ba0-e24d-450d-8f23-44a99e6ae415"}],
"privilegeId":"db7cd037-6503-4dbf-8566-2cca4787119d",
"privilegeName":"PERM_RVMC_DEV",
"privilegeDescription":"RVMC Dev",
"privilegeType":"Permission"
}
And here's my class:
public class ProfilePrivilege
{
public AccessType AccessType { get; set; }
public Guid PrivilegeId { get; set; }
public string PrivilegeName { get; set; }
public string PrivilegeDescription { get; set; }
public PrivilegeType PrivilegeType { get; set; }
public List<Guid> spaces;
}
When the spaces array is not null I get an error deserializing. I can get around this by simply creating a wrapper class for Guid
public class Space
{
public Guid spaceId;
}
and then instead of List<Guid> I can have a List<Space> in my Privilege class and it's all fine. But I was wondering if there's a better way to do this as I don't want to have a redundant wrapper class just for that. So is there any easy way to get around this without writing a custom deserializer for my Privilege type objects ?
I'm deserializing with JSON.Net btw.
You can use a simple JsonConverter class to flatten the spaces object array to a list of GUIDs, thereby eliminating the need for the wrapper class.
Here is the code you would need for the converter:
class SpaceListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(List<Guid>));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return JArray.Load(reader)
.Children<JObject>()
.Select(jo => Guid.Parse((string)jo["spaceId"]))
.ToList();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To use it, annotate the Spaces property in your ProfilePrivilege class with a [JsonConverter] attribute like this:
public class ProfilePrivilege
{
...
[JsonConverter(typeof(SpaceListConverter))]
public List<Guid> Spaces;
...
}
Then, when you deserialize, everything should "just work".
Full demo here: https://dotnetfiddle.net/EaYgbe
You don't necessarily need to create POCOs. An alternative is to use dynamics:
dynamic d = JObject.Parse(jsonString);
Console.WriteLine(d.accessType);
Console.WriteLine(d.spaces.Count);
Console.WriteLine(d.spaces[0].spaceId);