Rewrite JSON serialization and deserialization using extension methods

Source: Internet
Author: User

JSON serialization and deserialization are rewritten using extension methods later than. NET 3.5, which improves code readability and maintainability.

First, do not use the extension method.

Definition:

  1. /// <Summary>
  2. /// JSON deserialization Tool
  3. /// </Summary>
  4. Public class JSONControl
  5. {
  6. /// <Summary>
  7. /// JSON serialization
  8. /// </Summary>
  9. /// <Typeparam name = "T"> </typeparam>
  10. /// <Param name = "_ Object"> </param>
  11. /// <Returns> </returns>
  12. Public static String JsonSerializer <T> (T _ Object)
  13. {
  14. Using (MemoryStream TempMemoryStream = new MemoryStream ())
  15. {
  16. String MySerializationString = String. Empty;
  17. DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T ));
  18. MyDataContractJsonSerializer. WriteObject (TempMemoryStream, _ Object );
  19. MySerializationString = Encoding. UTF8.GetString (TempMemoryStream. ToArray ());
  20. Return MySerializationString;
  21. }
  22. }
  23. /// <Summary>
  24. /// Deserialization
  25. /// </Summary>
  26. /// <Typeparam name = "T"> </typeparam>
  27. /// <Param name = "_ JsonString"> </param>
  28. /// <Returns> </returns>
  29. Public static T JsonDeserializer <T> (String _ JsonString)
  30. {
  31. Using (MemoryStream TempMemoryStream = new MemoryStream (Encoding. UTF8.GetBytes (_ JsonString )))
  32. {
  33. DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T ));
  34. Return (T) MyDataContractJsonSerializer. ReadObject (TempMemoryStream );
  35. }
  36. }
  37. }

 

Usage:

  1. /// <Summary>
  2. /// Scenario control object
  3. /// </Summary>
  4. Public class SceneControl
  5. {
  6. /// <Summary>
  7. /// Temporary serialized object
  8. /// </Summary>
  9. [DataContract]
  10. Public class TempClass
  11. {
  12. /// <Summary>
  13. /// Attribute 1
  14. /// </Summary>
  15. [DataMember (Name = "Property1")]
  16. Public String Property1 {get; set ;}
  17. /// <Summary>
  18. /// Attribute 2
  19. /// </Summary>
  20. [DataMember (Name = "Property2")]
  21. Public String Property2 {get; set ;}
  22. }
  23. /// <Summary>
  24. /// Test
  25. /// </Summary>
  26. Public void Test ()
  27. {
  28. String JsonString = "{'property1': '000000', 'property2': '2 '}";
  29. TempClass MyTempClass = JSONControl. JsonDeserializer <TempClass> (JsonString );
  30. }
  31. }

 

Then use the Extension Method

 

Definition:

  1. /// <Summary>
  2. /// JSON deserialization Tool
  3. /// </Summary>
  4. Public static class JSONControl
  5. {
  6. /// <Summary>
  7. /// JSON serialization
  8. /// </Summary>
  9. /// <Typeparam name = "T"> </typeparam>
  10. /// <Param name = "_ Object"> </param>
  11. /// <Returns> </returns>
  12. Public static String ObjectToJson <T> (this T _ Object)
  13. {
  14. Using (MemoryStream TempMemoryStream = new MemoryStream ())
  15. {
  16. String MySerializationString = String. Empty;
  17. DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T ));
  18. MyDataContractJsonSerializer. WriteObject (TempMemoryStream, _ Object );
  19. MySerializationString = Encoding. UTF8.GetString (TempMemoryStream. ToArray ());
  20. Return MySerializationString;
  21. }
  22. }
  23. /// <Summary>
  24. /// JSON deserialization
  25. /// </Summary>
  26. /// <Typeparam name = "T"> </typeparam>
  27. /// <Param name = "_ JsonString"> </param>
  28. /// <Returns> </returns>
  29. Public static T JsonToObject <T> (this String _ JsonString)
  30. {
  31. Using (MemoryStream TempMemoryStream = new MemoryStream (Encoding. UTF8.GetBytes (_ JsonString )))
  32. {
  33. DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T ));
  34. Return (T) MyDataContractJsonSerializer. ReadObject (TempMemoryStream );
  35. }
  36. }
  37. }

 

Usage:

  1. /// <Summary>
  2. /// Scenario control object
  3. /// </Summary>
  4. Public class SceneControl
  5. {
  6. /// <Summary>
  7. /// Temporary serialized object
  8. /// </Summary>
  9. [DataContract]
  10. Public class TempClass
  11. {
  12. /// <Summary>
  13. /// Attribute 1
  14. /// </Summary>
  15. [DataMember (Name = "Property1")]
  16. Public String Property1 {get; set ;}
  17. /// <Summary>
  18. /// Attribute 2
  19. /// </Summary>
  20. [DataMember (Name = "Property2")]
  21. Public String Property2 {get; set ;}
  22. }
  23. /// <Summary>
  24. /// Test
  25. /// </Summary>
  26. Public void Test ()
  27. {
  28. String JsonString = "{'property1': '000000', 'property2': '2 '}";
  29. TempClass MyTempClass = JsonString. JsonToObject <TempClass> ();
  30. }
  31. }

 

Original article address: http://zhangrou.net/post/2014/06/23/ re-write json and deserialization .aspx

Related Article

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.