adaptivewebworks / prismicio-netstandard-kit

1 stars 2 forks source link

Newtonsoft serialization exception in Image Fragment ... #23

Open haukurhaf opened 6 months ago

haukurhaf commented 6 months ago

Probably related to the recent changes Prismic made to it's content editor. The Parse method in line 158 in Fragment.cs throws an exception on some occasions, when it tries to cast json["views"] to a JObject, when it is infact JArray in some cases.

Here's the updated method with a fix which seems to prevent the issue:

            public static Image Parse(JToken json)
            {
                View main = View.Parse((JObject)json["main"]);
                var views = new Dictionary<string, View>();

                if (json["views"] as JObject != null) //Check if it is JObject
                {
                    foreach (KeyValuePair<string, JToken> v in ((JObject)json["views"]))
                    {
                        views[v.Key] = View.Parse((JObject)v.Value);
                    }
                }
                else if (json["views"] as JArray != null)
                {
                    // Maybe this needs to be handled differently
                }

                return new Image(main, views);
            }