Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please Emphasize Requirement of "Id" Property Naming for Deserialization #129

Open
GuyPaddock opened this issue Jul 19, 2021 · 0 comments
Open

Comments

@GuyPaddock
Copy link

GuyPaddock commented Jul 19, 2021

I know the current "read me" file states that any object with an "Id" field is considered a resource object, but other docs also mention that fields can be aliased with JsonObject annotations. As a result, it's easy for someone to assume (incorrectly) that if you have a property aliased to id, that property will be used by the JsonConverter, when in fact the converter does not use aliases for searching.

For example, this will not work:

using System;
using Newtonsoft.Json;

namespace MyModels
{
    public class MyModel
    {
        [JsonProperty("id")]
        public string MyModelId { get; set; }
    }
}

You will know that you have an improperly-named Id field if you are getting this error:

"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MyModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'jsonapi', line 1, position 11.'"

It would be great if this was noted in the Read Me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant