To get through the. NET 3.5 to interact with EXTJS data

Source: Internet
Author: User
Tags extend json new features serialization static class

ExtJS is a very good UI framework that has benefited from an increasing number of enterprise-class applications using this framework. Then, in many projects, to the most of the Java project, the reason is the integration of the ExtJS with the more and more strong. and for use. NET platform developers, it is difficult to use ExtJS in their own projects, because data communication is hard to achieve unification. Before. NET 3.5,. NET platform provides limited JSON native support. As a result, many programmers use third party components. For example, the Litjson.net component.

In. NET 3.5, the framework provides a DataContractJsonSerializer class that allows for easy JSON serialization and deserialization of objects. Other than that. NET3.5 provides extended methods and LINQ, and it's also a powerful development for us. In this blog, I will use these new features to analyze how to integrate ExtJS with. NET communication. If there is anything wrong, please correct me and welcome the exchange.

First, the use of DataContractJsonSerializer class

This class is used for JSON serialization and deserialization of objects. The class is in the System.Runtime.Serialization.Json namespace. is a new class for the. NET3.5 platform. Use the WriteObject () and the ReadObject () method to manipulate the objects primarily. Related APIs, refer to MSDN.

Second, the extension method

Extension methods, a new feature provided by. NET 3.5, is used to enhance the functionality support of native objects and to extend the functionality of objects. For an introduction, please refer to MSDN or related books.

Three, the generic type

Generics for many. NET programmers are not unfamiliar. After. NET 2.0, support for generics has been added to the framework. Generics enable us to reduce the loss of resources in the process of packing and unpacking.

Next, we can write the following code to extend the JSON serialization functionality of the class. I used the extension method and used the generic constraint to constrain the type to be a class.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.IO;
Using System.Text;
Using System.Runtime.Serialization.Json;

<summary>
Summary description for Jsonextends
</summary>
public static Class Jsonextends
{
public static string tojson<t> (this T obj) where t:class
{
DataContractJsonSerializer ser = new DataContractJsonSerializer (obj. GetType ());
string output = string. Empty;
using (MemoryStream ms = new MemoryStream ())
{
Ser. WriteObject (MS, obj);
StringBuilder sb = new StringBuilder ();
Sb. Append (Encoding.UTF8.GetString) (Ms. ToArray ()));
Output = sb. ToString ();
}
return output;
}

public static T fromjson<t> (This string jsonstring) where T:class
{
T ouput = null;
Try
{
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T));
using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring))
{
Ouput = (T) ser. ReadObject (MS);
}
}
catch (Exception) {}
return ouput;
}
}

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.