Transforming attributes into a JSON format

Click here to view my JSFiddle example

function testing() {
  foo = canvas.getObjects();
  bar = JSON.stringify(canvas.getObjects());
  console.log(foo);
  console.log(bar);
}

After examining my JSFiddle link above, it appears that JSON.stringify() is altering the "x1", "y1", "x2", and "y2" properties, and I'm currently unsure as to why this is happening.

Answer №1

According to the information provided by (Mozilla) in the JSON.stringify documentation (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior)

Explanation of toJSON() method

When an object being converted to a string has a toJSON property that is a function, the toJSON() method dictates how the object is stringified. Instead of serializing the entire object, the value that the toJSON() method returns upon execution will be serialized. For instance:

If you execute the following code, you should receive similar outcomes

function testing() {
    foo = canvas.getObjects()[0];
    bar = JSON.stringify(canvas.getObjects()[0]);
    console.log(foo.toJSON());
    console.log(bar);
}

Alternatively, by setting the toJSON property to undefined (although this might cause issues with fabric.js unless performed on a clone of the object - I have directly applied it to the object below)

function testing() {
    foo = canvas.getObjects();
    bar = JSON.stringify(canvas.getObjects().map(function (o) {
        o.toJSON = undefined;
        return o
    }));
    console.log(foo);
    console.log(bar);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The text is not displaying as expected due to a timeout issue

Trying to create a pop-up that functions as follows: After 3 seconds, the close button should appear, but during those 3 seconds, there will be a countdown. However, I'm encountering an issue where no text is being displayed. var n = 3; function p ...

What steps should be taken to properly assess an AngularJS provider setup?

My provider definition looks like this: (function(angular) { angular.module('myModule', []) .provider('myService', function () { var service = {}; service.configureSomething = function () { }; service.$get = function () { ...

Ways to retrieve the returned value from the JS FETCH API outside of its scope

As a beginner in Typescript React and the Ionic framework, I am trying to use the JS FETCH API to fetch data from a third-party source. However, I am struggling to access this fetched data outside of the fetch function. If anyone could provide some guidan ...

Is it possible to incorporate an external javascript file in my CSS file?

Currently, I am attempting to create a setup where my background adjusts based on the width of the user's browser. However, I am constrained by a background specified in the external stylesheet under a specific element. While I have the ability to alt ...

Retrieving PHP data with jQuery

Isn't it interesting that I couldn't find anything on Google, but I believe you can assist me. I have a Table containing different accounts. Upon clicking on a specific row, I want another table related to that account to slide in. This secondary ...

Exploring the Past: How the History API, Ajax Pages, and

I have a layout for my website that looks like this IMAGE I am experimenting with creating page transitions using ajax and the history API. CODE: history.pushState(null, null, "/members/" + dataLink + ".php" ); // update URL console. ...

Eliminating a node in RABL

I am a beginner in Rails and currently working on developing an API for my application. Utilizing rabl to generate JSON responses, I am facing an issue with sending a timestamp alongside a collection of items in my rabl template. The code snippet is as fo ...

Incorporate the value of a JQuery variable within an HTML form

When the Submit button is clicked, I am attempting to pass the value of currentDate to a REST GET service. However, there are two things that I don't understand. Is it possible to include the URL of the REST service in the action attribute of the fo ...

A guide on setting up fixed row numbers in MUI-X DataGrid

One challenge I am facing is rendering the row numbers in a table so that they remain static even when columns are sorted or filtered. I attempted to use the getRowIndexRelativeToVisibleRows method of the grid API, but unfortunately, it does not work as ex ...

HTML5 validation fails to activate

Addressing the issue head-on Check out the form I have created: <form action="<?php echo base_url()?>" method="post" id="formfield"> <center> <label>How much? <small>( Minimum of 100 )</small></label&g ...

Query: Employ regex to pick out property values

Here is the json Object I am working with: { "foo": { "name": "Name 1", "color": "green", "something_else": { "name" : "Name 2" } }, "bar": { "name": "Something else", "color": "red" ...

What is the best way to retrieve a PDF file from another website and showcase it on our own site without any redirection?

Is there a way to display a pdf from another website without having to redirect to that site? I'm looking for a solution that involves using the URL directly. ...

Extract the label from Chip component within the onClick() event in material-ui

-- Using Material-UI with React and Redux -- Within my material-ui table, there are <TableRow> elements each containing multiple <TableCell> components with <Chip> elements. These <Chip> components display text via their label prop ...

Setting up a Progressive Web App installation feature with a built-in delay from a

I have integrated a v-dialog component that shows up when my webapp loads and is utilized for installing the PWA: <template> <div> <v-dialog v-model="popupAndroid" max-width="80%" ...

JavaScript - Identifying Repetitive Items in an Array

My goal is difficult to explain in words, but I can show you with code. Here is the array I am working with: var array = [{name:"John",lastname:"Doe"},{name:"Alex",lastname:"Bill"},{name:"John",lastname:"Doe"}] This array has duplicate elements, and I n ...

swapping the final word in a string with Node.js or JavaScript

var str = "Demo Docs Version 1.0.1"; var gotWord = str.split(" ").splice(-1)[0] str = str.replace(gotWord, "testing"); console.log(str); If there is a space between words, I can replace the last word. But how do I replace the last word when ...

Error handling middleware delivering a response promptly

Issue with my express application: Even after reaching the error middleware, the second middleware function is still being executed after res.json. As per the documentation: The response object (res) methods mentioned below can send a response to the cl ...

What is the best way to define properties for objects within views.py so that the updated object can be effectively passed to JavaScript code?

When loading an "endless scroll" feed via AJAX and pagination, I realized that before passing objects to the JS code, I need to add a property (or attribute) to every object indicating whether it was liked by the current user or not. However, my initial ...

Unveiling Elements as You Scroll Within a Specified Area

I have implemented a jQuery and CSS code to reveal my contact form as I scroll down the page. However, I am facing an issue with setting a specific range for displaying the element while scrolling. Currently, I have only managed to handle the scrolling dow ...

What methods can you use to locate the CSS selector within HTML that meets certain criteria?

Is it possible to parse a given link and find CSS selectors with attributes that partially or completely match a specific keyword? For example, if the keyword is "print," I want to identify all CSS selectors in the link containing "print" in their name, id ...