Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Graham Carr 5 posts 75 karma points
    Jan 25, 2017 @ 15:22
    Graham Carr
    0

    RJPMultiUrlPicker deserializing issue

    We are using Umbraco v7.5.7, Archetype v1.13.0, Newtonsoft v6.0.0 and RJPMultiUrlPicker v1.3.1 and are experiencing the following issue.

    We have a class as follows:

    public class LinkItem : ArchetypeFieldsetModel
    {
        public int? Id { get; set; }
        public string Name { get; set; }
        public string Target { get; set; }
        public LinkType Type { get; set; }
        public string Url { get; set; }
    }
    

    and have created a custom TypeConverter that has the following method:

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var result = JsonConvert.DeserializeObject<List<LinkItem>>(value.ToString());
            return result;
        }
    

    when this runs we are getting the following error message:

    [ArgumentException: Could not cast or convert from System.String to System.Guid.]
       Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) +225
       Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) +123
       Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) +348
    
    [JsonSerializationException: Error converting value "1098" to type 'System.Guid'. Path '[0].id', line 3, position 17.]
    

    which is confusing as the object it is trying to be converted to does not have a Guid declared. The source JSON data looks as follows:

    [\r\n  {\r\n    \"id\": \"1098\",\r\n    \"name\": \"Contact us\",\r\n    \"url\": \"/contact-us/\",\r\n    \"icon\": \"icon-article\"\r\n  }\r\n]
    

    This was working in an earlier version of Umbraco (v7.1.9) and Archetype (v1.7.5) with the same version of RJPMultiUrlPicker and Newtonsoft but now no longer works as above.

    Any ideas???

  • Rasmus Eeg 91 posts 457 karma points c-trib
    Jan 26, 2017 @ 06:19
    Rasmus Eeg
    0

    Hi Graham,

    Have you tried changing the id:"1111" to id:1111?

    As the value of id is expected to be of type integer, yet the json id is of type string.

  • Graham Carr 5 posts 75 karma points
    Jan 26, 2017 @ 08:01
    Graham Carr
    0

    Hi Rasmus,

    Thanks for the reply. I can confirm that I tried both ways such as changing the class to:

    public class LinkItem : ArchetypeFieldsetModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Target { get; set; }
        public LinkType Type { get; set; }
        public string Url { get; set; }
    }
    

    as well as removing the quotes around the id. As mentioned this all worked fine in an earlier version of Umbraco/Archetype (Newtonsoft and RJPMultiUrlPicker being the same versions as previous) so can't understand what the difference is. I have also checked the returned JSON in the previous version and it is identical!!

  • Rasmus Eeg 91 posts 457 karma points c-trib
    Jan 27, 2017 @ 07:30
    Rasmus Eeg
    0

    Hi Graham,

    I belive I found the fault.

    You are inheriting ArchetypeFieldsetModel which has a property of type guid, goes by name id, and has attribute [JsonProperty]

      [JsonProperty("id")]
      public Guid Id {get;set;}
    

    https://github.com/kgiszewski/Archetype/blob/master/app/Umbraco/Umbraco.Archetype/Models/ArchetypeFieldsetModel.cs#L24

    So when you try to deserialize the object, it uses the one which is assigned with JsonProperty over your LinkItem id property.

    Try adding attribute [JsonProperty("id")] to your LinkItem Id property.

    Though I dont think that it will work, but try anyways.

Please Sign in or register to post replies

Write your reply to:

Draft