피드 포맷터(JSON)
이 JsonFeeds 샘플에서는 사용자 지정 SyndicationFeedFormatter 및 DataContractJsonSerializer를 사용하여 JSON(JavaScript Object Notation) 형식으로 SyndicationFeed 클래스의 인스턴스를 serialize하는 방법을 보여 줍니다.
샘플 아키텍처
이 샘플에서는 JsonFeedFormatter
에서 상속되는 SyndicationFeedFormatter라는 클래스를 구현합니다. JsonFeedFormatter
클래스는 DataContractJsonSerializer를 사용하여 JSON 형식으로 데이터를 읽고 씁니다. 내부적으로 포맷터는 JsonSyndicationFeed
와 JsonSyndicationItem
이라는 데이터 계약 형식의 사용자 지정 집합을 사용하여 serializer에 의해 생성되는 JSON 데이터의 형식을 제어합니다. 이러한 구현 정보는 최종 사용자에게 표시되지 않으므로 표준 SyndicationFeed 및 SyndicationItem 클래스를 호출할 수 있습니다.
JSON 피드 쓰기
다음 샘플 코드와 같이 이 샘플에 구현된 JsonFeedFormatter
를 DataContractJsonSerializer와 함께 사용하여 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;
샘플을 설치, 빌드 및 실행하려면
Windows Communication Foundation 샘플의 일회 설치 절차를 수행했는지 확인합니다.
C# 또는 Visual Basic .NET 버전의 솔루션을 빌드하려면 Building the Windows Communication Foundation Samples의 지침을 따릅니다.
단일 컴퓨터 또는 다중 컴퓨터 구성에서 샘플을 실행하려면 Windows Communication Foundation 샘플 실행의 지침을 따릅니다.