그 중 C# JSON 라이브러리에 JsonExSerializer 라는 놈이 있는데 썩 잘만든 것 같다.
현재 2.0 버젼이 릴리즈 된 상태고 코드가 꽤 깔끔하다.
튜터리얼 과 사용법 을 보면
Serialization/Deserialization 이 모두 가능하고 사용법이 편리하다.
튜터리얼 내용
less..
Introduction
This tutorial walks you through serializing a simple class. First we'll start with a sample Customer class. The source is shown here:
Sample Class
public class Customer
{
private int _id;
private string _firstName;
private string _lastName;
private string _phoneNumber;
private List<Order> _orders;
public int Id
{
get { return this._id; }
set { this._id = value; }
}
public string FirstName
{
get { return this._firstName; }
set { this._firstName = value; }
}
public string LastName
{
get { return this._lastName; }
set { this._lastName = value; }
}
public string PhoneNumber
{
get { return this._phoneNumber; }
set { this._phoneNumber = value; }
}
}
Next we'll populate the class with values and serialize it. To do that we'll need to get an instance of a serializer for the Customer class, and call the Serialize method.
// customer
Customer customer = new Customer();
customer.Id = 1;
customer.FirstName = "Bob";
customer.LastName = "Smith";
customer.PhoneNumber = "(222)444-9987";
// serialize to a string
Serializer serializer = new Serializer(typeof(Customer));
string jsonText = serializer.Serialize(customer);
Console.WriteLine(jsonText);
The output should look something like this:
/*
Created by JsonExSerializer
Assembly: Samples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Type: Sample01.Customer
*/
{
"Id":1,
"FirstName":"Bob",
"LastName":"Smith",
"PhoneNumber":"(222)444-9987"
}
To deserialize the customer object is just as easy. We'll use the same serializer and the string that we wrote the previous result to to deserialize the class.
Customer deserializedCustomer = (Customer) serializer.Deserialize(jsonText);
Next we'll add an orders collection to the customer object. Collections are serialized as _javascript arrays. The Order class looks like this:
public class Order
{
private decimal _amount;
private string _orderNumber;
private int _itemCount;
public decimal Amount
{
get { return this._amount; }
set { this._amount = value; }
}
public string OrderNumber
{
get { return this._orderNumber; }
set { this._orderNumber = value; }
}
public int ItemCount
{
get { return this._itemCount; }
set { this._itemCount = value; }
}
}
And we'll declare it in the customer object like this:
public class Customer {
/* Other properties omitted ... */
private List<Order> _orders;
public List<Order> Orders
{
get { return this._orders; }
set { this._orders = value; }
}
}
Let's populate the Orders collection and add it to the Customer
// orders
Order order1 = new Order();
order1.Amount = new decimal(54.99);
order1.OrderNumber = "ORD123";
order1.ItemCount = 3;
Order order2 = new Order();
order2.Amount = new decimal(99.99);
order2.OrderNumber = "ORD235";
order2.ItemCount = 10;
List<Order> orders = new List<Order>();
orders.Add(order1);
orders.Add(order2);
customer.Orders = orders;
Now the output should look like this:
/*
Created by JsonExSerializer
Assembly: Samples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Type: Sample01.Customer
*/
{
"Id":1,
"FirstName":"Bob",
"LastName":"Smith",
"PhoneNumber":"(222)444-9987",
"Orders":
[
{
"Amount":54.99,
"OrderNumber":"ORD123",
"ItemCount":3
},
{
"Amount":99.99,
"OrderNumber":"ORD235",
"ItemCount":10
}
]
}
There's the basics of using the Serializer. In later installments we'll look at more advanced scenarios.
less..
사용법 예
less..
Usage:
To serialize and deserialize objects, first you must get an instance of a serializer.
Serializer serializer = Serializer.GetSerializer(typeof(MyClass));
Then to serialize use the Serialize method, passing in your object to serialize. You can either serialize to a string, or write the serialization to a TextWriter if you're serializing to a file or socket perhaps.
Serialize to String:
MyClass myClass = new MyClass();
... // set myClass properties
string result = serializer.Serialize(myClass);
Serialize to TextWriter:
MyClass myClass = new MyClass();
... // set myClass properties
TextWriter writer = new TextWriter(new FileStream("somefile.jsx", FileMode.Create));
serializer.Serialize(myClass, writer);
writer.Close();
Then to Deserialize, just reverse the process calling the Deserialize method.
Deserialize from String:
string result = ... Populate the string with previously serialized content
MyClass myClass = (MyClass) serializer.Deserialize(result);
Deserialize from TextReader:
TextReader reader = new TextReader(new FileStream("somefile.jsx", FileMode.Open));
MyClass myClass = (Myclass) serializer.Deserialize(reader);
reader.Close();
less..
'JavaScript' 카테고리의 다른 글
javascript 성능향상을 위한 10가지 팁 (0) | 2012.09.03 |
---|---|
jQuery 예제 (0) | 2012.09.03 |
Json for C# (0) | 2012.06.29 |
ASP.NET 프로젝트에 CKEditor, CKFinder 연동 방법 (0) | 2011.07.13 |
PHP 또는 자바스크립트로 달력 만들기 소스 코드 (0) | 2009.04.28 |