Jump to content

JSON Usage Question


Wandering Soulstar
 Share

You are about to reply to a thread that has been inactive for 3671 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi All,


I am working on a new project, and am using JSON structures for the first time. One of my modules is having a problem and while I am in the process of debugging, on the train this morning a question came to mind, and since it will likely be hours until I am back home again and able to go in world to continue testing, I thought I'd check here if I am doing something basically wrong.

Lets assume in script_main I define the following Global variable:

 

TEXTURE_PARAMS = llList2Json(JSON_OBJECT,
[
	SET_TYPE, MATCH,
	HORIZ_AXIS, X,
	VERT_AXIS, Z,
	SET_TEXT, (string)FALSE,
	SET_COLOUR, (string)FALSE,
	SET_ROT, (string)FALSE,
	SET_VERT, (string)FALSE,
	SET_HORIZ, (string)FALSE,
	BASE_PRIM, EMPTY_STR
]);

 Key to note, and where I am wondering if there is a problem is in the last node (BASE_PRIM) which is initialized to = "".

 

In another script I execute the following:

ME = llList2Json(JSON_OBJECT,
[
	SIZE, llList2Json(JSON_OBJECT,
	[
		X, (string)size.x,
		Y, (string)size.y,
		Z, (string)size.z
	]),
	FACE, llList2Json(JSON_OBJECT,
	[
		NUMBER, (string)touchFace,
		TEXTURE, llList2String(attribs, 0),
		COLOUR, (string)llGetColor(touchFace),
		ROT, llList2String(attribs, 3),
		HORIZ, (string)texSize.x,
		VERT, (string)texSize.y
	])
]);

llMessageLinked(LINK_THIS, CTRL_CHANNEL, llList2Json(JSON_OBJECT, [ACTION, ST_CLICK, PARAMETER, ME]), NULL_KEY);

 This message call is then picked up in script_main where after a few checks the following is executed:

if (llJsonGetValue(TEXTURE_PARAMS, [BASE_PRIM]) == EMPTY_STR)
{
	//Add the passed ME to the return structure for future Child calls
	TEXTURE_PARAMS = llJsonSetValue(TEXTURE_PARAMS, [BASE_PRIM], llJsonGetValue(message, [PARAMETER]));
	response = PARENT;
}

 So, now to my question. Is the set statement valid? What I am trying to do is set the node BASE_PRIM to hold the JSON structure ME that was passed in the linked message call (also as part of a JSON). I am assuming this works but had a doubt as to what this actually looked like under the covers and perhaps was losing some sort of detail in the moving around.

 

Thanks in advance!

 

Link to comment
Share on other sites


Wandering Soulstar wrote:

Hi All,

 

I am working on a new project, and am using JSON structures for the first time. One of my modules is having a problem and while I am in the process of debugging, on the train this morning a question came to mind, and since it will likely be hours until I am back home again and able to go in world to continue testing, I thought I'd check here if I am doing something basically wrong.

Lets assume in script_main I define the following Global variable:

 
TEXTURE_PARAMS = llList2Json(JSON_OBJECT,[	SET_TYPE, MATCH,	HORIZ_AXIS, X,	VERT_AXIS, Z,	SET_TEXT, (string)FALSE,	SET_COLOUR, (string)FALSE,	SET_ROT, (string)FALSE,	SET_VERT, (string)FALSE,	SET_HORIZ, (string)FALSE,	BASE_PRIM, EMPTY_STR]);

 Key to note, and where I am wondering if there is a problem is in the last node (BASE_PRIM) which is initialized to = "".

 

In another script I execute the following:
ME = llList2Json(JSON_OBJECT,[	SIZE, llList2Json(JSON_OBJECT,	[		X, (string)size.x,		Y, (string)size.y,		Z, (string)size.z	]),	FACE, llList2Json(JSON_OBJECT,	[		NUMBER, (string)touchFace,		TEXTURE, llList2String(attribs, 0),		COLOUR, (string)llGetColor(touchFace),		ROT, llList2String(attribs, 3),		HORIZ, (string)texSize.x,		VERT, (string)texSize.y	])]);llMessageLinked(LINK_THIS, CTRL_CHANNEL, llList2Json(JSON_OBJECT, [ACTION, ST_CLICK, PARAMETER, ME]), NULL_KEY);

 This message call is then picked up in script_main where after a few checks the following is executed:
if (llJsonGetValue(TEXTURE_PARAMS, [bASE_PRIM]) == EMPTY_STR){	//Add the passed ME to the return structure for future Child calls	TEXTURE_PARAMS = llJsonSetValue(TEXTURE_PARAMS, [bASE_PRIM], llJsonGetValue(message, [PARAMETER]));	response = PARENT;}

 So, now to my question. Is the set statement valid? What I am trying to do is set the node BASE_PRIM to hold the JSON structure ME that was passed in the linked message call (also as part of a JSON). I am assuming this works but had a doubt as to what this actually looked like under the covers and perhaps was losing some sort of detail in the moving around.

 

Thanks in advance!

 

This is all a rough draft of an idea, merely psuedo-code that needs a lot more work before it will be remotely compilable.

 

Keep in mind that a JSON object consists of "Key":Value pairs, where the "Key" must be a string. So:

// Globally declared

string TextureParams;

 

// Assigned within an event handler

TextureParams = llList2Json(JSON_OBJECT,[ "SetType", "match", "HorizAxis", x, "VertAxis", y, "SetText", FALSE, "SetColor", FALSE, "SetRot", FALSE, "SetVert", FALSE, "SetHoriz, FALSE, "BasePrim", ""]);

 Note: I've gotten away from the all caps and underscores, too easily to confuse that way of writing with the LSL constants. Also note that I'm not stringifying the LSL FALSE, no need of that. The variables x and y must need be declared as floats somewhere and assigned to.

 

Your second snippet is even more confusing. Both SIZE and FACE must be strings. The Value of "Size" ought to simply be a vector. You're complicating things by having a deeper JSON object as a Value for "Face". You can do it, just keep in mind that you'll need to write: integer faceNumber = (integer)llJsonGetValue(ME, ["face", "number"]); Your llMessageLinked is, again, not forming a JSON object with strings as the "Keys".

 

Your third snippet would write the entire ME object, as you defined it in the other script, to the Value of the BASE_PRIM Key of TEXTURE_PARAMS if, and only if, you changed everything throughout to where you were using strings for "Keys".

 

Looking at your rough draft here, I get the impression you feel the Values all have to be strings. It's the other way around, all the "Keys" must be strings. OK?

 

[ETA] When you're using llJsonSetValue, you do have to set the value you're supplying to string but with llList2Json you don't, just alternate the string "Keys" and their associated Values within the supplied list. Oh, and always remember, everything that comes out of a JSON text is always coming out as a string, regardless of what it was going in.

Link to comment
Share on other sites

LepreKhaun,

Thanks for the response, guess I was not clear enough. All the Keys are strings, this is cut from my code and I did not include all the declarations, but every key is a 'Constant' set to equal a unique string. So both SIZE & FACE are strings. Good point on just assigning the vector to SIZE without the sub parts, will have to check the rest of my code to see if there was a reason for that. And as to FACE 'complicating things' it would if a Prim only had one face, but this needs to be able to hold the specific values for an individual face, and ME can have multiple Faces.

 

It does seem that my problem is elsewhere, as the key question I had you answered:

'Your third snippet would write the entire ME object, as you defined it in the other script, to the Value of the BASE_PRIM Key of TEXTURE_PARAMS'

And that was what I was wondering, if it was going to be written there as a JSON that I could then query.,

Link to comment
Share on other sites


Wandering Soulstar wrote:

LepreKhaun,

Thanks for the response, guess I was not clear enough. All the Keys are strings, this is cut from my code and I did not include all the declarations, but every key is a 'Constant' set to equal a unique string. So both SIZE & FACE are strings. Good point on just assigning the vector to SIZE without the sub parts, will have to check the rest of my code to see if there was a reason for that. And as to FACE 'complicating things' it would if a Prim only had one face, but this needs to be able to hold the specific values for an individual face, and ME can have multiple Faces.

 

It does seem that my problem is elsewhere, as the key question I had you answered:

'
Your third snippet would write the entire ME object, as you defined it in the other script, to the Value of the BASE_PRIM Key of TEXTURE_PARAMS'

And that was what I was wondering, if it was going to be written there as a JSON that I could then query.,

Ahhhh, all that makes sense then. Yes, you're on the right track.

 

However I could make a suggestion that will radically improve the performance: Get away from using JSON objects and just go with nested arrays. The reason I suggest this is that both memory and speed are adversely affected as the JSON text lengthens. The JSON manipulations begin to bog down very quickly and heap memory will grow rapidly in a short time. You can sprinkle a few llOwnerSay(llGetFreeMemory()); statements around to see exactly what I'm refering to.

 

So, the idea is to keep your JSON text as short as possible, and the JSON array can't be beat. Just change your constant assignments from strings to integers that represent where in the array the Value is to be accessed. As in:

// Use these to access the index of the required ValueSET_TYPE = 0;HORIZ_AXIS = 1;VERT_AXIS = 2;SET_TEXT = 3;SET_COLOUR = 4;SET_ROT = 5;SET_VERT = 6;SET_HORIZ = 7;BASE_PRIM = 8;TEXTURE_PARAMS = llList2Json(JSON_ARRAY,[    MATCH,    X,    Z,    FALSE,    FALSE,    FALSE,    FALSE,    FALSE,    ""]);

 And, BASE_PRIM would later become a nested array, along the same lines. Follow?

 

This works because you are writing the JSON arrays and have complete control over what is going where. One only needs to use the JSON object (with its associated weight of the "Keys") when communicating with another program, which may order the elements differently.

Link to comment
Share on other sites

I was wondering about the difference and imagined that it would get a bit memory intensive with the text keys. I thought initially about using the JSON ARRAYs and numbered constants but the rhing thast kept me from doing that  is that sections of my code reference and set values in the JSON based on text values that they receive from other sources.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3671 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...