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

Post your work in progress addons and get feedback

Post » Tue Dec 02, 2014 8:23 am

Hi,

A useful feature for my purposes would be to set the path to one "level' back.

So, if the path is set as follows:

JSON: Set Current Path to [email protected]"my","path","to","an","array"


setCurrentPath_one_back

would set it as so:

"my","path","to","an"


this is very useful for when you loop though complex structures and need to "automatically" back up one step, before going down several path steps again.

Dan
B
9
S
4
G
1
Posts: 207
Reputation: 1,385

Post » Tue Dec 02, 2014 2:41 pm

Thank you @Yann for your response! I agree about your reasoning, but I think the benefits might overweight the losses. For a complex JSON-object where we have to go down even more hierarchy's it quickly gets messy and probably even more error prone when doing it in Construct.

As a side-note, I actually tested what you described (created new object at every "level"), and had it worked I wouldn't have even bothered to look for this "bug". But I will try again and see if I maybe made some error the first time. Will get back to you! EDIT: It seems I had missed that there were an Action called "New Object" which was necessary for it to work as intended (which however isn't necessary when creating first-level objects).

This is a lot more code (and probably more error prone as more objects gets added)
Code: Select all
Clear [email protected]
Set New Object at [email protected]   // so the clear isn't necessary
Set "Hello World" at [email protected]"someText"
Set New Object at [email protected]"location"
Set New Object at [email protected]"location","latitude"
Set New Object at [email protected]"location","latitude","someCoordObject"
Set 100 at [email protected]"location","latitude","someCoordObject","Coord1"
Set 110 at [email protected]"location","latitude","someCoordObject","Coord2"
Set 120 at [email protected]"location","latitude","someCoordObject","Coord3"


Then this:
Code: Select all
Clear [email protected]
Set New Object at [email protected]   // so the clear isn't necessary
Set "Hello World" at [email protected]"someText"
Set 100 at [email protected]"location","latitude","someCoordObject","Coord1"
Set 110 at [email protected]"location","latitude","someCoordObject","Coord2"
Set 120 at [email protected]"location","latitude","someCoordObject","Coord3"


Imagine when we have multiple objects that have multiple hierarchy's the actions required will expontential (figure of speech :P)!
B
4
Posts: 12
Reputation: 224

Post » Wed Dec 03, 2014 12:31 am

@Whyser
well, remember you can also do:

Code: Select all
Load JSON " {
  ""someText"":""Hello World"",
  ""location"": {
      ""latitude"":"&mylatitude&"
      ""longitude"":"&mylongitude&"
   }
}" at [email protected]

And trust me, I recently made a manhattan distance algorithm for strategy game prototype using this JSON plugin, and having the plugin spitting out warnings was a big help. I had forgotten some checks and looping through empty data.
Without those warning the data would have been wrongly, automatically filled and I would have had very hard to fix bugs.
You have to like "failing early" (:

@grossd
that's an idea, I'll think about it thanks =)
B
68
S
22
G
14
Posts: 1,485
Reputation: 16,561

Post » Thu Dec 04, 2014 8:40 pm

@Yann
I agree with you kinda, but I think it might depend on the use-case! My scenario is that I create those fields only to call "AsJSON(0)" directly after in an AJAX call, so that i can send JSON-formatted data to a server.

Doing what you suggested (Load JSON) is very error prone and messy! If that's what i wanted to do, I would just write the JSON-object-string myself. :P
B
4
Posts: 12
Reputation: 224

Post » Thu Dec 04, 2014 8:52 pm

@Yann,

In the meantime I created my own C2 based "library" function to push and pop a set the path using an Array/Stack. It works pretty well ...

so, whenever i descend I push the current last path element into a stack, and when i return from the decent i pop the stack, after each push and pop I set the path looping through the current elements in the stack (array).

So, if you could implement a simple "pop" that would be great.
B
9
S
4
G
1
Posts: 207
Reputation: 1,385

Post » Thu Dec 18, 2014 8:01 pm

Pulling my hair out trying to set the text from an item in the json produced here:

http://api.tvmaze.com/search/shows?q=walking+dead

How can I set a text object to the value of the chosen key?

Example: value "4.401577" from key "score".
B
17
S
2
Posts: 88
Reputation: 1,503

Post » Thu Dec 18, 2014 11:27 pm

Looking at your JSON, you have an array of objects with two keys, "score" and "show"
So to get the score of the first object, you can do it like this:
Code: Select all
JSON.Value(0,0,"score")
The first 0 means "from root", the seconds means, "first element", and then "key score"

in javascript it would correspond to
Code: Select all
root[0]["score"]
B
68
S
22
G
14
Posts: 1,485
Reputation: 16,561

Post » Fri Dec 19, 2014 4:07 am

Yann wrote:Looking at your JSON, you have an array of objects with two keys, "score" and "show"
So to get the score of the first object, you can do it like this:
Code: Select all
JSON.Value(0,0,"score")
The first 0 means "from root", the seconds means, "first element", and then "key score"

in javascript it would correspond to
Code: Select all
root[0]["score"]


You're a life-saver. Thanks.
B
17
S
2
Posts: 88
Reputation: 1,503

Post » Wed Jan 07, 2015 4:23 am

@Yann

This is quite a powerful plugin, thanks for creating it. I was wondering if you plan on updating the plugin to address this issue:
Yann wrote: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);


I have a workaround where I clear the entire array, and recreate it sans the deleted values, but it would be a lot cleaner to delete the value directly.
B
64
S
19
G
8
Posts: 341
Reputation: 12,195

Post » Fri Jan 09, 2015 8:50 am

@zatyka
yeah I'll probably release the fix someday, but I had started implementing object references to give a bit more power to the plugin, but got stuck with how to import/export references using JSON.
I found some possible solution from dojo where they have a specific syntax for references but I didn't have time to really wrap my head around that to extract what I need from dojo (I really don't want to add dojo to the plugin just for their JSON functions)
So, yeah... someday :D

I could just release a fix on the array bug, but.... sadly... I'm not using version control so... <_<
B
68
S
22
G
14
Posts: 1,485
Reputation: 16,561

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests