Rwindon
Rwindon

Reputation: 55

System.Runtime.Serialization.SerializationException when trying to deserialize

i have the following json string i am trying to deserialize

[{\"sha\":\"29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"commit\":{\"author\":{\"name\":\"Christophe Coevoet\",\"email\":\"[email protected]\",\"date\":\"2013-03-06T08:53:13Z\"},\"committer\":{\"name\":\"Christophe Coevoet\",\"email\":\"[email protected]\",\"date\":\"2013-03-06T08:53:13Z\"},\"message\":\"Merge pull request #1071 from BrandonLWhite/master\n\nThis resolves Issue #1055\",\"tree\":{\"sha\":\"277ae283e984236883a18bf7e2c703abc17ce48a\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/git/trees/277ae283e984236883a18bf7e2c703abc17ce48a\"},\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/git/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"comment_count\":0},\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"comments_url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2/comments\",\"author\":{\"login\":\"stof\",\"id\":439401,\"avatar_url\":\"https://avatars.githubusercontent.com/u/439401?v=2\",\"gravatar_id\":\"7894bbdf1c05b18a1444ad8c76c9d583\",\"url\":\"https://api.github.com/users/stof\",\"html_url\":\"https://github.com/stof\",\"followers_url\":\"https://api.github.com/users/stof/followers\",\"following_url\":\"https://api.github.com/users/stof/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/stof/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/stof/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/stof/subscriptions\",\"organizations_url\":\"https://api.github.com/users/stof/orgs\",\"repos_url\":\"https://api.github.com/users/stof/repos\",\"events_url\":\"https://api.github.com/users/stof/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/stof/received_events\",\"type\":\"User\",\"site_admin\":false},\"committer\":{\"login\":\"stof\",\"id\":439401,\"avatar_url\":\"https://avatars.githubusercontent.com/u/439401?v=2\",\"gravatar_id\":\"7894bbdf1c05b18a1444ad8c76c9d583\",\"url\":\"https://api.github.com/users/stof\",\"html_url\":\"https://github.com/stof\",\"followers_url\":\"https://api.github.com/users/stof/followers\",\"following_url\":\"https://api.github.com/users/stof/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/stof/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/stof/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/stof/subscriptions\",\"organizations_url\":\"https://api.github.com/users/stof/orgs\",\"repos_url\":\"https://api.github.com/users/stof/repos\",\"events_url\":\"https://api.github.com/users/stof/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/stof/received_events\",\"type\":\"User\",\"site_admin\":false},\"parents\":[{\"sha\":\"70c46cfac7cb281af61d39f352d333eda9f1a84b\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/70c46cfac7cb281af61d39f352d333eda9f1a84b\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/70c46cfac7cb281af61d39f352d333eda9f1a84b\"},{\"sha\":\"b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\"}]}]

using this method of deserialization

public List<RepositoryCommitData> DeserializeGithubCommitUrlJsonResponseAndTripUneededData(string json)
    {
        var serializer = new DataContractJsonSerializer(typeof (List<RepositoryCommitData>));
        var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
        var desirializedData = (List<RepositoryCommitData>) serializer.ReadObject(stream);

        return desirializedData;
    }

into the following classes

[DataContract]
public class RepositoryCommitData
{
    [DataMember] public Committer commit;
}
[DataContract]
public class Committer
{
    [DataMember] public string name;
    [DataMember] public string date;
}

but i always receive

System.Runtime.Serialization.SerializationException : There was an error deserializing the object of type System.Collections.Generic.List`1[[GitReport.Deserialization.RepositoryCommitData, GitReport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Encountered invalid character ' '. ----> System.FormatException : Encountered invalid character ' '.

Upvotes: 4

Views: 5303

Answers (1)

Ronald Wildenberg
Ronald Wildenberg

Reputation: 32134

You have two NEWLINE characters in your input string (look for \n in the input text). If you escape (or remove) these, the original error disappears. Here's a list of control characters you should escape before deserializing.

In your original JSON string I then received another error but when you update it, all I had to do was escape the \n with \\n.

Upvotes: 3

Related Questions