I have the following setup for deserializing some json:
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
throw new MyParseException($"Parse error: {args.ErrorContext.Error.Message}");
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
In one case, json has a bunch of null values (like "title" : null, etc) which causes a NullReferenceException in one of my converters. But throwing MyParseException in the error handler causes
System.InvalidOperationException: Current error context error is different to requested error.
I think I could do this instead:
try
{
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
}
catch (Exception ex)
{
throw new MyParseException($"Parse error: {ex.Message}");
}
But is there a better way? (Maybe something more similar to my original solution that doesn't cause the error context issue?)
Based on the Newtonsoft example here. I've ended up doing the following:
List<MyParseException> errors = new List<MyParseException>();
T parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
errors.Add(new MyParseException(String.Format("Parse error: {0}", args.ErrorContext.Error.Message), args.ErrorContext.Error));
args.ErrorContext.Handled = true;
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
if (errors.Count == 1)
{
MyParseException firstException = errors[0];
firstException.Data["json"] = json;
throw firstException;
}
else if (errors.Count > 1)
{
AggregateException ex = new AggregateException("Unable to parse json. See innner exceptions and exception.Data[\"json\"] for details", errors);
ex.Data["json"] = json;
throw ex;
}
Related
Whenever I try to get the list of items in NewtownSoft json with c# I get this exception "System.NullReferenceException" I don't know how to resolve this ,I tried my best but still getting this exception.
Here is the code and output.
public object Retrive(String FileName, Type DataType)
{
JObject obj = new JObject();
obj = null;
try
{
if (!File.Exists(CreateConfigutionFilePath(FileName)))
{
_ = File.Create(CreateConfigutionFilePath(FileName));
return obj;
}
if (new FileInfo(CreateConfigutionFilePath(FileName)).Length != 0)
{
JsonSerializer jsonSerializer = new JsonSerializer();
using (StreamReader textReader = new StreamReader(CreateConfigutionFilePath(FileName)))
{
JsonReader jsonReader = new JsonTextReader(textReader);
obj = jsonSerializer.Deserialize(jsonReader) as JObject;
}
}
}
catch (Exception e)
{
// TODO TO Add A PRompt
Console.WriteLine(e);
}
object DataObject = obj.ToObject(DataType);
return DataObject;
}
Output:
Exception thrown: 'System.NullReferenceException' in IMS.exe
System.NullReferenceException: Object reference not set to an instance of an object.
at IMS.Helpers.SettingHelper.Retrive(String FileName, Type DataType) in D:\ME\Visual Studio Projects\IMS\IMS\Helpers\SettingHelper.cs:line 168
at IMS.Pages.SettingsPages.InstituteSettings.RetriveConfig() in D:\ME\Visual Studio Projects\IMS\IMS\Page
In the following function, I want to test the case where an exception is thrown using XUnit. The test should verify that the excpetion is correctly thrown.
public IDictionary<string, Label> Build(string content)
{
try
{
var settings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
};
var contentStudioResponse = JsonConvert.DeserializeObject<ContentStudioResponse<CmsLabel>>(content, settings);
if (contentStudioResponse?.Items == null)
{
_logger.Warning("No records found in content studio response for label:({#content})", content);
return new Dictionary<string, Label>();
}
return contentStudioResponse.Items.ToDictionary(x => x.Key,
x => new Label
{
Value = x.DynamicProperties.MicroContentValue
}
);
}
catch (Exception e)
{
_logger.Error(e, "Failed to deserialize or build contentstudio response for label");
return new Dictionary<string, Label>();
}
}
Below is my solution which is not working:
[Fact]
public void Builder_ThrowsException()
{
string json_responsive_labels = "abcd";
var builder = new LabelBuilder(_testLogger).Build(json_responsive_labels);
Assert.Throws<Exception>(() => builder);
//var sut = new LabelBuilder(_testLogger);
//Should.Throw<Exception>(() => sut.Build(json_responsive_labels));
}
Have a read through of this. This explains step by step on how to test for an exception being thrown.
However, based on what you have written, the code won't throw an exception since at this point you're only logging your exception and then returning a Dictionary.
catch (Exception e)
{
_logger.Error(e, "Failed to deserialize or build contentstudio response for label");
return new Dictionary<string, Label>();
}
What you actually want to do is explicitly throw an exception like so:
catch (Exception e)
{
throw new Exception();
}
In doing so, your code will throw an exception which you can catch and test against.
I have a requirement to create a controller action that can accept any data in XML, JSON or form-urlencoded. However, I couldn't make it work.
If my action has a Dictionary<string, object> parameter, it works for JSON and XML data, but not for form-urlencoded. If my action has a FormDataCollection parameter, it works for form-urlencoded but not for JSON and XML.
[HttpPost]
public HttpResponseMessage Default(FormDataCollection data /*Dictionary<string,object> data*/)
{
try
{
if (data == null)
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = "The request body is empty" });
//var collection = GetCollection(data); //used when data=Dictionary<string,object>
var collection = data.ReadAsNameValueCollection();
var agent = new ScriptingAgentClient();
var parameters = ServiceAgentParameters.From(collection);
var result = agent.Run(parameters);
if (result.Error)
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = result.ErrorMessage, Exception = result.Exception });
if (result.Data != null && result.Data.Count == 1) //result.Data is byte[]
{
//TODO: use the right Serializer
var resultString = Encoding.UTF8.GetString(result.Data[0]);
var serializer = new JavaScriptSerializer();
var dict = serializer.Deserialize<Dictionary<string, string>>(resultString);
return Request.CreateResponse(HttpStatusCode.OK, dict);
}
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = "Unknown error" });
}
catch (Exception ex)
{
Logger.Error("Error handling request", ex);
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = ex.Unwrap().Message });
}
}
ScriptingAgentClient will handle the data, whatever it may be.
If this isn't possible, how can I create two actions with same route where one will handle XML/JSON and another will handle form-urlencoded?
Note that I'm using .net40 and I can't change that. Noteworthy to mention that this webapi is a self-hosted api that will run in a windows service.
I was able to make it work reading the Request instead of letting WebApi figuring it out:
[HttpPost]
public HttpResponseMessage Default()
{
try
{
NameValueCollection collection = Request.Content.IsFormData() ?
Request.Content.ReadAsFormDataAsync().Result :
GetCollection(Request.Content.ReadAsAsync<IDictionary<string, object>>().Result);
var parameters = ServiceAgentParameters.From(collection);
var agent = new ScriptingAgentClient();
var response = agent.Run(parameters);
if (response.Error)
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = response.ErrorMessage, Exception = response.Exception });
if (response.Data != null && response.Data.Count == 1) //result.Data is List<byte[]>
{
//TODO: use the right Serializer
var resultString = Encoding.UTF8.GetString(result.Data[0]);
var serializer = new JavaScriptSerializer();
var dict = serializer.Deserialize<Dictionary<string, string>>(resultString);
return Request.CreateResponse(HttpStatusCode.OK, dict);
}
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = "Unknown error" });
}
catch (Exception ex)
{
Logger.Error("Error handling request", ex);
return Request.CreateResponse(HttpStatusCode.BadRequest, new { ErrorMessage = ex.Unwrap().Message });
}
}
I have some code that looks like this:
public Tuple<bool, SomeObjectModel> CheckIfJsonIsValid(string IncomingJson)
{
SomeObjectModel TheObjectModel = new SomeObjectModel();
JavascriptSerializer TheSerializer = new JavascriptSerializer();
.....
try
{
TheObjectModel = TheSerializer.Deserialize<SomeObjectModel>(IncomingJson);
}
catch
{
return new Tuple<bool, SomeObjectModel>(false, null); //question here
}
.....
return new Tuple<bool, SomeObjectModel>(true, TheObjectModel);
}
The calling method first check the returning tuple's Item1, and if it's false, ends its process.
Is it better practice a) to return a null value in the Tuple or b) to return a new and fresh instance of SomeObjectModel? Are there any performance implications?
Thanks for your suggestions.
Let me suggest three alternative solutions:
ParseJsonIfValid: If deserializing works, TheObjectModel is always non-null. Thus, there is no need for the boolean:
public SomeObjectModel ParseJsonIfValid(string IncomingJson)
{
JavascriptSerializer TheSerializer = new JavascriptSerializer();
.....
try
{
return TheSerializer.Deserialize<SomeObjectModel>(IncomingJson);
}
catch
{
return null;
}
}
In the calling function simply check whether the return value is null or not.
ParseJson: If the JSON is usually valid, and invalid JSON is a sign of something gone terribly wrong, just throw an exception:
public SomeObjectModel ParseJson(string IncomingJson)
{
JavascriptSerializer TheSerializer = new JavascriptSerializer();
.....
try
{
return TheSerializer.Deserialize<SomeObjectModel>(IncomingJson);
}
catch (Exception e)
{
throw new TheServerSentRubbishException(e);
}
}
Be sure to include the inner ("real") exception, so that the calling function can log the real cause of the error for debugging purposes.
TryParseJson: If null can be a valid deserialization, you can use the following pattern, which has the advantage of being consistent with the TryParse methods of the .NET framework:
public bool TryParseJson(string IncomingJson, out SomeObjectModel theObjectModel)
{
JavascriptSerializer TheSerializer = new JavascriptSerializer();
.....
try
{
theObjectModel = TheSerializer.Deserialize<SomeObjectModel>(IncomingJson);
return true;
}
catch (Exception e)
{
return false;
}
}
I am developing a web api using WCF Web Api preview 5. At the moment I have a resource class fully functional, how ever I noticed my methods inside this resouce are getting complex.
For example:
[WebInvoke(UriTemplate = "{EhrID}/PhysicalTest",Method="POST")]
public HttpResponseMessage<DTO.PhysicalTest> PostPhysicalTest(int EhrID, DTO.PhysicalTest PhysicalTestDTO)
{
var EHR = repository.FindById(EhrID);
var PhysicalTest = Mapper.Map<DTO.PhysicalTest, PhysicalTest>(PhysicalTestDTO);
if (PhysicalTest == null)
{
var response = CreateResponseForException("No object to store", HttpStatusCode.BadRequest);
throw new HttpResponseException(response);
}
try
{
if (EHR.PhysicalTests == null)
{
EHR.PhysicalTests = new List<PhysicalTest>();
}
PhysicalTest.CreationDate = DateTime.Now;
EHR.PhysicalTests.Add(PhysicalTest);
_unitOfWork.Commit();
return new HttpResponseMessage<DTO.PhysicalTest>(PhysicalTestDTO, HttpStatusCode.Created);
}
catch (Exception ex) {
var response = CreateResponseForException("Cannot create Physical Test", HttpStatusCode.InternalServerError);
throw new HttpResponseException(response);
}
}
As you may notice this method has the task of posting a new Physical Test, but it's actually validating my model too (I'm missing lots of validations still, property validations), which should not be this class concern. If there any approachable way to reduce the complexity of the methods inside de resource?
I would split it up into smaller more focused methods. I might also start using instance variables instead of passing all these arguments around, but for the sake of this post I've rewritten it without pushing stuff to instance variables.
[WebInvoke(UriTemplate = "{EhrID}/PhysicalTest",Method="POST")]
public HttpResponseMessage<DTO.PhysicalTest> PostPhysicalTest(int EhrID, DTO.PhysicalTest PhysicalTestDTO)
{
var EHR = repository.FindById(EhrID);
var PhysicalTest = Mapper.Map<DTO.PhysicalTest, PhysicalTest>(PhysicalTestDTO);
if (PhysicalTest == null)
{
var response = CreateResponseForException("No object to store", HttpStatusCode.BadRequest);
throw new HttpResponseException(response);
}
PostPhysicalTest(EHR, PhysicalTest);
return new HttpResponseMessage<DTO.PhysicalTest>(PhysicalTestDTO, HttpStatusCode.Created);
}
private void PostPhysicalTest(EHR ehr, PhysicalTest physicalTest)
{
try
{
CreatePhysicalTest(ehr, physicalTest);
}
catch (Exception ex) {
var response = CreateResponseForException("Cannot create Physical Test", HttpStatusCode.InternalServerError);
throw new HttpResponseException(response);
}
}
private void CreatePhysicalTest(EHR ehr, PhysicalTest physicalTest)
{
if (ehr.PhysicalTests == null)
{
ehr.PhysicalTests = new List<PhysicalTest>();
}
physicalTest.CreationDate = DateTime.Now;
ehr.PhysicalTests.Add(physicalTest);
_unitOfWork.Commit();
}