다음을 통해 공유


피드 포맷터(JSON)

JsonFeeds 샘플에서는 사용자 지정 SyndicationFeedFormatterDataContractJsonSerializer를 사용하여 JSON(JavaScript Object Notation) 형식으로 SyndicationFeed 클래스의 인스턴스를 serialize하는 방법을 보여 줍니다.

샘플 아키텍처

이 샘플에서는 JsonFeedFormatter에서 상속되는 SyndicationFeedFormatter라는 클래스를 구현합니다. JsonFeedFormatter 클래스는 DataContractJsonSerializer를 사용하여 JSON 형식으로 데이터를 읽고 씁니다. 내부적으로 포맷터는 JsonSyndicationFeedJsonSyndicationItem이라는 데이터 계약 형식의 사용자 지정 집합을 사용하여 serializer에 의해 생성되는 JSON 데이터의 형식을 제어합니다. 이러한 구현 정보는 최종 사용자에게 표시되지 않으므로 표준 SyndicationFeedSyndicationItem 클래스를 호출할 수 있습니다.

JSON 피드 쓰기

다음 샘플 코드와 같이 이 샘플에 구현된 JsonFeedFormatterDataContractJsonSerializer와 함께 사용하여 JSON 피드를 쓸 수 있습니다.

//Basic feed with sample data
SyndicationFeed feed = new SyndicationFeed("Custom JSON feed", "A Syndication extensibility sample", null);
feed.LastUpdatedTime = DateTime.Now;
feed.Items = from s in new string[] { "hello", "world" }
select new SyndicationItem()
{
    Summary = SyndicationContent.CreatePlaintextContent(s)
};

//Write the feed out to a MemoryStream in JSON format
DataContractJsonSerializer writeSerializer = new DataContractJsonSerializer(typeof(JsonFeedFormatter));
writeSerializer.WriteObject(stream, new JsonFeedFormatter(feed));

JSON 필드 읽기

다음 코드와 같이SyndicationFeed를 사용하여 JSON 형식의 데이터 스트림에서 JsonFeedFormatter를 가져올 수 있습니다.

//Read in the feed using the DataContractJsonSerializer

DataContractJsonSerializer readSerializer = new DataContractJsonSerializer(typeof(JsonFeedFormatter));

JsonFeedFormatter formatter = readSerializer.ReadObject(stream) as JsonFeedFormatter;

SyndicationFeed feedRead = formatter.Feed;

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

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

  2. C# 또는 Visual Basic .NET 버전의 솔루션을 빌드하려면 Building the Windows Communication Foundation Samples의 지침을 따릅니다.

  3. 단일 컴퓨터 또는 다중 컴퓨터 구성에서 샘플을 실행하려면 Windows Communication Foundation 샘플 실행의 지침을 따릅니다.