TLDR:如果反序列化对象的所有字段都保持 null,没有任何异常,请检查基类是否具有 [DataContract] 属性,这使得 Json.net 忽略没有 [DataMember] 的每个属性。
很抱歉代码量很大,但我无法缩小导致问题的原因。
我有一个带有类 AssetViewModel 数组的 json 对象
[{
"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":[]
}]
资产视图模型:
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; }
}
如果我用反序列化它,见下文,它工作正常。
var result = JsonConvert.DeserializeObject<IEnumerable<AssetViewModel>>(content);
但是对于类 AssetViewModel2,它不会:
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;
set
{
if (_id == value) return;
_id = value;
NotifyOfPropertyChange();
}
}
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
NotifyOfPropertyChange();
}
}
public UserViewModel Creator
{
get => _creator;
set
{
if (_creator == value) return;
_creator = value;
NotifyOfPropertyChange();
}
}
public string Extension
{
get => _extension;
set
{
if (_extension == value) return;
_extension = value;
NotifyOfPropertyChange();
}
}
public DateTime CreationDate
{
get => _creationDate;
set
{
if (_creationDate == value) return;
_creationDate = value;
NotifyOfPropertyChange();
}
}
public DateTime ModificationDate
{
get => _modificationDate;
set
{
if (_modificationDate == value) return;
_modificationDate = value;
NotifyOfPropertyChange();
}
}
public long Size
{
get => _size;
set
{
if (_size == value) return;
_size = value;
NotifyOfPropertyChange();
}
}
public bool IsDeleted
{
get => _isDeleted;
set
{
if (_isDeleted == value) return;
_isDeleted = value;
NotifyOfPropertyChange();
}
}
public int Version
{
get => _version;
set
{
if (_version == value) return;
_version = value;
NotifyOfPropertyChange();
}
}
public bool HasSinglePreview
{
get => _hasSinglePreview;
set
{
if (_hasSinglePreview == value) return;
_hasSinglePreview = value;
NotifyOfPropertyChange();
}
}
public bool HasCollagePreview
{
get => _hasCollagePreview;
set
{
if (_hasCollagePreview == value) return;
_hasCollagePreview = value;
NotifyOfPropertyChange();
}
}
public GroupViewModel Group
{
get => _group;
set
{
if (_group == value) return;
_group = value;
NotifyOfPropertyChange();
}
}
public CollectionViewModel Collection
{
get => _collection;
set
{
if (_collection == value) return;
_collection = value;
NotifyOfPropertyChange();
}
}
public ProjectViewModel Project
{
get => _project;
set
{
if (_project == value) return;
_project = value;
NotifyOfPropertyChange();
}
}
public BindableCollection<TagViewModel> Tags
{
get => _tags;
set
{
if (_tags == value) return;
_tags = value;
NotifyOfPropertyChange();
}
}
public int CustomPreviews
{
get => _customPreviews;
set
{
if (_customPreviews == value) return;
_customPreviews = value;
NotifyOfPropertyChange();
}
}
}
属性是相同的,只是在此类中,它们具有用于 WPF 绑定的 NotifyOfPropertyChange(( 的支持字段。我将其中一些更改为具体类型,因为我认为这可能会造成一些麻烦,但它没有帮助。
如果使用 AssetViewModel2 反序列化,则结果的所有属性都保持 null,并且我没有任何异常。
我在 AssetViewModel2 中做错了什么?
问题出在基类中。我正在使用Caliburn micros PropertyChangedBase。该类具有 [DataContract] 属性,这使得有必要将 [DataMember] 属性添加到属性中,这些属性应被反序列化。
解决 方案:
- 复制不带 [DataContract] 属性的类,并将 [JsonIgnore] 添加到 IsNotifying 或
- 将 [DataMember] 属性添加到应反序列化的每个属性。