Difference between revisions of "Talk:LlList2Json"

From Second Life Wiki
Jump to navigation Jump to search
(Safe strings)
 
 
(3 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:22, 6 June 2014 (PDT)
--[[User:Sei Lisa|Sei Lisa]] 17:22, 6 June 2014 (PDT)
:Due to {{jira|BUG-6495}} this won't work. Nothing will. JSON is just broken.--[[User:Sei Lisa|Sei Lisa]] ([[User talk:Sei Lisa|talk]]) 07:27, 27 January 2015 (PST)
== String Handling Caveat ==
Currently the caveats section lists string handling as not enclosing strings automatically in inverted commas, however I just tried this myself and it does not appear to be true, as strings were enclosed as I would expect; was this historic behaviour during testing? Can the caveat be removed? -- --'''[[User:Haravikk_Mistral|Haravikk]]''' 02:50, 13 November 2016 (PST)
: There clearly is still a difference, using the expressions shown in the warning. just as documented. The following script:
<source lang="lsl2">
default
{
    state_entry()
    {   
        llOwnerSay(llDumpList2String(llJson2List(llList2Json(JSON_ARRAY,  ["bacon", "true", "false", "null"])),  "~"));
   
        llOwnerSay(llDumpList2String( llJson2List(llList2Json(JSON_ARRAY, ["\"bacon\"", "\"true\"", "\"false\"", "\"null\"" ])), "~"));
    }
}
</source>
: prints:
[11:40] Object: bacon~﷖~﷗~﷕
[11:40] Object: bacon~true~false~null
: --[[User:ObviousAltIsObvious Resident|ObviousAltIsObvious Resident]] ([[User talk:ObviousAltIsObvious Resident|talk]]) 11:45, 14 November 2016 (PST)

Latest revision as of 12:45, 14 November 2016

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:22, 6 June 2014 (PDT)

Due to BUG-6495 this won't work. Nothing will. JSON is just broken.--Sei Lisa (talk) 07:27, 27 January 2015 (PST)

String Handling Caveat

Currently the caveats section lists string handling as not enclosing strings automatically in inverted commas, however I just tried this myself and it does not appear to be true, as strings were enclosed as I would expect; was this historic behaviour during testing? Can the caveat be removed? -- --Haravikk 02:50, 13 November 2016 (PST)

There clearly is still a difference, using the expressions shown in the warning. just as documented. The following script:
default
{
    state_entry()
    {    
        llOwnerSay(llDumpList2String(llJson2List(llList2Json(JSON_ARRAY,  ["bacon", "true", "false", "null"])),  "~"));
    
        llOwnerSay(llDumpList2String( llJson2List(llList2Json(JSON_ARRAY, ["\"bacon\"", "\"true\"", "\"false\"", "\"null\"" ])), "~"));
    }
}
prints:
[11:40] Object: bacon~﷖~﷗~﷕
[11:40] Object: bacon~true~false~null
--ObviousAltIsObvious Resident (talk) 11:45, 14 November 2016 (PST)