Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

TLDR: If all the fields of the deserialized object stay null, without any exceptions, check if your base class has the [DataContract] attribute, which makes Json.net ignore every property without [DataMember].

Sorry for the amount of code but I couldn't narrow down what causes the problem.

I have this json object with an array of the class AssetViewModel

"id":117, "name":"BMP", "creator":null, "extension":".bmp", "creationDate":"2019-03-31T23:18:20.8080488Z", "modificationDate":"2019-03-31T23:18:21.3191844Z", "size":1440056, "isDeleted":false, "version":0, "hasSinglePreview":true, "hasCollagePreview":false, "customPreviews":0, "group":null, "collection":null, "project":null, "tags":[]

AssetViewModel:

public class AssetViewModel : IAsset
    public int Id { get; set; }
    public string Name { get; set; }
    public IUser Creator { get; set; }
    public string Extension { get; set; }
    public DateTime CreationDate { get; set; }
    public DateTime ModificationDate { get; set; }
    public long Size { get; set; }
    public bool IsDeleted { get; set; }
    public int Version { get; set; }
    public bool HasSinglePreview { get; set; }
    public bool HasCollagePreview { get; set; }
    public int CustomPreviews { get; set; }
    public IGroup Group { get; set; }
    public ICollection Collection { get; set; }
    public IProject Project { get; set; }
    public ICollection<ITag> Tags { get; set; }

If I deserialize it with, see below, it works fine.

var result = JsonConvert.DeserializeObject<IEnumerable<AssetViewModel>>(content);

But with class AssetViewModel2 it doesn't:

var result = JsonConvert.DeserializeObject<IEnumerable<AssetViewModel2>>(content);
public class AssetViewModel2 : PropertyChangedBase
    private CollectionViewModel _collection;
    private DateTime _creationDate;
    private UserViewModel _creator;
    private int _customPreviews;
    private string _extension;
    private GroupViewModel _group;
    private bool _hasCollagePreview;
    private bool _hasSinglePreview;
    private int _id;
    private bool _isDeleted;
    private DateTime _modificationDate;
    private string _name;
    private ProjectViewModel _project;
    private long _size;
    private BindableCollection<TagViewModel> _tags = new BindableCollection<TagViewModel>();
    private int _version;
    public int Id
        get => _id;
            if (_id == value) return;
            _id = value;
            NotifyOfPropertyChange();
    public string Name
        get => _name;
            if (_name == value) return;
            _name = value;
            NotifyOfPropertyChange();
    public UserViewModel Creator
        get => _creator;
            if (_creator == value) return;
            _creator = value;
            NotifyOfPropertyChange();
    public string Extension
        get => _extension;
            if (_extension == value) return;
            _extension = value;
            NotifyOfPropertyChange();
    public DateTime CreationDate
        get => _creationDate;
            if (_creationDate == value) return;
            _creationDate = value;
            NotifyOfPropertyChange();
    public DateTime ModificationDate
        get => _modificationDate;
            if (_modificationDate == value) return;
            _modificationDate = value;
            NotifyOfPropertyChange();
    public long Size
        get => _size;
            if (_size == value) return;
            _size = value;
            NotifyOfPropertyChange();
    public bool IsDeleted
        get => _isDeleted;
            if (_isDeleted == value) return;
            _isDeleted = value;
            NotifyOfPropertyChange();
    public int Version
        get => _version;
            if (_version == value) return;
            _version = value;
            NotifyOfPropertyChange();
    public bool HasSinglePreview
        get => _hasSinglePreview;
            if (_hasSinglePreview == value) return;
            _hasSinglePreview = value;
            NotifyOfPropertyChange();
    public bool HasCollagePreview
        get => _hasCollagePreview;
            if (_hasCollagePreview == value) return;
            _hasCollagePreview = value;
            NotifyOfPropertyChange();
    public GroupViewModel Group
        get => _group;
            if (_group == value) return;
            _group = value;
            NotifyOfPropertyChange();
    public CollectionViewModel Collection
        get => _collection;
            if (_collection == value) return;
            _collection = value;
            NotifyOfPropertyChange();
    public ProjectViewModel Project
        get => _project;
            if (_project == value) return;
            _project = value;
            NotifyOfPropertyChange();
    public BindableCollection<TagViewModel> Tags
        get => _tags;
            if (_tags == value) return;
            _tags = value;
            NotifyOfPropertyChange();
    public int CustomPreviews
        get => _customPreviews;
            if (_customPreviews == value) return;
            _customPreviews = value;
            NotifyOfPropertyChange();

The properties are the same, except in this class they have backing fields with NotifyOfPropertyChange() for WPF binding. I changed some of them to concrete types because I thought this might cause some trouble, but it didn't help.

All the properties for result stay null if deserialized with AssetViewModel2 and I don't get any exceptions.

What am I doing wrong in AssetViewModel2?

If you change AssetViewModel2 to only have properties with simple types in it (DateTime, string, int etc) does it then populate this properties correctly? – mjwills Apr 1, 2019 at 0:30 And yes even with only simple types it doesn't insert the values. But i guess the main problem is that the breakpoints in the properties setters aren't hit. – Unavi Apr 1, 2019 at 0:34 dotnetfiddle.net/dYhtex works fine for me - suggesting that there is either something weird in your base class, or the extra properties (that I deleted) aren't being handled property. Add the properties I deleted, one by one, until you find the culprit. – mjwills Apr 1, 2019 at 2:47 @mjwills the something weird in your base class, pointed me into the right direction. Thanks for the help! – Unavi Apr 1, 2019 at 12:11

The problem was in the base class. I am using caliburn micros PropertyChangedBase. The class has a [DataContract] attribute, which makes it necessary to add the [DataMember] attribute to properties, which should be de/serialized.

Solutions:

  • Copy the class without the [DataContract] attribute and add [JsonIgnore] to IsNotifying or
  • add the [DataMember] attribute to every property, that should be de/serialized.
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.