0
votes

It's a .net core project running on a Mac. I've a custom Exception class that I'm serializing as JSON as output in case of an error.

  public class HttpException : Exception {
    private readonly int statusCode;

    public HttpException() {}

    public HttpException(int statusCode, string message) : base(message) {
        this.statusCode = statusCode;
    }

    public int StatusCode {get {return this.statusCode;}}
}

Code related to json serialization

Exception exception = (Exception)serviceRequest.ResultData;
MemoryStream memoryStream = new MemoryStream();  
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(Exception));  


// it fails here
dataContractJsonSerializer.WriteObject(memoryStream, exception);  


byte[] json = memoryStream.ToArray();  
memoryStream.Close();  
Console.WriteLine(Encoding.UTF8.GetString(json, 0, json.Length));

System.Runtime.Serialization.SerializationException: Type 'Service.Model.Exceptions.HttpException' with data contract name 'HttpException:http://schemas.datacontract.org/2004/07/Service.Model.Exceptions' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)

Edit

Trying to use System.Web.Extensions but have squiggly lines.

enter image description here

2
// it fails here with what error?Matt Burland
You didn't ask a question or state what the issue is.user47589
Matt it's not duplicate. The issue was with new DataContractJsonSerializer(typeof(HttpException));, I used Exception before. this brings another question, how can I make it generic because system can throw different kinds of exceptions, do I've to narrow down to typeof each?App2015
Use Json.Net string json = JsonConvert.SerializeObject(exception); it will workEser
@Eser I wish but I can't fall outside with 3rd party libraries, only native implementations because of some requirements.App2015

2 Answers

0
votes

I wish but I can't fall outside with 3rd party libraries, only native implementations because of some requirements.

You can include the System.Web.Extensions library and use JavaScriptSerializer

var ser = new JavaScriptSerializer();
string json = ser.Serialize(exception);

But as I said in comments, I would prefer Json.Net

0
votes

System.Web.Extensions is not supported in .Net Core, but you can use the Newtonsoft.Json nuget package. One of these two methods should do the trick:

Newtonsoft.Json.JsonConvert.DeserializeObject(string value)
Newtonsoft.Json.JsonConvert.DeserializeObject<T>(string value)

I encountered a similar issue when porting unit tests from .Net 4.6.1 to .Net Core.

On a side-note, if you are getting any strange runtime exceptions, look for instances of JavaScriptSerializer and replace them with corresponding Newtonsoft.Json methods.