Difference between revisions of "Talk:LlJsonSetValue"

From Second Life Wiki
Jump to navigation Jump to search
(Safe strings)
 
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:


Passing strings verbatim to [[llJsonSetValue]] or [[llList2Json]] is not safe. One way to escape strings properly to make them safe for use with both is to use this function:
Passing strings verbatim to [[llJsonSetValue]] or [[llList2Json]] is not safe. One way to escape strings properly to make them safe for use with both is to use this function:
<lsl>
<source lang="lsl2">
string String2Json(string s)
string String2Json(string s)
{
{
     return llGetSubString(llList2Json(JSON_OBJECT, [s,""]), 1, -5);
     return llGetSubString(llList2Json(JSON_OBJECT, [s,""]), 1, -5);
}
}
</lsl>
</source>
Examples:
Examples:
<lsl>
<source lang="lsl2">
llOwnerSay(String2Json(llUnescapeURL("%09"))); // outputs: Object: "\t"
llOwnerSay(String2Json(llUnescapeURL("%09"))); // outputs: Object: "\t"
llOwnerSay(String2Json("\n")); // outputs: Object: "\n"
llOwnerSay(String2Json("\n")); // outputs: Object: "\n"
Line 19: Line 19:
llOwnerSay(llList2Json(JSON_ARRAY, [String2Json("this \"string\" is not safe")]));
llOwnerSay(llList2Json(JSON_ARRAY, [String2Json("this \"string\" is not safe")]));
// both output: Object: ["this \"string\" is not safe"]
// both output: Object: ["this \"string\" is not safe"]
</lsl>
</source>


--[[User:Sei Lisa|Sei Lisa]] 17:19, 6 June 2014 (PDT)
--[[User:Sei Lisa|Sei Lisa]] 17:19, 6 June 2014 (PDT)
---------
Sadly, even this isn't a complete solution. With a URL like "http://www.google.com/", you'll get a "leaning-toothpicks" style value, but a call to llJsonSetValue() still won't pack it in correctly.  -- [[User:Lavanya Hartnell|Lavanya Hartnell]] 7:45, 11 August 2014 (PDT)
:Works for me. Can you give an example? Mine was <code>llJsonSetValue("[]",[0],String2Json("http://www.google.com/"))</code> and it returned the correct JSON -- [[User:Sei Lisa|Sei Lisa]] 10:39, 11 September 2014 (PDT)
::Well, turns out it won't work in all cases, but the reason is {{jira|BUG-6495}}. Basically, with LL's implementation of JSON you can't have JSON strings that have a ''']''' or a '''}''' character in them.--[[User:Sei Lisa|Sei Lisa]] ([[User talk:Sei Lisa|talk]]) 07:12, 27 January 2015 (PST)
---------
"null", "false" and "true" are interpreted as JSON null, false and true respectively. IMHO making most of the special identifiers somewhat pointless but importnatly increasing the amount of surface area of unsafe strings per Sei. Noticed that scientific notation is interpreted as a number as well.
Recommend having function description rewritten that value should be a JSON string or special identifier only to avoid further misunderstanding or confusion by scripters.
With a note that the function has some capability to wrap strings but that this is unsafe to rely on as strings could be interpreted to be a JSON string if they look like JSON such as looking like a number, scientific notation, null, true, false or start with a { or [ at the beginning of the string. This is because JSON that is a single value is technically valid JSON.
-- [[User:Nexii Malthus|<span style="color: #111; text-shadow:-1px -1px #ddd, 1px 1px #888;">Nexii Malthus</span>]] 12:57, 28 April 2024 (PDT)

Latest revision as of 11:57, 28 April 2024

Safe strings

Passing strings verbatim to llJsonSetValue or llList2Json is not safe. One way to escape strings properly to make them safe for use with both is to use this function:

string String2Json(string s)
{
    return llGetSubString(llList2Json(JSON_OBJECT, [s,""]), 1, -5);
}

Examples:

llOwnerSay(String2Json(llUnescapeURL("%09"))); // outputs: Object: "\t"
llOwnerSay(String2Json("\n")); // outputs: Object: "\n"
llOwnerSay(String2Json("this \"string\" is not safe")); // outputs: Object: "this \"string\" is not safe"
llOwnerSay(String2Json("\"ab\"")); // outputs: Object: "\"ab\""

// Examples of use with the corresponding functions:
llOwnerSay(llJsonSetValue("[]", [0], String2Json("this \"string\" is not safe")));
llOwnerSay(llList2Json(JSON_ARRAY, [String2Json("this \"string\" is not safe")]));
// both output: Object: ["this \"string\" is not safe"]

--Sei Lisa 17:19, 6 June 2014 (PDT)


Sadly, even this isn't a complete solution. With a URL like "http://www.google.com/", you'll get a "leaning-toothpicks" style value, but a call to llJsonSetValue() still won't pack it in correctly. -- Lavanya Hartnell 7:45, 11 August 2014 (PDT)

Works for me. Can you give an example? Mine was llJsonSetValue("[]",[0],String2Json("http://www.google.com/")) and it returned the correct JSON -- Sei Lisa 10:39, 11 September 2014 (PDT)
Well, turns out it won't work in all cases, but the reason is BUG-6495. Basically, with LL's implementation of JSON you can't have JSON strings that have a ] or a } character in them.--Sei Lisa (talk) 07:12, 27 January 2015 (PST)

"null", "false" and "true" are interpreted as JSON null, false and true respectively. IMHO making most of the special identifiers somewhat pointless but importnatly increasing the amount of surface area of unsafe strings per Sei. Noticed that scientific notation is interpreted as a number as well.

Recommend having function description rewritten that value should be a JSON string or special identifier only to avoid further misunderstanding or confusion by scripters.

With a note that the function has some capability to wrap strings but that this is unsafe to rely on as strings could be interpreted to be a JSON string if they look like JSON such as looking like a number, scientific notation, null, true, false or start with a { or [ at the beginning of the string. This is because JSON that is a single value is technically valid JSON.

-- Nexii Malthus 12:57, 28 April 2024 (PDT)