nyk0r

Serialization - C#

Sep 11th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Web.Script.Serialization; // Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
  6.  
  7. namespace SimpleTests
  8. {
  9.     class Program
  10.     {
  11.         public class SerializeInfo
  12.         {
  13.             public string Label { get; private set; }
  14.             public string[][] Data { get; private set; }
  15.  
  16.             public SerializeInfo(string lable, Dictionary<DateTime, string> data)
  17.             {
  18.                 Label = lable;
  19.                 Data = data.Select(p => new[]
  20.                                             {
  21.                                                 p.Key.ToString("d",CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat),
  22.                                                 p.Value
  23.                                             }).OrderBy(d => d[0]).ToArray();
  24.             }
  25.         }
  26.  
  27.         static void Main(string[] args)
  28.         {
  29.             var js = new JavaScriptSerializer();
  30.             var timeline = new Dictionary<DateTime, string>
  31.                                {
  32.                                    {new DateTime(2000, 1, 1), "3.2"},
  33.                                    {new DateTime(2002, 2, 2), "5.2"},
  34.                                    {new DateTime(2003, 2, 2), "8.1"}
  35.                                };
  36.  
  37.             Console.WriteLine(js.Serialize(new SerializeInfo("Europe (EU27)", timeline)));
  38.             // {"Label":"Europe (EU27)","Data":[["1/1/2000","3.2"],["2/2/2002","5.2"],["2/2/2003","8.1"]]}
  39.             Console.ReadLine();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment