I would like to try processing ServiceStack json, but I already figured out how to do something I need through Newtonsoft. Can this be done using ServiceStack?
I tried with the commented code, but it gives exceptions, see below for details of the exception.
Thanks!
Josh
[Test] public void TranslateFromGitHubToCommitMessage() { const string json = @" { 'commits': [ { 'author': { 'email': ' dev@null.org ', 'name': 'The Null Developer' }, 'message': 'okay i give in' }, { 'author': { 'email': ' author@github.com ', 'name': 'Doc U. Mentation' }, 'message': 'Updating the docs, that\ my job' }, { 'author': { 'email': ' author@github.com ', 'name': 'Doc U. Mentation' }, 'message': 'Oops, typos' } ] } "; dynamic root = JObject.Parse(json); //dynamic root = ServiceStack.Text.JsonSerializer.DeserializeFromString<JsonObject>(json); //dynamic root = ServiceStack.Text.JsonObject.Parse(json); var summaries = new List<string>(); foreach (var commit in root.commits) { var author = commit.author; var message = commit.message; summaries.Add(string.Format("{0} <{1}>: {2}", author.name, author.email, message)); } const string expected1 = "The Null Developer < dev@null.org >: okay i give in"; const string expected2 = "Doc U. Mentation < author@github.com >: Updating the docs, that my job"; const string expected3 = "Doc U. Mentation < author@github.com >: Oops, typos"; Assert.AreEqual(3, summaries.Count); Assert.AreEqual(expected1, summaries[0]); Assert.AreEqual(expected2, summaries[1]); Assert.AreEqual(expected3, summaries[2]); }
Exception Details
When using the first numbered line:
dynamic root = ServiceStack.Text.JsonSerializer.DeserializeFromString<JsonObject>(json);
This exception occurs when a method is called.
NullReferenceException:
at ServiceStack.Text.Common.DeserializeListWithElements`2.ParseGenericList(String value, Type createListType, ParseStringDelegate parseFn) at ServiceStack.Text.Common.DeserializeEnumerable`2.<>c__DisplayClass3.<GetParseFn>b__0(String value) at ServiceStack.Text.Common.DeserializeSpecializedCollections`2.<>c__DisplayClass7. <GetGenericEnumerableParseFn>b__6(String x) at ServiceStack.Text.Json.JsonReader`1.Parse(String value) at ServiceStack.Text.JsonSerializer.DeserializeFromString[T](String value) at GitHubCommitAttemptTranslator.Tests.GitHubCommitAttemptTranslatorTests.TranslateFromGitHubToCommitMessage()
And secondly:
dynamic root = ServiceStack.Text.JsonObject.Parse(json); var summaries = new List<string>(); foreach (var commit in root.commits)
'ServiceStack.Text.JsonObject' does not contain a definition for 'commits'
Note. the message "string" does not contain a definition for "commits" if I use the code from the first line, but change the type to or instead
at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at GitHubCommitAttemptTranslator.Tests.GitHubCommitAttemptTranslatorTests.TranslateFromGitHubToCommitMessage()
After using DynamicJson from .NET 4.0 ServiceStack
Referring to a comment about myths: This test case works, but if I change it, as shown below:
var dog = new { Name = "Spot", Parts = new { Part1 = "black", Part2 = "gray" }, Arr = new [] { "one", "two", "three"} }; var json = DynamicJson.Serialize(dog); var deserialized = DynamicJson.Deserialize(json);
Then the deserialized .Name and Parts are fine, but Arr has a type string.
also:
If I use quotation marks, it does not work. This is normal? Json2 works (to the extent that Arr is also still a string), but json3 doesn't work at all. It just returns
Immediate Window: deserialized = DynamicJson.Deserialize(json3); {} base {System.Dynamic.DynamicObject}: {} _hash: Count = 1 ----- code: ----- var json2 = @" { ""Name"": ""Spot"", ""Parts"": { ""Part1"": ""black"", ""Part2"": ""gray"" }, ""Arr"": [ ""one"", ""two"", ""three"" ] }"; var json3 = @" { 'Name': 'Spot', 'Parts': { 'Part1': 'black', 'Part2': 'gray' }, 'Arr': [ 'one', 'two', 'three' ] }"; var deserialized = DynamicJson.Deserialize(json1);