Using C # / Json to Serialize a Structure in a Web Service

I am writing very simple C # HttpHandler (ashx) for use as a web service and intend to use Json.Net to serialize a small (4-5 fields) structure on the server border.

What are my options for being able to serialize the structure on the web service side and deserialize it to the correct type at the other end (which happens to be a separate C # web application) without having to more or less copy and paste the structure definition at each end? The structure is now a nested type within the web service and on the consumer web page. I could extract it into my own class assembly and add a link at both ends, but that doesn't seem much easier than saving the definition from both ends. Do I have any other reasonable options?

+4
source share
1 answer

If you have only a few types to carry, and they are unlikely to change much, just define them at both ends.

If you have lots, share dll

No magic really ... json's design is completely free. In fact, even anonymous types serialize very well in most cases.

Personally, I would use class not a struct , though ... as always, prefer class if you don't have a good one why you use struct : in most cases they are used incorrectly.

+2
source

Source: https://habr.com/ru/post/1345226/


All Articles