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

Post your work in progress addons and get feedback

Post » Thu Apr 17, 2014 10:34 pm

Hello,

This looks really neat. Can we expect some sort of example capx ones the plugin will be released "clean"? Thanks
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
85
S
27
G
20
Posts: 1,962
Reputation: 18,651

Post » Thu Apr 17, 2014 11:51 pm

B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Fri Apr 18, 2014 12:22 am

Yann wrote:@megatronx
In the first post there are already 2 example capx.
https://dl.dropboxusercontent.com/u/235 ... ction.capx
https://dl.dropboxusercontent.com/u/235 ... otype.capx


Oh sorry, I've missed them! >.>
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
85
S
27
G
20
Posts: 1,962
Reputation: 18,651

Post » Fri Apr 18, 2014 1:53 am

@Yann , damn, just when i wanted to get some sleep, i see this.. :) great! who needs sleep! testing....
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Sat Apr 19, 2014 12:25 am

@Yann , first impression, looks great, love the debugger and log info, and the new way of displaying with "@" feels a bit lighter,

but just wanted to quickly say, that something as loopindex is missing, and could be usefull for not making a counter, or what i just did,
for an array int(json.currentkey) , is that a design decision?
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Sat Apr 19, 2014 2:26 am

@vtrix
Well... The problem is a mix between javascript ambiguity, striving for simplicity (nice way to say laziness) and giving as much control as possible

In javascript, you usually loop through array elements like this:
Code: Select all
for (var i = 0; i < myArray.length; i++) {
      // do things with myArray[i]
}

and you loop through objects members like this:
Code: Select all
for (var m in myObject) {
      // do things with myObject[m]
}


Thing is, in javascript arrays are also some kind of objects. So looping using a for-in works.
However there are few differences.
- With the first solution you have an index that increments. So expected ordering is ensured and you only explore the range between 0 and size-1
- With the second you treat array indexes as object properties, so if you did weird things like adding a value to an array using a non positive integer (like -1) or a key as an index (c2 doesn't prevent that), you would, at least, be able to loop through it using For each property. However, if I'm not wrong the JSON exporting will just export the array part, so overall adding properties to an array should be discouraged)
- If you skip array indexes, like for example set a value at index 10 on an less than 9-elements array, the first loop will allow you to loop through all the indexes from 0 to 10 (the inbetweens will return undefined and the JSON export will be padded with null), whereas if you use the second loop (for... in) you'll only loop through existing indexes (skipping the inbetweens)

So my idea was to leave as much possibilities as the user might want (so internally choosed "for .. in") while avoiding confusion (there's only one type of loop)
This way:
- If you want to loop through arrays like an object (for... in), use For each property
- otherwise, use a for/repeat loop like this:
Code: Select all
+ System: repeat JSON.Size(0,"path","to","array")
   -> Browser: log JSON.Value(0,"path","to","array",loopindex)
which is equivalent to for(var i = 0; i < arr.length, i++) {...}

As for the necessity to cast currentKey to int... it's because I return currentKey as a string, I should probably return it as an "any" type (meaning string or number).
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Wed Apr 23, 2014 4:12 am

@Yann

i'm having trouble deleting an index of array

delete root@"array","0" or 0 both just set the arrayvalue to null
the path "key" however is gone, but the size is still counting the null as an entry

edit: i think i gonna use strictly object from now on and use only c2 arrays for listing things
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Wed Apr 23, 2014 5:44 pm

@vtrix
thanks nice finding, I was doing
Code: Select all
delete array[0];

but this doesn't change the size of the array so javascript set it to undefined (which is transformed to null on export)
I have to do
Code: Select all
array.splice(0,1);
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Fri Apr 25, 2014 2:48 am

@Yann , just wanted to mention, i have some instances where i could use the json.key like it was before, json.key(1), i know it was me who suggested using currentkey, but the way its now, you cant use it outside of a loop and the key(1) option isnt there anymore,, if you set the path to current and then use currentkey it doesn't return it.

i mean, currentkey, or currentvalue, would always return the the key / value at currentpath whether you're in a loop or not, so in sense its a shortcut to key(1) or value(1), but the parameter kind should also be an option

so in the end you have currentkey, currentvalue, or key(0,"..") key(1) , value(0,"..") value(1) with parameter usage

why i suggested it, is because i think it will be easier to understand for somebody new to the plugin, you just set the currentpath, and then use the currentkey/currentvalue, for advance use you have parameter options.
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Fri Apr 25, 2014 11:37 am

@vtrix
First there's never been any JSON.Key(...) expression, with or without parameters.
Getting the key at a specific path doesn't mean much thing, because the key would be the last part of the path you are setting.
Key(0,"my","path") should return "path". Key(0) shouldn't return anything (although it's "root" in javascript, but this implementation detail needs to be hidden from the user)
Mabye the only use I could think of is Key(1) that would return the last component of the CurrentPath, but you should already know it in your code somehow.
The only place where you could be unable to know the CurrentPath is in the foreach loop, that's why CurrentKey is set here.

Otherwise, Value(0,"my","path") is already returning the value at the given path, and Value(1,"my","path") as well but relatively to CurrentPath.
The difference between v1.0 and v1.1 is just the addition of CurrentValue which is an alias to Value(1).
The other change I made was to reset the CurrentPath to what it should be at the begining of each loop iteration, and reset it to what it was before the foreach loop when you exit it.

I was thinking about providing an alias like ValueFromRoot("my","path") and ValueFromCurrent("my","path") to make things more clear (maybe someone has a better idea)

So, to summarize, I fail to see where a `Key` expression would be usefull, and the `Value` expression already works as expected, isn't it?

And as far as CurrentKey and CurrentValue goes I prefer them to be simple. They are more or less the equivalent of the Dictionary counterpart.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 1 guest