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

Post your work in progress addons and get feedback

Post » Mon Aug 04, 2014 10:51 am

@rexrainbow
When I'll find a way to have cyclic objects and being able to export/import them as JSON.
For that I investigated how dojo does it
http://dojotoolkit.org/reference-guide/1.10/dojox/json/ref.html
And I'd like to extract only that specific algorithm to use in the plugin (not having to carry the entire dojo library)

For now I managed to have a Save/Load Reference actions so you can basically put objects inside objects by reference instead of copy. (even from different object-type of the JSON plugin)
So you can make a cyclic linked list for example

But if you export as JSON and import it back, you'll lose the cyclic aspect, so it breaks all the save/load features.
Even if I manage that, I'm not sure the save/load will always work since I won't be able to save as JSON the possible link between two different objects.
So yeah, I hit a wall and I had other things to do so the dev stopped for awhile.

However, I already used the plugin in a few of my projects and it's behaving nicely. Though it adds some layer of complexity.
Though bear in mind that there's two bugs hanging in the current version:
Code: Select all
- bug fix: array elements weren't properly deleted using the delete function
- bug fix: error when using clear and delete with current path.

If I used some subversion I would have just made a release with those fixes but now they are merged with my half-implemented reference thing so...
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Thu Sep 04, 2014 12:40 am

It will be useful if there has a JSON behavior , since family does not has container feature, only has behavior.
B
108
S
26
G
258
Posts: 4,430
Reputation: 145,179

Post » Thu Sep 04, 2014 8:07 am

@rexrainbow
Maybe a container behavior? so you could link any datastructure to families? I'd like to avoid forking my plugin into a behavior, since it's a pain to maintain two almost similar code base.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Thu Sep 04, 2014 8:57 am

@Yann

I had put this request to Ashley. As you know, he is very very busy, so that the only one solution is making a behavior version.
B
108
S
26
G
258
Posts: 4,430
Reputation: 145,179

Post » Tue Sep 16, 2014 8:14 pm

So I'm trying to use this with json data that's been put into an array, and its reporting back this.

[c2array] is a boolean : True
[size] is an array of length: 3
+---[0] is a number: 1
+---[1] is a number: 1
+---[2] is a number: 1
[data] is an array of length: 1
+---[0] is an array of length: 1
+---+---[0] is an array of length: 1
+---+---+---[0] is an object
+---+---+---+---[Username] is a string : "Justin"
+---+---+---+---[Password] is a string : "Test"
+---+---+---+---[objectId] is a string : "NYulexXQuC"
+---+---+---+---[createdAt] is a string : "2014-09-15T17:44:35.614Z"
+---+---+---+---[updatedAt] is a string : "2014-09-15T17:44:35.614Z"

But i can't figure out how to access the values of the data eg; username, password etc.

eg: JSON.Value(0,"Username") is undefined.
B
82
S
39
G
15
Posts: 982
Reputation: 16,712

Post » Tue Sep 16, 2014 10:49 pm

@justifun
Of course it's undefined
If it were in javascript you would probably do something like
myJSONData["data"][0][0][0]["Username"]

So in the plugin it would translate as
JSON.Value(0,"data",0,0,0,"Username")

Ya need to tell the thing where to put the data from.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Wed Sep 17, 2014 1:27 am

Thanks @Yann.

Is the syntax similar if you are reading it from an array object instead of using your plugin?
B
82
S
39
G
15
Posts: 982
Reputation: 16,712

Post » Wed Sep 17, 2014 6:15 am

@justinfun
Nope, the array object is just using indices and you can only have 3 dimension
something like Array.At(3,5,1) you can't have strings as "index"
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sun Nov 30, 2014 8:10 pm

I think I found a bug.

It seems it's not possible to do the following:

clear root@
set "Hello World" at root@"someText"
set 10 at root@"location","latitude"
set 20 at root@"location","longitude"

This will cause a warning and the JSON object will be {"someText":"Hello World"}. After a long time of reading your plugin I think I found the error (or at least a fix for my problem). In the method on Line 208: "instanceProto.setValueFromPath".

Change this:
Code: Select all
if(i < path_.length-1) {
   obj = obj[path_[i]];
} else {
   obj[path_[i]] = value; // silently create a new property if doesn't exist yet
}


To this:
Code: Select all
if(i < path_.length-1) {
   if(!(path_[i] in obj))
      obj[path_[i]] = {};

   obj = obj[path_[i]];
} else {
   obj[path_[i]] = value; // silently create a new property if doesn't exist yet
}
B
4
Posts: 12
Reputation: 224

Post » Tue Dec 02, 2014 1:33 am

@Whyser
Yup, it's not a bug, it's done on purpose, you should first manually create an object before filling it with property/value pairs.
I prefer it not to be done silently

Following your idea, I should check if an argument is a string or a number because
Code: Select all
set "Hello World" at root@"0"
it should automagically do
Code: Select all
var root = {};
root["0"] = "Hello World";


Whereas
Code: Select all
set "Hello World" at [email protected]
it should automagically do
Code: Select all
var root = [];
root[0] = "Hello World";


And I think that's messy and error prone.

So basically your code should look like:
Code: Select all
Clear root@
Set New Object at root@   // so the clear isn't necessary
Set "Hello World" at root@"someText"
Set New Object at root@"location"
Set 10 at root@"location","latitude"
Set 20 at root@"location","longitude"


it's equivalent to the javascript:
Code: Select all
root = {};
root["someText"] = "Hello World";
root["location"] = {};
root["location"]["latitude"] = 10;
root["location"]["longitude"] = 20;


so yeah bottom line, the json plugin shouldn't automagically create objects for you.
if I'm not wrong, if you try inserting a property/value and the object doesn't exist, you should get the following warning in the console:
Code: Select all
invalid path: [email protected]
or
Code: Select all
invalid path: [email protected]


This "invalid path" warning allowed me to debug some wrong code many times and I think silencing it would be a mistake. Yes you have to write more actions, but at least you stay in control of your datastructure.
And events using the JSON plugins can get quite messy, 'cause construct2 doesn't have a lot of possibilities UI-wise. (I wish you could use the bracket notation)

Hope I make sense.

In any case you can rip apart my plugin to your heart's content :D after all it's still in "beta" and if you find good ideas I'd be glad to add them.

But keep in mind I have a more advanced version on my computer since more than a year, but I haven't released it because it's incomplete. I wanted to be able to have references to other object to avoid having to copy things and be able to create fun things like cyclic datastructures.
But it's breaking the JSON serialization so you wouldn't be able to save/load the object's state properly.
I found something from the dojo framework that might be a solution (they have a JSON.stringify that support references) but I didn't have time to rip it apart to just get the usefull bits (I wouldn't want to include the whole dojo library for just a plugin)
(that's the current state of things concerning this plugin :D)
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 2 guests