Deserialization and throw exception for required property when it is null - c#

I'm probably missing something.
I'd like to test behavior when an API call response returns a property of null and I would like to throw an exception when that happens.
I have a Form object as follow
public class Form
{
[Required]
public string Html { get; set; }
public string Json { get; set; }
public string Name { get; set; }
}
I have initialized an object
var myData = new
{
Json = "foo",
};
string jsonData = JsonConvert.SerializeObject(myData);
var response = JsonConvert.DeserializeObject<Form>(File.ReadAllText(jsonPath));
I was expecting to have an exception since the Html property is required and not nullable,
but actually getting the object as
{
Html = null,
Json = foo,
Name = null
}
I have tried to use JsonSerializerSettings as follows but this actually throws an exception only when there is an additional unwanted property and not when there's a missing one.
JsonSerializerSettings config = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error, NullValueHandling = NullValueHandling.Include};
var res = JsonConvert.DeserializeObject<Form>(json,config);

You need [JsonProperty(Required = Required.Always)].
public class Form
{
[JsonProperty(Required = Required.Always)]
public string Html { get; set; }
public string Json { get; set; }
public string Name { get; set; }
}
Sample Program
While [Required] attribute is for Data Annotation.
Reference
JsonPropertyAttribute required

Related

How to Deserialize Json Array

I have a Json Response to deserialize, after doing the deserialize I get error.
This is the Json array I want to deserialize:
{
"status": 200,
"success": true,
"message": "Success",
"data": [
{
"transaction_reference": "REF202301031128311645_1",
"virtual_account_number": "1234567890",
"principal_amount": "200.00",
"settled_amount": "199.80",
"fee_charged": "0.20",
"transaction_date": "2023-01-03T00:00:00.000Z",
"transaction_indicator": "C",
"remarks": "Transfer FROM Transfer | [1234567] TO Merchant Name",
"currency": "NGN",
"alerted_merchant": false,
"merchant_settlement_date": "2023-01-03T10:29:25.847Z",
"frozen_transaction": null,
"customer": {
"customer_identifier": "1234567"
}
}
]
}
From the Array, I want to get the following as variable:
status,
transaction_reference,
Virtual_account_number,
principal_amount,
customer_identifier,
below is what I tried:
string data = response.Content;
string dataT = data.Replace("", string.Empty);
dynamic stuff = JObject.Parse(dataT);
dynamic status = stuff.status;
var JsonResponse = stuff.data;
var ResponseX = JsonNode.Parse(JsonResponse); // Deserializing json Array
dynamic transaction_reference = ResponseX[0]["transaction_reference"];
dynamic virtual_account_number = ResponseX[1]["virtual_account_number"];
dynamic principal_amount = ResponseX[2]["principal_amount"];
dynamic customer = ResponseX[13]["customer_identifier"];
dynamic customer_identifier = customer.customer_identifier;
The error I got is as below
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500 Message=The best overloaded method match for
'System.Text.Json.Nodes.JsonNode.Parse(ref
System.Text.Json.Utf8JsonReader,
System.Text.Json.Nodes.JsonNodeOptions?)' has some invalid arguments
Source= StackTrace:
The error occurred at the line
var ResponseX = JsonNode.Parse(JsonResponse); // Deserializing json Array
What I really want achieve is to separate the following and get each as variable:
status,
transaction_reference,
Virtual_account_number,
principal_amount,
customer_identifier,
Please can someone point to me where my error is?
you have too much code that you don't need, if you don't want to deserialize to c# class, you can do it this way too
var ResponseX = JsonNode.Parse(response.Content); // Parse the response string
int status= (int)ResponseX["status"];
var data=ResponseX["data"][0];
string transaction_reference = (string) data["transaction_reference"];
string virtual_account_number = (string) data["virtual_account_number"];
string principal_amount = (string) data["principal_amount"];
string customer = (string) data["customer_identifier"];
int customer_identifier = Convert.ToInt32 ((string) data["customer"]["customer_identifier"]);
another way is to deserialize data to c# class
Datum d = System.Text.Json.JsonSerializer.Deserialize<Datum>(data.AsObject());
string transaction_reference = d.transaction_reference;
string virtual_account_number = d.virtual_account_number;
int customer_identifier = Convert.ToInt32( d.customer.customer_identifier);
public class Datum
{
public string transaction_reference { get; set; }
public string virtual_account_number { get; set; }
public string principal_amount { get; set; }
public string settled_amount { get; set; }
public string fee_charged { get; set; }
public DateTime transaction_date { get; set; }
public string transaction_indicator { get; set; }
public string remarks { get; set; }
public string currency { get; set; }
public bool alerted_merchant { get; set; }
public DateTime merchant_settlement_date { get; set; }
public object frozen_transaction { get; set; }
public Customer customer { get; set; }
}
public class Customer
{
public string customer_identifier { get; set; }
}
It would be nice to know what the specific error is but from inspecting the JSON it looks like you are stepping into the wrong parent node.
Try
dynamic transaction_reference = ResponseX["data"][0]["transaction_reference"];
dynamic virtual_account_number = ResponseX["data"][1]["virtual_account_number"];
dynamic principal_amount = ResponseX["data"][2]["principal_amount"];
dynamic customer = ResponseX["data"][13]["customer_identifier"];
dynamic customer_identifier = customer.customer_identifier;
Also, make sure the array has at least 14 entries under the parent data.
You are passing an invalid into JsonNode.Parse().
The error is thrown at runtime because you use dynamic as type and the compiler does not know the type at compile time.
In line dynamic stuff = JObject.Parse(dataT); you are using Newtonsoft to parse the data.
In Line var JsonResponse = stuff.data; you are writing a variable of type Newtonsoft.Json.Linq.JArray.
Then you try to call JsonNode.Parse(JsonResponse); which is not possible with the given argument, because the type of JsonResponse is not valid.
You will get this error on compile time, when you use the "real" types instead of dynamic.
To fix your issue, you should use the same function for parsing.

Deserializing from BsonDocument to string and serializing back to BsonDocument

I have a requirement where I need a property that is actually a JSON value from a MongoDB collection that needs to be deserialized into a string. This conversion is throwing a "Cannot deserialize a 'String' from a BsonType 'Document'" exception.
I tried implementing a JSON custom converter, but as the value is being treated as a BsonDocument, it is not helping and I am getting the same exception. I also need it in the original format as I need to cast it back into a BsonDocument down the line. I guess I would need a custom Bson serializer/deserializer.
Incoming sample document from MongoDB collection:
{
"name": "Jane Doe",
"dob": {
"month": "Sep",
"day": 09,
"year": 1987
}
}
Type it is expecting for deserialization:
public class Person
{
public string name { get; set; }
public Dob dob { get; set; }
public class Dob
{
public string month { get; set; }
public int day { get; set; }
public int year { get; set; }
}
}
Type I want it to deserialize into:
public class Person
{
public string name { get; set; }
public string dob { get; set; }
}
To summarize, you have a public-facing string property on your model that contains JSON which you would like to internally serialize to MongoDB by deserializing the JSON string to some intermediate DTO, then serializing the DTO itself to Mongo.
Here are a couple of approaches to solving your problem.
Firstly, you could introduce a private DTO-valued property Dob SerializedDOB { get; set; } into your data model, mark that property with [BsonElement("dob")] to force it to be serialized, then modify dob to be a non-serialized surrogate property that serializes from and to the underlying SerializedDOB within its getter and setter. The following code shows this approach:
public class Person
{
public string name { get; set; }
[BsonIgnore]
public string dob
{
get => BsonExtensionMethods.ToJson(SerializedDOB);
set => SerializedDOB = MyBsonExtensionMethods.FromJson<Dob>(value);
}
[BsonElement("dob")]
Dob SerializedDOB { get; set; }
class Dob // The DTO
{
public string month { get; set; }
public int day { get; set; }
public int year { get; set; }
}
}
The advantage of this approach is that, by making the JSON string a surrogate, the setter automatically ensures that it is well-formed.
Demo fiddle #1 here.
Secondly, you could create a custom SerializerBase<string> for dob that maps the string value to and from the DTO Dob during (de)serialization. The following code shows this approach:
public class Person
{
public string name { get; set; }
[BsonSerializer(typeof(JsonStringAsObjectSerializer<Dob>))]
public string dob { get; set; }
class Dob // The DTO
{
public string month { get; set; }
public int day { get; set; }
public int year { get; set; }
}
}
public class JsonStringAsObjectSerializer<TObject> : SerializerBase<string> where TObject : class
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, string value)
{
if (value == null)
{
var bsonWriter = context.Writer;
bsonWriter.WriteNull();
}
else
{
var obj = MyBsonExtensionMethods.FromJson<TObject>(value);
var serializer = BsonSerializer.LookupSerializer(typeof(TObject));
serializer.Serialize(context, obj);
}
}
public override string Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
var serializer = BsonSerializer.LookupSerializer(typeof(TObject));
var obj = (TObject)serializer.Deserialize(context);
return (obj == null ? null : BsonExtensionMethods.ToJson(obj));
}
}
The advantage of this approach is that JsonStringAsObjectSerializer<TObject> can be reused whenever this requirement arises.
Demo fiddle #2 here.
The following extension method is used with both solutions to deserialize the JSON string to a specified type because, confusingly, BsonExtensionMethods has a ToJson() method but no FromJson() method:
public static partial class MyBsonExtensionMethods
{
// Not sure why but BsonExtensionMethods.cs seems to lack methods for deserializing from JSON, so I added some here.
// See https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Bson/BsonExtensionMethods.cs
public static TNominalType FromJson<TNominalType>(
string json,
JsonReaderSettings readerSettings = null,
IBsonSerializer<TNominalType> serializer = null,
Action<BsonDeserializationContext.Builder> configurator = null)
{
return (TNominalType)FromJson(json, typeof(TNominalType), readerSettings, serializer, configurator);
}
public static object FromJson(
string json,
Type nominalType,
JsonReaderSettings readerSettings = null,
IBsonSerializer serializer = null,
Action<BsonDeserializationContext.Builder> configurator = null)
{
if (nominalType == null || json == null)
throw new ArgumentNullException();
serializer = serializer ?? BsonSerializer.LookupSerializer(nominalType);
if (serializer.ValueType != nominalType)
throw new ArgumentException(string.Format("serializer.ValueType {0} != nominalType {1}.", serializer.GetType().FullName, nominalType.FullName), "serializer");
using (var textReader = new StringReader(json))
using (var reader = new JsonReader(textReader, readerSettings ?? JsonReaderSettings.Defaults))
{
var context = BsonDeserializationContext.CreateRoot(reader, configurator);
return serializer.Deserialize(context);
}
}
}

Dynamic JSON parsing in. net core

We have N number of JSON parameters and class properties as well but
have to remove dynamically the JSON parameters which are not available in class properties while serializing.
If I use [JsonIgnore] it is only removing values, not the entire property; we need to remove the entire property.
Example:
JSON request:
{
"Name":"ABC",
"Age":26,
"Designation":"Er",
"Place":"Pune",
"Gender":"Male"
}
Class:
[Serializable]
public class SampleProperties
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Age")]
public int Age { get; set; }
[JsonProperty("Designation")]
public string Designation { get; set; }
}
Result Expecting :
{
"Name":"ABC",
"Age":26,
"Designation":"Er"
}
the best way to do this is to create an object with 30 fields and deserialize the json string to this object
try somthing like this :
class MyObject
{
public string prop1 {get;set; }
public string prop2 {get;set; }
}
then :
string json = "your json";
MyObject objectWith30Fields = JsonConvert.DeserializeObject<MyObject>(json);
You can set NullValueHandling like the code below that you can see on the documentation of Newtonsoft.Json or in this link as well, in addition, you can use ExpandoObject() as you can see on this link
public class Movie
{
public string Name { get; set; }
public string Description { get; set; }
public string Classification { get; set; }
public string Studio { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<string> ReleaseCountries { get; set; }
}
Movie movie = new Movie();
movie.Name = "Bad Boys III";
movie.Description = "It's no Bad Boys";
string included = JsonConvert.SerializeObject(movie,
Formatting.Indented,
new JsonSerializerSettings { });
// {
// "Name": "Bad Boys III",
// "Description": "It's no Bad Boys",
// "Classification": null,
// "Studio": null,
// "ReleaseDate": null,
// "ReleaseCountries": null
// }
string ignored = JsonConvert.SerializeObject(movie,
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
// {
// "Name": "Bad Boys III",
// "Description": "It's no Bad Boys"
// }
More about ExpandoObject

Use different name for serializing and deserializing with Json.Net

I am receiving JSON data from a web API that looks like this:
[
{
"id": 1
"error_message": "An error has occurred!"
}
]
I deserialize this data to objects of the following type:
public class ErrorDetails
{
public int Id { get; set; }
[JsonProperty("error_message")]
public string ErrorMessage { get; set; }
}
Later in my application I would like to serialize the ErrorDetails object again to JSON but using the property name ErrorMessage instead of error_message. So the result would look like this:
[
{
"Id": 1
"ErrorMessage": "An error has occurred!"
}
]
Is there an easy way I can accomplish this with Json.Net? Perhaps using a custom resolver and some attributes like:
public class ErrorDetails
{
public int Id { get; set; }
[SerializeAs("ErrorMessage")]
[DeserializeAs("error_message")]
public string ErrorMessage { get; set; }
}
But the resolver doesn't tell me when I'm serializing or deserializing.
You can make use of the JsonSerializerSettings, the ContractResolver and the NamingStrategy.
public class ErrorDetails
{
public int Id { get; set; }
public string ErrorMessage { get; set; }
}
var json = "{'Id': 1,'error_message': 'An error has occurred!'}";
For dezerialization you could use the SnakeCaseNamingStrategy.
var dezerializerSettings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
var obj = JsonConvert.DeserializeObject<ErrorDetails>(json, dezerializerSettings);
To serialize the object again you dont have to change the JsonSerializerSettings as the default will use the property name.
var jsonNew = JsonConvert.SerializeObject(obj);
jsonNew = "{'Id': 1,'ErrorMessage': 'An error has occurred!'}"
Or you could create a contract resolver which can decide which name to use. Then you can decide when you dezerialize and serialize if you want to use the pascal case name format or the one with the underscore.
public class CustomContractResolver : DefaultContractResolver
{
public bool UseJsonPropertyName { get; }
public CustomContractResolver(bool useJsonPropertyName)
{
UseJsonPropertyName = useJsonPropertyName;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (!UseJsonPropertyName)
property.PropertyName = property.UnderlyingName;
return property;
}
}
public class ErrorDetails
{
public int Id { get; set; }
[JsonProperty("error_message")]
public string ErrorMessage { get; set; }
}
var json = "{'Id': 1,'error_message': 'An error has occurred!'}";
var serializerSettings = new JsonSerializerSettings()
{
ContractResolver = new CustomContractResolver(false)
};
var dezerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver(true)
};
var obj = JsonConvert.DeserializeObject<ErrorDetails>(json, dezerializerSettings);
var jsonNew = JsonConvert.SerializeObject(obj, serializerSettings);
jsonNew = "{'Id': 1,'ErrorMessage': 'An error has occurred!'}"
Another way of achieving a different property name when serialising vs deserisalising is by using the ShouldSerialize method: https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm#ShouldSerialize
The docs say:
To conditionally serialize a property, add a method that returns
boolean with the same name as the property and then prefix the method
name with ShouldSerialize. The result of the method determines whether
the property is serialized. If the method returns true then the
property will be serialized, if it returns false then the property
will be skipped.
E.g:
public class ErrorDetails
{
public int Id { get; set; }
// This will deserialise the `error_message` property from the incoming json into the `GetErrorMessage` property
[JsonProperty("error_message")]
public string GetErrorMessage { get; set; }
// If this method returns false then the property after the `ShouldSerialize` prefix will not be serialised into the output
public bool ShouldSerializeGetErrorMessage() => false;
// The serialised output will return `ErrorMessage` with the value from `GetErrorMessage` i.e. `error_message` in the original json
public string ErrorMessage { get { return GetErrorMessage; } }
}
This results in slightly more overhead so be careful if dealing with lots of properties or with lots of data but for small payloads, and if you don't mind messing up your DTO class a little, then this could be a quicker solution than writing custom contract resolvers etc.
I liked the answer by #lee_mcmullen, and implemented it in my own code. Now I think I've found a slightly neater version.
public class ErrorDetails
{
public int Id { get; set; }
// This will deserialise the `error_message` property from the incoming json and store it in the new `GetErrorMessage` property
[JsonProperty("error_message")]
public string GetErrorMessage { get { return ErrorMessage; } set { ErrorMessage = value; } }
// If this method returns false then the property after the `ShouldSerialize` prefix will not be serialised into the output
public bool ShouldSerializeGetErrorMessage() => false;
// The serialised output will return `ErrorMessage` with the value set from `GetErrorMessage` i.e. `error_message` in the original json
public string ErrorMessage { get; set; }
}
The reason I like this better is that in more complicated models it allows for inheritance while keeping all of the "old" custom stuff separate
public class ErrorDetails
{
public int Id { get; set; }
public string ErrorMessage { get; set; }
}
// This is our old ErrorDetails that hopefully we can delete one day
public class OldErrorDetails : ErrorDetails
{
// This will deserialise the `error_message` property from the incoming json and store it in the new `GetErrorMessage` property
[JsonProperty("error_message")]
public string GetErrorMessage { get { return ErrorMessage; } set { ErrorMessage = value; } }
// If this method returns false then the property after the `ShouldSerialize` prefix will not be serialised into the output
public bool ShouldSerializeGetErrorMessage() => false;
}

JSON Deserialize Error: Parameter cannot be null

Couldn't find an answer from the other Json Serialization issue questions, so maybe someone can help me:
I'm getting a JSON object from a REST api and attempting to Deserialize it to an object. Below is the JSON Object I receive:
{"id":"6wVcZ9ZF67ECUQ8xuIjFT2",
"userId":"83ca0ab5-3b7c-48fe-8019-000320081b00",
"authorizations":["employee","API","trainer","queueAdmin","supervisor","workflowAdmin","realtimeManager","forecastAnalyst","qualityEvaluator","contactCenterManager","teamLead","personnelAdmin","telephonyAdmin","qualityAdmin","businessAdmin","businessUser","accountAdmin","dialerAdmin","contentManagementUser","contentManagementAdmin","admin","api","scriptDesigner","agent","user"],
"primaryAuthorization":"employee",
"thirdPartyOrgName":"in",
"username":"somebody",
"selfUri":"https://blahblahblah.com/api/v1/auth/sessions/6wVcZ9ZF67ECUQ8xuIjFT2"}
And my object I'm attempting to DeSerialize to:
[Serializable]
public class Session : BaseRequest, ISession
{
public Session(string url) : base(url)
{
}
#region Members
[JsonProperty(PropertyName = "userId")]
public string UserId { get; set; }
[JsonProperty(PropertyName = "authorizations")]
public object[] Authorizations { get; set; }
[JsonProperty(PropertyName = "primaryAuthorization")]
public string PrimaryAuthorization { get; set; }
[JsonProperty(PropertyName = "thirdPartyOrgName")]
public string ThirdPartyOrgName { get; set; }
[JsonProperty(PropertyName = "username")]
public string Username { get; set; }
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "selfUri")]
public string SelfUri { get; set; }
#endregion
}
I simply make the web request and get the response stream using a stream reader and return the string. Pretty standard.
However, when I attempt to Deserialize into my Session object it always throws an error: Value Cannot be Null
var serializer = new JsonSerializer();
response = MakePostRequest(true);
var obj = serializer.Deserialize<Session>(new JsonTextReader(new StringReader(response)));
The response is the JSON string I get back from the web request and is exact to what I specified above.
I've done this before but normally I've been the one that designed the REST api. Not the case this time but I can't for the life of my figure out why this won't deserialize? I've specified the JSonProperty PropertyName to avoid issues with proper casing, is this not working right maybe? Any help is appreciated!
UDPATE
I think I found part of the problem. It is attempting to deserialize my base class which consists of :
public abstract class BaseRequest
{
protected BaseRequest(string apiUrl)
{
ApiUrl = apiUrl;
Request = (HttpWebRequest)WebRequest.Create(apiUrl);
}
public string ApiUrl { get; set; }
public string JsonPayload { get; set; }
public HttpWebRequest Request { get; private set; }
}
Is there any directive I can give to prevent it from doing so? Or will I need to refactor around this?
Below code works (using Json.Net):
var session = JsonConvert.DeserializeObject<Session>(json);
public class Session
{
public string Id { get; set; }
public string UserId { get; set; }
public List<string> Authorizations { get; set; }
public string PrimaryAuthorization { get; set; }
public string ThirdPartyOrgName { get; set; }
public string Username { get; set; }
public string SelfUri { get; set; }
}
EDIT
How should I tell it to ignore the base class?
var session = (Session)System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(typeof(Session));
JsonConvert.PopulateObject(DATA, session);
But I don't think this is a nice way of doing it. Changing your design may be a better solution.
I've tested your code and it works fine, only change I made was removing the constructor, I take it that the serializer can't create an instance on the object for some reason, can you remove
public Session(string url) : base(url)
{
}
Your code works just fine for me but I haven't the BaseRequest source code so I made class with empty constructor.
IMO the exception is coming exactly from there. In the Session constructor the url parameter is null because your JSON object doesn't have url property. May be in the BaseRequest class you use this url param and you receive the Value Can't be Null error.
You can change just the name of parameter if this is the issue:
public Session(string selfUri ) : base(selfUri)
{
}
Check also if the 'response' variable is null. StringReader can throw this exception if you pass null to its constructor.

Categories

Resources