intercom / intercom-dotnet

Intercom API client library for .NET
https://developers.intercom.io/reference
Apache License 2.0
63 stars 54 forks source link

ListJsonConverter Exception when reserializing / deserializing #149

Open SpencerWaz opened 5 years ago

SpencerWaz commented 5 years ago

I'm getting an exception thrown when serializing and then de serializing Users.

Intercom: Error while serializing AppCount endpoint json result. Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '[0].social_profiles', line 1, position 1297

Repo:

List<User> users = new List<User>();
## Add users returned from UserClient.Scroll(token) loop

var serialized = JsonConvert.SerializeObject(users);
var deserialized = JsonConvert.DeserializeObject<List<User>>(serialized)

The stack trace shows the exception being thrown from: Intercom.Converters.AttributeConverters.ListJsonConverter.ReadJson

SpencerWaz commented 5 years ago

Considering it more, your converters are used to serialize to/from your API response.

To solve my issue I just inherited from your models and hid the base members

    public class IntercomUser : User
    {
        new public List<IntercomSocialProfile> social_profiles { get; set; }
        new public List<IntercomCompany> companies { get; set; }
        new public List<IntercomSegment> segments { get; set; }
        new public List<IntercomTag> tags { get; set; }
    }
    public class IntercomCompany : Company
    {
        new public List<IntercomTag> tags { get; set; }
    }
    public class IntercomSocialProfile : SocialProfile { }
    public class IntercomSegment : Segment { }
    public class IntercomTag : Tag { }

Then using automapper to map between

Mapper.Initialize(config =>
{
    config.CreateMap<User, IntercomUser>();
    config.CreateMap<Company, IntercomCompany>();
    config.CreateMap<Tag, IntercomTag>();
    config.CreateMap<SocialProfile, IntercomSocialProfile>();
    config.CreateMap<Segment, IntercomSegment>();
});

var users = new List<User>();
# get users

var mapped = Mapper.Map<List<IntercomUser>>(users);

var converted = JsonConvert.SerializeObject(mapped);
var deserialized = JsonConvert.DeserializeObject<List<IntercomUser>>(converted);

Kindof a workaround.. but it works well imho. This allowed me to re serialize / deserialize the models.

You should consider removing the JsonConverter attributes and just use them during the serialization operation instead. I'll see if I can make some time and add a PR.

Thanks!

MennoKlup commented 4 years ago

@SpencerWaz I'm running into the same issue that serialisation is generating an issue. Do you perhaps have an PR available?

SpencerWaz commented 4 years ago

@MennoKlup Sorry for the delayed response.. this isn't my main account. I just moved on after using the workaround mentioned earlier.. but I suppose I can look into adding a PR later tonight when I'm off the clock.