Created
November 2, 2011 20:55
-
-
Save einarwh/1334883 to your computer and use it in GitHub Desktop.
Extracts JSON data into .NET object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static PixItData ToPixItData(string json) | |
{ | |
JObject o = JObject.Parse(json); | |
int size = o.SelectToken("pixelSize").Value<int>(); | |
int wide = o.SelectToken("pixelsWide").Value<int>(); | |
int high = o.SelectToken("pixelsHigh").Value<int>(); | |
JToken bg = o.SelectToken("background"); | |
Color? bgColor = null; | |
if (bg != null) | |
{ | |
string bgStr = bg.Value<string>(); | |
bgColor = ColorTranslator.FromHtml(bgStr); | |
} | |
JToken payload = o.SelectToken("payload"); | |
var dict = new Dictionary<Color, IEnumerable<Pixel>>(); | |
foreach (var t in payload) | |
{ | |
var list = new List<Pixel>(); | |
foreach (var xyArray in t.SelectToken("pixels")) | |
{ | |
int x = xyArray[0].Value<int>(); | |
int y = xyArray[1].Value<int>(); | |
list.Add(new Pixel(x, y)); | |
} | |
string cs = t.SelectToken("color").Value<string>(); | |
Color clr = ColorTranslator.FromHtml(cs); | |
dict[clr] = list; | |
} | |
return new PixItData(wide, high, size, dict); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment