다음을 통해 공유


약한형 JSON Serialization 샘플

사용자 정의 형식을 지정된 통신 형식으로 직렬화하거나 통신 형식을 사용자 정의 형식으로 다시 역직렬화할 경우 서비스와 클라이언트 모두에서 지정된 사용자 정의 형식을 사용할 수 있어야 합니다. 보통 이렇게 하기 위해 이 사용자 정의 형식에 DataContractAttribute 특성을 적용하고 해당 멤버에 DataMemberAttribute 특성을 적용합니다. 이 메커니즘은 How to: Serialize and Deserialize JSON Data(방법: JSON 데이터 직렬화 및 역직렬화) 항목에 설명된 대로 JSON(JavaScript Object Notation) 개체로 작업하는 경우에도 적용됩니다.

일부 시나리오에서는 WCF(Windows Communication Foundation) 서비스 또는 클라이언트가 개발자의 제어를 벗어난 외부의 서비스 또는 클라이언트로 생성된 JSON 개체에 액세스해야 합니다. 더 많은 웹 서비스에서 공개적으로 JSON API를 노출하면 WCF 개발자가 임의의 JSON 개체를 역직렬화하는 로컬 사용자 정의 형식을 구성하는 것에 실익이 없어질 수도 있습니다.

WeaklyTypedJson 샘플에서는 WCF 개발자가 사용자 정의 형식을 만들지 않고 역직렬화된 임의의 JSON 개체로 작업할 수 있도록 해 주는 메커니즘을 제공합니다. 컴파일할 때에는 JSON 개체가 역직렬화되는 형식을 알 수 없기 때문에 JSON 개체의 약한 형식의 serialization 이라고 합니다.

예를 들어, 공개 웹 서비스 API는 서비스 사용자에 대한 일부 정보를 설명하는 다음 JSON 개체를 반환합니다.

{"personal": {"name": "Paul", "age": 23, "height": 1.7, "isSingle": true, "luckyNumbers": [5,17,21]}, "favoriteBands": ["Band ABC", "Band XYZ"]}

이 개체를 역직렬화하려면 WCF 클라이언트에서 다음 사용자 정의 형식을 구현해야 합니다.

[DataContract]
public class MemberProfile
 {
     [DataMember]
     public PersonalInfo personal;

     [DataMember]
     public string[] favoriteBands;
 }

 [DataContract]
public class PersonalInfo
 {
     [DataMember]
     public string name;

     [DataMember]
     public int age;

     [DataMember]
     public double height;

     [DataMember]
     public bool isSingle;

     [DataMember]
     public int[] luckyNumbers;
 }

이 과정이 부담이 될 수 있으며, 클라이언트에서 두 개 이상의 JSON 개체를 처리해야 하는 경우 특히 그렇습니다.

이 샘플에서 제공하는 JsonObject 형식은 역직렬화된 JSON 개체의 약한 형식의 표현을 소개합니다. JsonObject는 JSON 개체와 .NET Framework 사전 간의 자연스러운 매핑 및 JSON 배열과 .NET Framework 배열 간의 매핑을 사용합니다. 다음 코드에서는 JsonObject 형식을 보여 줍니다.

// Instantiation of JsonObject json omitted

string name = json["root"]["personal"]["name"];
int age = json["root"]["personal"]["age"];
double height = json["root"]["personal"]["height"];
bool isSingle = json["root"]["personal"]["isSingle"];
int[] luckyNumbers = {
                                     json["root"]["personal"]["luckyNumbers"][0],
                                     json["root"]["personal"]["luckyNumbers"][1],
                                     json["root"]["personal"]["luckyNumbers"][2]
                                 };
string[] favoriteBands = {
                                        json["root"]["favoriteBands"][0],
                                        json["root"]["favoriteBands"][1]
                                    };

컴파일할 때 해당 형식을 선언하지 않고 JSON 개체와 배열을 '찾아볼' 수 있습니다. 최상위 ["root"] 개체의 요구 사항에 대한 설명은 Mapping Between JSON and XML항목에 설명된 대로 JSON(JavaScript Object Notation) 개체로 작업하는 경우에도 적용됩니다.

참고 항목

JsonObject 클래스는 예로서만 제공됩니다. 이 클래스는 아직 테스트를 제대로 거치지 않았으며 프로덕션 환경에서는 사용하지 말아야 합니다. 약한 형식의 JSON serialization에서 확실히 암시되는 의미는 JsonObject로 작업할 때 형식 안전성이 부족하다는 것입니다.

JsonObject 형식을 사용하려면 클라이언트 작업 계약에서 반환 형식으로 Message 를 사용해야 합니다.

[ServiceContract]
    interface IClientSideProfileService
    {
        // There is no need to write a DataContract for the complex type returned by the service.
        // The client will use a JsonObject to browse the JSON in the received message.

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        Message GetMemberProfile();
    }

그러면 다음 코드에 표시된 것과 같이 JsonObject 가 인스턴스화됩니다.

// Code to instantiate IClientSideProfileService channel omitted…

// Make a request to the service and obtain the Json response
XmlDictionaryReader reader = channel.GetMemberProfile().GetReaderAtBodyContents();

// Go through the Json as though it is a dictionary. There is no need to map it to a .NET CLR type.
JsonObject json = new JsonObject(reader);

JsonObject 생성자는 XmlDictionaryReader메서드를 통해 얻을 수 있는 GetReaderAtBodyContents 를 받습니다. 판독기에는 클라이언트에서 받는 JSON 메시지의 XML 표현이 포함되어 있습니다. 자세한 내용은 JSON과 XML 간의 매핑 항목을 참조하세요.

프로그램에서는 다음이 출력됩니다.

Service listening at http://localhost:8000/.
To view the JSON output from the sample, navigate to http://localhost:8000/GetMemberProfile
This is Paul's page. I am 23 years old and I am 1.7 meters tall.
I am single.
My lucky numbers are 5, 17, and 21.
My favorite bands are Band ABC and Band XYZ.

샘플을 설치, 빌드 및 실행하려면

  1. Windows Communication Foundation 샘플의 일회 설치 절차를 수행했는지 확인합니다.

  2. Building the Windows Communication Foundation Samples에 설명된 대로 WeaklyTypedJson.sln 솔루션을 빌드합니다.

  3. 솔루션을 실행합니다.