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

ListJsonConverter Exception when reserializing / deserializing #149

Open
SpencerWaz opened this issue Apr 11, 2019 · 3 comments
Open

ListJsonConverter Exception when reserializing / deserializing #149

SpencerWaz opened this issue Apr 11, 2019 · 3 comments

Comments

@SpencerWaz
Copy link

SpencerWaz commented Apr 11, 2019

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
Copy link
Author

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!

@SpencerWaz SpencerWaz changed the title Serialize / Deserialize Exception ListJsonConverter Exception when reserializing / deserializing Apr 12, 2019
@MennoKlup
Copy link

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

@SpencerWaz
Copy link
Author

@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.

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

2 participants