[plugin] JSON (import/export/generate/edit/inspect/...)

Post your work in progress addons and get feedback

Post » Fri Mar 28, 2014 12:14 am

my choise - using square brackets.or maybe rounded
ImageImage
B
81
S
27
G
7
Posts: 191
Reputation: 10,545

Post » Fri Apr 04, 2014 10:23 pm

@Yann

while i can work around this, i would be nice to check the length of an object / not array.length

examplecode:
var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}
var count = Object.keys(myObject).length
console.log(count);

about the brackets, to me it looks like rounded would fit better, it doesn't refer to an object or array
ImageImage
B
70
S
21
G
7
Posts: 827
Reputation: 10,052

Post » Sat Apr 05, 2014 2:24 pm

@vtrix
Thing is, even in javascript you have no property/function to get the number of properties in an object. An object has no length per se.
The way you can answer this question is to usually count them manually
Code: Select all
var count = 0;
for (var p in myObject) {
    if(myObject.hasOwnProperty(p) {
         count++;
    }
}

I could implement that, but I'm not sure if it's a good idea to blend the boundaries between array and object. If the user can get the property count (which shouldn't really be called length) they might be tempted to believe they could loop through properties using indices... well still thinking about it.

Hmm, yeah I might add that as well as "isEmpty" and "Clear" function for array and object. We'll see. Maybe renaming "Length" to "ElementCount" or something more general to cover array and objects with the same function...
Hmm according to json.org, objects contain "members" whereas arrays contain "elements"... not sure how far I should stick to the vocabulary.
Hmm... "Size" might be better then.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sat Apr 05, 2014 4:34 pm

I was about to ask same question as @vtrix.

And one more question, is there a way to rename objects or properties?
If I have for example something like this:
Code: Select all
"Star1": {
    "name": "somename",
    "size": "somesize"
      }
"Star2": {
    "name": "somename2",
    "size": "somesize2"
     }


I would like to change "Star2" (or any property) name to something else. I'v been using a workaround simply creating new object ie. "Star5" and setting it's all properties to "Star2" and deleteting "Star2". but it gets more complicated if I have more properties, objects and arrays belonging to Key I want to rename.
ImageImageImageImage
B
157
S
66
G
42
Posts: 2,603
Reputation: 35,343

Post » Sat Apr 05, 2014 5:59 pm

@shinkan
You can't do that kind of thing in any language I know of.

JSON objects (a.k.a. dictionnary or hashtables) are values indexed using a key.
The only way to change the key, for a given value, is, as you did, copying to another key and deleting the previous one.

In javascript it wouldn't be a big deal because you would probably just swap objects references (the new key will point to the same object, hence no copy). But since, in construct2, you don't have access to any value type other than strings and number, it's not possible.

To be clear, you would have that in javascript:
Code: Select all
// this creates an empty object
// and put its reference in the variable root
var root = {};

// this creates an object with "name" and "size"
// and  put its reference in the "Star1" property of root
root["Star1"] = {"name":"somename","size":"somesize"};

// "Star1" and "Star2" properties point to the same exact object
root["Star2"] = root["Star1"];

// for example:
root["Star1"]["name"] = "plop";
root["Star2"]["name"] == "plop"; // this will return true ("==" means "is equal?")

// then to finish things up, in javascript you would do:
delete root["Star1"];


Now, the closest thing you can do with the current JSON plugin is:
Code: Select all
-> JSON: Set Value at root["Star1"] to "{""name"":""somename"",""size"":""somesize""}"
-> JSON: Load JSON JSON.ToJson(0,"Star1") at root["Star2"] // the copy
-> JSON: delete root["Star1"]

as you notice it's almost the same
The only difference is that after the copy (and before the delete) if you modify the object in "Star1", the one in "Star2" will be left untouched because it's not a reference but a copy.

Modifying the key this way is totally fine, but you have to be carefull because copy is slower than just passing references. And going from an object to a JSON string to a new object is even slower than if you just copied each properties one by one. But it's a lot simpler (:
Last edited by Yann on Sun Apr 06, 2014 2:10 am, edited 1 time in total.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sat Apr 05, 2014 6:15 pm

about the key adjust , maybe thats where json.stringify is used or parsing, not sure, would having a template object on first run be any good or will you still have the naming issue

----

oh great isEmpty and clear would indeed be usefull! , and yes size or objectsize, would make a better distinction
i also tested/used json.family and this works as expected, picking the json by uid and then working with it..

parsing the data of an object to an array went pretty smooth, just needed some vars to count the objects and properties, then push x for objects and y for keys and values, i also did the keys, to make sure i get the values where i want, still working on the array to json,

something i want to try later is querying by ID's, (within the json structure), still wondering how its best/could be done, by looping properties, or direct access, problem is that its not like an array, to get to the id, you must first get the right object, ... (thinking), i can name the object as the id,.. but not if its in an array.. damn,.. maybe later it comes to me..
ImageImage
B
70
S
21
G
7
Posts: 827
Reputation: 10,052

Post » Sun Apr 06, 2014 2:06 am

@vtrix
You guessed right, when I do
Code: Select all
JSON.ToJson(0,"Star1")
in javascript I in fact do
Code: Select all
JSON.stringify(this.root["Star1"]);
(well not exactly since I loop through the arguments provided to go through the path, but in a nutshell that's what I do)

So yeah, if you already have the JSON string as a template, instead of copying one object into another with that
Code: Select all
-> JSON: Load JSON at root["Star2"] to JSON.ToJson(0,"Star1")
Doing that:
Code: Select all
-> JSON: Load JSON at root["Star2"] to myTemplate
(with myTemplate containing the JSON string for your... template)
would be slightly more efficient but you still will do a javascript JSON.parse(myTemplate) which can potentially be a bit slow as well (meaning slower than just passing references in javascript)

If I rename the Length property I would probably not use ObjectSize but just Size, so you could use it for arrays or objects. (Note that your projects using Length will break when I'll make this change... or I can just set it to deprecated for a while to give you the opportunity to change it)

Not sure I get your thing about ID. If you want to reference a JSON object using some kind of numbering, an array seems ok, you just need that numbering to be consecutive. (well in theory, javascript allows non consecutive indexing but when generating a JSON from that, in chrome at least, you get an array with the holes filled with null)
For example you could do:
Code: Select all
-> JSON: Create new array at root[]
-> JSON: Create new object at root[2]
-> JSON: Create new object at root[4]
but then if you want to get back the JSON you'll get something like
Code: Select all
[null,null,{},null,{}]
The other possibility would be to reference them in an object using the ID as a key.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sun Apr 06, 2014 2:33 am

@Yann , ah ok i thought you keep the length for array and size for object, yes please if you could first deprecate it for a while, that would be great

well the importing from array to json is working, and its also one way to create/modify keys , it was pretty complicated if you consider that the array can be larger ( if items are added) than the json, i had to check where the existing objects ended, and make sure, that new paths to objects existed, maybe i just was better of deleting all objects and remake them, o well... it is noticeable faster tho... i joke :)

about id, its because i have worked alot with arrays recently, that it made sense to me to put my objects in indexes, put in fact, working with id's it doesn't make sense to have it in an array, (the reason i want to use id's is because ordering could potentially be changed by moving indexes, + i can simply reference the objects in multiple places,
so a better workflow would be to have objects with id's (like youre saying), and then i can simply put those id's an ordered list if i need it
making this all work will be another story ..
ImageImage
B
70
S
21
G
7
Posts: 827
Reputation: 10,052

Post » Sun Apr 06, 2014 12:32 pm

@vtrix
Not sure I understand all that you are saying, an example capx to illustrate your case would be better.

Anyway, a simple capx showing how to simply go from array to the JSON plugin:
arrayToJSONplugin.capx
Last edited by Yann on Sun Sep 14, 2014 8:43 pm, edited 1 time in total.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sun Apr 06, 2014 5:12 pm

@Yann

nice example, i think i just need to clear the current objects,
will the clear function work like if you have object size 5, you clear this object, it will object size 0?
ImageImage
B
70
S
21
G
7
Posts: 827
Reputation: 10,052

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests