Json.NET How to modify property values before serializing

Source: Internet
Author: User

Today write a json.net comparison of the use of the door, perhaps you have not used for a long time, maybe you can come in handy tomorrow.

We all know the use of json.net sequence words

New " 123 ", B = DateTime.Now, C = DateTime.Now.AddDays (1)};             string json = jsonconvert.serializeobject (test);

You will find that this string of code generates something like this:

{"A": "123", "B": "2014-09-14t19:08:11.8653266+08:00", "C": "2014-09-15t19:08:11.8663266+08:00"}

There's nothing wrong with this in itself. If you encounter a more ridiculous demand as follows: I want B and C to generate a JSON string that doesn't show the time I want it to show a difference in seconds (int type) How do I handle

I've asked a lot of people before, and they've got some suggestions like modifying a generated string or adding two properties to the entity class.

That would solve the problem, but if I had 10 classes and 100 classes, and each class had a different property name than the one ... It's scary to think about it and then give up on that idea.

I was wondering if there was a way to modify the value of this property before the JSON string was generated. That wouldn't be nice .... Tut tut

Later to view the source code of Json.NET really let me find a way

The code is as follows:

classTest { Public stringAGet;Set; }  PublicDateTime B {Get;Set; }  PublicDatetime? E =Get;Set; } }    classProgram {Static voidMain (string[] args) {test test=NewTest () {A ="123", B = DateTime.Now, C = DateTime.Now.AddDays (1) }; Jsonconverter JC=NewDateConverter (); stringJSON =jsonconvert.serializeobject (test, JC);        Console.WriteLine (JSON); }    }     Public classDateconverter:jsonconverter { Public Override voidWritejson (Jsonwriter writer,Objectvalue, Jsonserializer serializer) {DateTime I=(DateTime) value; Writer. WriteValue ("I can change the value of the DateTime type in the Model ."); }         Public Override ObjectReadjson (Jsonreader Reader, Type ObjectType,ObjectExistingvalue, Jsonserializer Serializer) {            Throw Newnotimplementedexception (); }         Public Override BOOLCanconvert (Type objectType) {if(ObjectType = =typeof(DateTime))returnObjectType = =typeof(DateTime); Else if(ObjectType = =typeof(DateTime?)))                returnObjectType = =typeof(DateTime?)); Else                return false; }    }

You will find that the generated JSON string is Jiangzi ....

It's amazing.

Here is just to provide a way of thinking and implementation, how to adapt to the need you crossing your own ideas.

------------------------------------------------------make by night, flowers clear Light

Json.NET How to modify property values before serializing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.