void converting_list_numeric() { before = () => { List <dynamic> users = new List <dynamic> { 10, 20 }; objectToConvert = new { Users = users.Where(s => s % 10 == 0) }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["executes deferred statement and serializes result"] = () => { string expected = @"{ ""Users"": [ 10, 20 ] }"; jsonString.should_be(expected); }; }
void describe_gemini_to_json() { before = () => { objectToConvert = new Gemini(new { Id = 15, String = "hello", Char = 'a', DateTime = DateTime.Today, Double = (double)100, Guid = Guid.Empty, Decimal = (decimal)15, }); }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["converts gemini"] = () => { string expected = @"{{ ""id"": {0}, ""string"": ""{1}"", ""char"": ""{2}"", ""dateTime"": ""{3}"", ""double"": {4}, ""guid"": ""{5}"", ""decimal"": {6} }}" .With(15, "hello", 'a', DateTime.Today, (double)100, Guid.Empty, (decimal)15); jsonString.should_be(expected); }; }
void converting_named_classes() { before = () => { objectToConvert = new { Goal = new Goal { Cost = 100, Name = "Goal", Expense = new Expense { Amount = 500, Name = "Expense" } } }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["includes serialization of named classes"] = () => { string expected = @"{ ""Goal"": { ""Name"": ""Goal"", ""Cost"": 100, ""Expense"": { ""Name"": ""Expense"", ""Amount"": 500 } } }"; jsonString.should_be(expected); }; }
void describe_prototype_to_json() { before = () => { objectToConvert = new Prototype(); objectToConvert.Id = 15; objectToConvert.String = "hello"; objectToConvert.Char = 'a'; objectToConvert.DateTime = DateTime.Today; objectToConvert.Double = (double)100; objectToConvert.Guid = Guid.Empty; objectToConvert.Decimal = (decimal)15; objectToConvert.StringAsNull = null as string; objectToConvert.Long = (long)100; }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["converts prototype"] = () => { var expected = @"{{ ""Id"": {0}, ""String"": ""{1}"", ""Char"": ""{2}"", ""DateTime"": ""{3}"", ""Double"": {4}, ""Guid"": ""{5}"", ""Decimal"": {6}, ""StringAsNull"": {7}, ""Long"": 100 }}" .With(15, "hello", 'a', DateTime.Today, (double)100, Guid.Empty, (decimal)15, "null"); jsonString.should_be(expected); }; }
void converting_nested_object() { before = () => { objectToConvert = new Gemini( new { Id = 15, Name = "Mirror's Edge", Owner = new Gemini(new { Id = 22, Handle = "@amirrajan" }) }); }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["converts whole object graph"] = () => { string expected = @"{ ""Id"": 15, ""Name"": ""Mirror's Edge"", ""Owner"": { ""Id"": 22, ""Handle"": ""@amirrajan"" } }"; jsonString.should_be(expected); }; }
void converting_anonymous_types_that_have_defferred_execution() { before = () => { List <dynamic> users = new List <dynamic> { new { Name = "Jane" }, new { Name = "John" }, new { Name = "Jake" } }; objectToConvert = new { Users = users.Where(s => s.Name.StartsWith("Ja")) }; }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["executes deferred statement and serializes result"] = () => { string expected = @"{ ""Users"": [ { ""Name"": ""Jane"" }, { ""Name"": ""Jake"" } ] }"; jsonString.should_be(expected); }; }
void coverting_list_string() { before = () => { List <dynamic> users = new List <dynamic> { "Jane", "Doe" }; objectToConvert = new { Users = users.Where(s => s.Contains("e")) }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["executes deferred statement and serializes result"] = () => { string expected = @"{ ""Users"": [ ""Jane"", ""Doe"" ] }"; jsonString.should_be(expected); }; }
void describe_casing() { before = () => { objectToConvert = new Gemini( new { Id = 15, Name = "Mirror's Edge", Owner = new Gemini(new { Id = 22, Handle = "@amirrajan" }) }); }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert, new NoCasingChange()); }; it["serializes with altered casing"] = () => { var expected = @"{ ""Id"": 15, ""Name"": ""Mirror's Edge"", ""Owner"": { ""Id"": 22, ""Handle"": ""@amirrajan"" } }"; jsonString.should_be(expected); }; }
void describe_can_be_converted() { new Each <dynamic, bool, string>() { { new Prototype(), true, "prototype" }, { new Gemini(), true, "gemini" }, { new DynamicModel(), true, "dynamic model" }, { new { Name = "Jane Doe" }, true, "anonymous type" }, { "Jane Doe", false, "string" }, { new List <object> { new Prototype(), new Gemini(), new DynamicModel() }, true, "list containing convertable types" }, { new List <string>() { "Jane", "Doe" }, false, "list of string" }, { new List <object> { new { Name = "Jane Doe" }, new Prototype(), new Gemini() }, true, "list containing gemini's and anonymous types" }, { new List <object> { new { Name = "Jane Doe" }, "Foobar" }, false, "list containing convertable types and non convertable types" }, { new List <string>(), true, "empty list" } }.Do((entity, expectedResult, type) => { it["{0} should evaluate to: {1}".With(type, expectedResult)] = () => { DynamicToJson.CanConvertObject(entity as object).should_be(expectedResult); }; }); }
public static Response Json(object o) { var response = (Response)DynamicToJson.Convert(o); response.ContentType = "application/json"; return(response); }
void describe_collections_that_have_collections() { before = () => { dynamic person1 = new Gemini(new { Name = "Jane Doe", Friends = new List <string> { "A", "B", "C" } }); objectToConvert = new List <dynamic> { person1 }; }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["serializes the list for both"] = () => { var expected = @"[ { ""name"": ""Jane Doe"", ""friends"": [ ""A"", ""B"", ""C"" ] } ]"; jsonString.should_be(expected); }; }
void describe_dynamic_model_to_json() { before = () => { objectToConvert = new DynamicModel(new { Id = 15, String = "hello", Char = 'a', DateTime = DateTime.Today, Double = (double)100, Guid = Guid.Empty, Decimal = (decimal)15, }); }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["converts dynamic model"] = () => { string expected = @"{{ ""Id"": {0}, ""String"": ""{1}"", ""Char"": ""{2}"", ""DateTime"": ""{3}"", ""Double"": {4}, ""Guid"": ""{5}"", ""Decimal"": {6} }}" .With(15, "hello", 'a', DateTime.Today, (double)100, Guid.Empty, (decimal)15); jsonString.should_be(expected); }; }
void converting_list_of_boolean() { before = () => { List <dynamic> users = new List <dynamic> { true, false }; objectToConvert = new { IsAdded = true, Users = users.Where(s => s == true || s == false) }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["executes deferred statement and serializes result"] = () => { string expected = @"{ ""IsAdded"": true, ""Users"": [ true, false ] }"; jsonString.should_be(expected); }; }
void describe_anonymous_type_to_json() { before = () => objectToConvert = new { FirstName = "Jane", LastName = "Doe" }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["converts properties of anonymous type"] = () => jsonString.should_be(@"{{ ""FirstName"": ""{0}"", ""LastName"": ""{1}"" }}".With("Jane", "Doe")); }
void describe_db_rows_to_json() { before = () => { Seed seed = new Seed(); seed.PurgeDb(); seed.CreateTable("Rabbits", seed.Id(), new { Name = "nvarchar(255)" }).ExecuteNonQuery(); seed.CreateTable("Tasks", seed.Id(), new { Description = "nvarchar(255)" }, new { RabbitId = "int" }, new { DueDate = "datetime" }).ExecuteNonQuery(); var rabbitId = new { Name = "Yours Truly" }.InsertInto("Rabbits"); new { rabbitId, Description = "bolt onto vans", DueDate = new DateTime(2013, 1, 14) }.InsertInto("Tasks"); rabbitId = new { Name = "Hiro Protaganist" }.InsertInto("Rabbits"); new { rabbitId, Description = "save the world", DueDate = new DateTime(2013, 1, 14) }.InsertInto("Tasks"); new { rabbitId, Description = "deliver pizza", DueDate = new DateTime(2013, 1, 14) }.InsertInto("Tasks"); rabbitId = new { Name = "Lots" }.InsertInto("Rabbits"); for (int i = 0; i < 10; i++) { new { rabbitId, Description = "Task: " + i.ToString(), DueDate = new DateTime(2013, 1, 14) }.InsertInto("Tasks"); } }; it["disregards self referencing objects"] = () => { var results = tasks.All().Include("Rabbits").ToList(); (results as IEnumerable <dynamic>).ForEach(s => { s.Rabbit = s.Rabbit(); }); objectToConvert = new Gemini(new { Tasks = results }); string expected = @"{ ""tasks"": [ { ""id"": 1, ""description"": ""bolt onto vans"", ""rabbitId"": 1, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 1, ""name"": ""Yours Truly"" } }, { ""id"": 2, ""description"": ""save the world"", ""rabbitId"": 2, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 2, ""name"": ""Hiro Protaganist"" } }, { ""id"": 3, ""description"": ""deliver pizza"", ""rabbitId"": 2, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 2, ""name"": ""Hiro Protaganist"" } }, { ""id"": 4, ""description"": ""Task: 0"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 5, ""description"": ""Task: 1"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 6, ""description"": ""Task: 2"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 7, ""description"": ""Task: 3"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 8, ""description"": ""Task: 4"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 9, ""description"": ""Task: 5"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 10, ""description"": ""Task: 6"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 11, ""description"": ""Task: 7"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 12, ""description"": ""Task: 8"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } }, { ""id"": 13, ""description"": ""Task: 9"", ""rabbitId"": 3, ""dueDate"": ""1/14/2013 12:00:00 AM"", ""rabbit"": { ""id"": 3, ""name"": ""Lots"" } } ] }"; jsonString = DynamicToJson.Convert(objectToConvert); jsonString.should_be(expected); }; }
private dynamic ExecuteRequest(string href, string method, object payload) { var uriPath = GenerateUrl(href); var uri = new Uri(uriPath, UriKind.Absolute); var request = CreateRequest(uri, Authorization); request.Method = method; request.ContentType = "application/json"; if (payload == null) { request.ContentLength = 0; } else { var requestStream = request.GetRequestStream(); var sr = new StreamWriter(requestStream); if (payload is Gemini) { sr.Write(DynamicToJson.Convert(payload)); } else { sr.Write(JsonConvert.SerializeObject(payload)); } sr.Close(); } try { var response = request.GetResponse(); var stream = response.GetResponseStream(); return(Parse(stream, response.ContentType, response.Headers)); } catch (WebException ex) { ThrowNiceError(ex); throw; } }
void converting_dynamic_model() { before = () => { objectToConvert = new SomeDynamicModel(new { Id = 20, Title = "SomeTitle" }); }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["includes both properties from hash and properties defined on dynamic model"] = () => { jsonString.should_be(@"{ ""Id"": 20, ""Title"": ""SomeTitle"", ""Name"": ""SomeName"" }"); }; }
void describe_empty_collection() { before = () => { objectToConvert = new List <dynamic>(); }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["serializes empty list"] = () => { var expected = @"[ ]"; jsonString.should_be(expected); }; }
void describe_collection() { before = () => { dynamic prototype = new Prototype(); prototype.Id = 1; objectToConvert = new List <dynamic> { prototype, new DynamicModel(new { Id = 2 }), new Gemini(new { Id = 3 }), }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["converts collection"] = () => jsonString.should_be(@"[ { ""Id"": 1 }, { ""Id"": 2 }, { ""Id"": 3 } ]"); }
void describe_two_objects_referencing_the_same_list() { before = () => { dynamic person1 = new Gemini(new { Name = "Jane Doe" }); dynamic person2 = new Gemini(new { Name = "John Doe" }); dynamic person3 = new Gemini(new { Name = "Jane Smith" }); var friends = new List <dynamic> { new Gemini(new { Name = "John Smith", Friends = new List <dynamic> { person1, person2, person3 } }) }; person1.Friends = friends; person2.Friends = friends; objectToConvert = new Gemini(new { People = new List <dynamic>() { person1, person2 } }); }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["serializes the list for both"] = () => { var expected = @"{ ""people"": [ { ""name"": ""Jane Doe"", ""friends"": [ { ""name"": ""John Smith"", ""friends"": [ { ""name"": ""Jane Smith"" } ] } ] }, { ""name"": ""John Doe"", ""friends"": [ { ""name"": ""John Smith"", ""friends"": [ { ""name"": ""Jane Smith"" } ] } ] } ] }"; jsonString.should_be(expected); }; }
void escaping_strings() { before = () => { objectToConvert = new { Quotes = @"""Quoted""", Ticks = @"'Ticked'", //ticks don't need to be escaped, jquery for some reason refuses to deserialize a payload if ticks are escaped when they dont need to be BackSlashes = @"c:\Temp", NewLine = "New" + Environment.NewLine + "Line" }; }; act = () => jsonString = DynamicToJson.Convert(objectToConvert); it["special characters are escaped"] = () => { jsonString.should_be(@"{ ""Quotes"": ""\""Quoted\"""", ""Ticks"": ""'Ticked'"", ""BackSlashes"": ""c:\\Temp"", ""NewLine"": ""New\r\nLine"" }"); }; }
void describe_entities_with_error_collection() { before = () => { var list = new List <dynamic>(); list.Add(new Gemini(new { Name = "" })); list.Add(new Gemini(new { Name = "" })); list.ForEach(s => { s.Validates = new DynamicFunction(() => { var rules = new List <dynamic>(); rules.Add(new Presence("Name")); return(rules); }); }); list.ForEach(s => s.Extend <Validations>()); list.ForEach(s => s.IsValid()); list.ForEach(s => s.Errors = s.Errors()); objectToConvert = list; }; it["serializes errors for each record"] = () => { var expected = @"[ { ""name"": """", ""errors"": [ { ""key"": ""Name"", ""value"": ""Name is required."" } ] }, { ""name"": """", ""errors"": [ { ""key"": ""Name"", ""value"": ""Name is required."" } ] } ]"; jsonString = DynamicToJson.Convert(objectToConvert); jsonString.should_be(expected); }; }
void describe_key_value_pair_serialization() { before = () => { dynamic rabbit = new Rabbit(new { }); rabbit.IsValid(); objectToConvert = new { Errors = rabbit.Errors() }; }; act = () => { jsonString = DynamicToJson.Convert(objectToConvert); }; it["serializes key value pairs (structs)"] = () => { var expected = @"{ ""Errors"": [ { ""Key"": ""Name"", ""Value"": ""Name is required."" } ] }"; jsonString.should_be(expected); }; }
void friends_have_different_games_that_are_preferred_by_user() { before = () => GivenUserIsFollowingTwoPeopleWhoHaveDifferentGames(); it["contains each preferred game (ordered alphabetically)"] = () => { var gearsOfWar = PreferredGames().First(); (gearsOfWar.Name as string).should_be("Gears of War"); (OwnerId(gearsOfWar) as object).should_be(anotherUserId as object); (Owner(gearsOfWar).Handle as string).should_be("@another"); var mirrorsEdge = PreferredGames().Last(); (mirrorsEdge.Name as string).should_be("Mirror's Edge"); (mirrorsEdge.Console as string).should_be("XBOX360"); (OwnerId(mirrorsEdge) as object).should_be(followingId as object); (Owner(mirrorsEdge).Handle as string).should_be("@following"); }; it["doesn't contain the password for the user when serialized"] = () => { var mirrorsEdge = PreferredGames().First(); (DynamicToJson.Convert(Owner(mirrorsEdge) as object) as string).Contains("Password").should_be_false(); var gearsOfWar = PreferredGames().Last(); (DynamicToJson.Convert(Owner(gearsOfWar) as object) as string).Contains("Password").should_be_false(); }; }