简体   繁体   中英

How to handle dbnull value with standard JSON-serializer in c#?

I just created an REST-ful webservice with a WebserviceHost. I have several methods that wil return a JSON-object to the user, if he makes a GET request.

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "appliances/{username}")]
List<object[]> GetAvailableAppliances(string username);

This method returns values from a database. In the object-array are a few dbnull values but the standard JSON Serializer can't handle it. If the user does a request, the connection is resetted, but only if there are dbnull values in the list.

How can I handle these dbnull values? Do I have to change the values before the list will be serialized or is there a different solution?

Personally I would never let a DBNull (which is, ultimately, an implementation detail) out past the data-access code; so yes, I would sanitize those at origin. null is usually an ideal substitute for DBNull outside of data-access code (although personally I'm of the opinion that DBNull shouldn't even exist ). Treating everything as object isn't making it any clearer - if possible, some well-typed objects would also go a long way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM