2

I am trying to deserialize an object in a Web Api from an object posted by angular. I am receiving an error: Error reading object reference '1'. Path 'Developers[0].DeveloperId', line 20, position 21 My Json object is (which has been validated as valid JSON):

{
  "Id": 0,
  "Name": "Name",
  "OwnerId": 1,
  "Description": "Description",
  "Note": "My Notes",
  "Stakeholders": [
    {
      "$id": "1",
      "StakeholderId": 1,
      "Name": "Mary",
      "DateModified": "2018-02-21T12:28:15.023",
      "DateCreated": "2018-02-21T12:28:15.023",
      "$$hashKey": "object:3"
    }
  ],
  "Developers": [
    {
      "$id": "1",
      "DeveloperId": 1,
      "DeveloperName": "Joseph",
      "DateModified": "2018-02-21T12:28:26.07",
      "DateCreated": "2018-02-21T12:28:26.07",
      "$$hashKey": "object:4"
    }
  ]
}

I am trying to deserialize with:

var app = JsonConvert.DeserializeObject<Application>(request.ToString(), new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

The developer class (which is similar to Stakeholder class)

public class Developer : IModificationHistory
{
    public int DeveloperId { get; set; }
    [Required]
    public string DeveloperName { get; set; }
    [JsonIgnore]
    public virtual List<Application> Applications { get; set; }
    public DateTime DateModified { get; set; }
    public DateTime DateCreated { get; set; }
}

The application class is simply:

public class Application
{

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    public string Note { get; set; }

    public virtual List<Stakeholder> Stakeholders { get; set; }
    public int OwnerId { get; set; }
    public virtual List<Developer> Developers { get; set; }

}

The javascript I use to call this post is:

    var data =
    {
        Id: vm.appId,
        Name: vm.applicationName,
        OwnerId: vm.owner.DeveloperId,
        Description: vm.applicationDescription,
        Note: vm.note,
        Stakeholders: vm.selectedStakeholders,
        Developers: vm.selectedDevelopers

};


    $http.post("/api/Application/Post/", JSON.stringify(data))

The Stakeholders List gets filled properly, but the Developers list does not. If I put developers in the list before stakeholders, then developers list gets filled properly and stakeholders does not. Any suggestions would be greatly appreciated!

Joseph
  • 93
  • 1
  • 11

1 Answers1

0

The problem is with the same value of $id, both are set to 1, see inner exception:

{"A different value already has the Id '1'."}

I just changed its value to 2 and it is working fine:

{
  "Id": 0,
  "Name": "Name",
  "OwnerId": 1,
  "Description": "Description",
  "Note": "My Notes",
  "Stakeholders": [
    {
      "$id": "1",
      "StakeholderId": 1,
      "Name": "Mary",
      "DateModified": "2018-02-21T12:28:15.023",
      "DateCreated": "2018-02-21T12:28:15.023",
      "$$hashKey": "object:3"
    }
  ],
  "Developers": [
    {
      "$id": "2",
      "DeveloperId": 1,
      "DeveloperName": "Joseph",
      "DateModified": "2018-02-21T12:28:26.07",
      "DateCreated": "2018-02-21T12:28:26.07",
      "$$hashKey": "object:4"
    }
  ]
}

Here is the screenshot of my output:

enter image description here

FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
  • Ok thank you. Angular is adding $id and $hashKey so perhaps I should ask a new question as to why angular is adding this? – Joseph Feb 21 '18 at 19:02
  • 1
    This still doesn't make any since, they are 2 completly different lists, why does he give this error? – Haytam Feb 21 '18 at 19:02
  • @Haytam I am also not sure why, but it has to do something why dollar sign `$` prefix. If I remove and deserialize it works fine. – FaizanHussainRabbani Feb 21 '18 at 19:20
  • @Haytam upon further reading, `$` indicates metadata, not an actual data field. – FaizanHussainRabbani Feb 21 '18 at 19:26
  • 1
    Web api config was messed up, json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; was in there for some reason. Your post helped me to get to the bottom. Thank you! – Joseph Feb 21 '18 at 19:31
  • Still you can read about it on: https://stackoverflow.com/questions/4638585/how-do-c-sharp-classes-deal-with-dollar-signs-in-json – FaizanHussainRabbani Feb 21 '18 at 19:31
  • 1
    hey @Joseph, how did you solve that problem? I have a similar case with React, but setting `options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None` doesn't help... What did you finally do to solve that? – Dawid Sibiński Feb 03 '22 at 04:35
  • 1
    I've just found a solution here: https://stackoverflow.com/questions/4638585/how-do-c-sharp-classes-deal-with-dollar-signs-in-json , applied this one: `settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; ` – Dawid Sibiński Feb 03 '22 at 06:43