2

I have this JSON Object (FabricJS) that I want to de-serialize with JSON.NET on a POST request:

"{\"objects\":[{\"type\":\"OpenLayout\",\"originX\":\"left\",\"originY\":\"top\",\"left\":300,\"top\":203,\"width\":200,\"height\":100,\"fill\":\"#f05a30\",\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"backgroundColor\":\"\",\"rx\":0,\"ry\":0,\"x\":0,\"y\":0,\"label\":\"btn1\"},{\"type\":\"OpenLayout\",\"originX\":\"left\",\"originY\":\"top\",\"left\":13,\"top\":335,\"width\":200,\"height\":100,\"fill\":\"#f05a30\",\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"backgroundColor\":\"\",\"rx\":0,\"ry\":0,\"x\":0,\"y\":0,\"label\":\"\"}],\"background\":\"\"}"

Here is the class representing the structure:

public partial class ControlPageResponse
{

    [JsonProperty("objects")]
    public CanvasBtns[] Btns { get; set; }

    [JsonProperty("background")]
    public string Background { get; set; }
}

public class CanvasBtns
{

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("originX")]
    public string OriginX { get; set; }

    [JsonProperty("originY")]
    public string OriginY { get; set; }

    [JsonProperty("left")]
    public int Left { get; set; }

    [JsonProperty("top")]
    public int Top { get; set; }

    [JsonProperty("width")]
    public int Width { get; set; }

    [JsonProperty("height")]
    public int Height { get; set; }

    [JsonProperty("fill")]
    public string Fill { get; set; }

    [JsonProperty("stroke")]
    public object Stroke { get; set; }

    [JsonProperty("strokeWidth")]
    public int StrokeWidth { get; set; }

    [JsonProperty("strokeDashArray")]
    public object StrokeDashArray { get; set; }

    [JsonProperty("strokeLineCap")]
    public string StrokeLineCap { get; set; }

    [JsonProperty("strokeLineJoin")]
    public string StrokeLineJoin { get; set; }

    [JsonProperty("strokeMiterLimit")]
    public int StrokeMiterLimit { get; set; }

    [JsonProperty("scaleX")]
    public int ScaleX { get; set; }

    [JsonProperty("scaleY")]
    public int ScaleY { get; set; }

    [JsonProperty("angle")]
    public int Angle { get; set; }

    [JsonProperty("flipX")]
    public bool FlipX { get; set; }

    [JsonProperty("flipY")]
    public bool FlipY { get; set; }

    [JsonProperty("opacity")]
    public int Opacity { get; set; }

    [JsonProperty("shadow")]
    public object Shadow { get; set; }

    [JsonProperty("visible")]
    public bool Visible { get; set; }

    [JsonProperty("clipTo")]
    public object ClipTo { get; set; }

    [JsonProperty("backgroundColor")]
    public string BackgroundColor { get; set; }

    [JsonProperty("rx")]
    public int Rx { get; set; }

    [JsonProperty("ry")]
    public int Ry { get; set; }

    [JsonProperty("x")]
    public int X { get; set; }

    [JsonProperty("y")]
    public int Y { get; set; }

    [JsonProperty("label")]
    public string Label { get; set; }
}

}

As I try to de-serialize, I get "A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll [...] Error converting value [...] 'ControlPageDesigner.General.ControlPageResponse'. Path '". I can't seem to find the problem.

ControlPageResponse controlPageRecord = JsonConvert.DeserializeObject<ControlPageResponse>(ControlPage);
3
  • That's very odd that you're getting an error. I threw together a new Console application and cut-and-pasted your code and JSON into it. It runs just fine and deserializes the JSON into the objects correctly. I grabbed the most recent version of JSON.NET from NuGet. Perhaps you're using an older version that doesn't handle your data correctly? I'm assuming that the variable that you're trying to deserialize (ControlPage) is a string variable containing the JSON you included in your post. Commented Apr 18, 2014 at 17:25
  • I was using the built-in JSON.NET contained in MVC4. It seems that my application is running JSON.NET 5.0.8. I'll update and see if it works! Commented Apr 18, 2014 at 17:43
  • Why using Type type in your class ?? why not its full name ? This may be the issue Commented Aug 31, 2020 at 14:40

2 Answers 2

0

I'm not sure about my following guess, because I haven't used fabricjs with ASP.NET But if you serialize a fabric.canvas-object with javascript, it does not the same like other javascript objects, because in the framework the toJSON-Method is overwritten. If toJSON is called, the fabirc.canvas object only returns an object representing his content. After you serialize this returned object, you'll get the json representation of the canvas content. Not the canvas object itself. And for deserialization you have to use a special method (loadFromJSON).

I think the behaviour using ASP.NET will be the same.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a very similar issue to this one, and found out that I needed to declare an empty constructor in my JSON object. Otherwise the deserializer would attempt to use my other constructor and give the above error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.