Difference between revisions of "LlList2Json"

From Second Life Wiki
Jump to navigation Jump to search
(Noted that all strings are trimmed during this operation)
 
(13 intermediate revisions by 7 users not shown)
Line 12: Line 12:
* If type is [[JSON_ARRAY]] then a string representing a json array will be returned.
* If type is [[JSON_ARRAY]] then a string representing a json array will be returned.
* If type is any other string then [[JSON_INVALID]] will be returned.
* If type is any other string then [[JSON_INVALID]] will be returned.
|constants={{Constants/JSON|subset=*}}
* Json types from the input list are inferred from the LSL data type
|examples
** [[integer]] and [[float]] types are encoded as [[JSON_NUMBER]]
** "true" and "false" are encoded as [[JSON_TRUE]] and [[JSON_FALSE]], respectively
** "null" is encoded as [[JSON_NULL]]
** properly-formatted json strings in the input are kept as-is (will either be [[JSON_STRING]], [[JSON_OBJECT]], or [[JSON_ARRAY]])
** all other strings encoded as [[JSON_STRING]]. Strings are trimmed of whitespace at the beginning and end, as if passed thru [[llStringTrim]]
** Note in particular that strings containing valid JSON numbers are converted to JSON strings, not left as JSON numbers ({{Jira|BUG-6284}}). This differs to the treatment that [[llJsonSetValue]] gives to strings that contain JSON numbers, which leaves them as numbers.
|constants={{LSL Constants/JSON|subset=*}}
|examples=
<source lang="lsl2">
string CSV2Json(string csv)
{
    list li = llCSV2List(csv);
    return llList2Json(JSON_ARRAY, li);
}
// This function converts a comma separated values string to a Json array string.
// CSV strings are often used for link-messages, notecards and they are easier to type as input commands.
// A Json-Array can store multiple of those as a nested list within the same Json-string via JSON_APPEND.
</source>
* You could store multiple lists that set different particle effects in the same string. Use LlList2Json() to write the string and llJson2List() to read the particle system defining lists from it. The loss in float-accuracy (Json has BAD float-to-string-conversion) does not matter as much for particle effects.
 
* You could store multiple lists, that set different llSetPrimitiveparamsFast() values for different situation (like different animation key frames), in the same Json String.
 
|helpers
|helpers
|also_functions={{LSL DefineRow||[[llJson2List]]|}}
|also_functions={{LSL DefineRow||[[llJson2List]]|}}
Line 26: Line 47:
|also_articles={{LSL DefineRow||[[Typecast]]|}}
|also_articles={{LSL DefineRow||[[Typecast]]|}}
|notes=
|notes=
|caveats=* Note that string '''values''' items are interpreted as JSON, not LSL strings. Quotation marks, if required, must be added explicitly. For example, <code>[[llJson2List]]([[llList2Json]]([[JSON_ARRAY]], ["bacon", "true", "false", "null"]))</code> returns the LSL list <code>["bacon", [[JSON_TRUE]], [[JSON_FALSE]], [[JSON_NULL]]]</code>, while <code>[[llJson2List]]([[llList2Json]]([[JSON_ARRAY]], ["\"bacon\"", "\"true\"", "\"false\"", "\"null\""]))</code> returns the LSL list <code>["bacon", "true", "false", "null"]</code>
|permission
|permission
|negative_index
|negative_index
|sort=CSV2List
|sort=List2Json
|cat1=List
|cat1=List
|cat2=String
|cat2=String
|cat3=Data Conversion
|cat3=Data Conversion
|cat4=Json
|cat4=JSON
|history = Date of Release  [[ Release_Notes/Second_Life_Server/13#13.05.20.276191 | 20/05/2013 ]]
|haiku={{Haiku|Their ships list to port|for what reason I know not|we list to JSON}}
{{Haiku|Listen to Jason|cryptically my sensei said|I must have misheard}}
}}
}}

Latest revision as of 17:05, 20 April 2017

Summary

Function: string llList2Json( string type, list values );

This function takes a list and returns a JSON string of that list as either a json object or json array.
Returns a string that is either values serialized as a JSON type, or if an error was encountered JSON_INVALID.

• string type
• list values

To convert a json formatted string into a list use llJson2List.

Specification

See Json_usage_in_LSL

  • If type is JSON_OBJECT the list must be a strided list of key, value pairs and a string representing a json object will be returned.
  • If type is JSON_ARRAY then a string representing a json array will be returned.
  • If type is any other string then JSON_INVALID will be returned.
  • Json types from the input list are inferred from the LSL data type
    • integer and float types are encoded as JSON_NUMBER
    • "true" and "false" are encoded as JSON_TRUE and JSON_FALSE, respectively
    • "null" is encoded as JSON_NULL
    • properly-formatted json strings in the input are kept as-is (will either be JSON_STRING, JSON_OBJECT, or JSON_ARRAY)
    • all other strings encoded as JSON_STRING. Strings are trimmed of whitespace at the beginning and end, as if passed thru llStringTrim
    • Note in particular that strings containing valid JSON numbers are converted to JSON strings, not left as JSON numbers (BUG-6284). This differs to the treatment that llJsonSetValue gives to strings that contain JSON numbers, which leaves them as numbers.

Type Flags Value Unicode Integer URL Encoded HTML Encoded Description
JSON_INVALID U+FDDO 64976 "%EF%B7%90" &#xFDD0; Value returned when inputs are not well formed.
JSON_OBJECT U+FDD1 64977 "%EF%B7%91" &#xFDD1;
JSON_ARRAY U+FDD2 64978 "%EF%B7%92" &#xFDD2;

Caveats

Examples

string CSV2Json(string csv)
{
    list li = llCSV2List(csv);
    return llList2Json(JSON_ARRAY, li);
}
// This function converts a comma separated values string to a Json array string. 
// CSV strings are often used for link-messages, notecards and they are easier to type as input commands. 
// A Json-Array can store multiple of those as a nested list within the same Json-string via JSON_APPEND.
  • You could store multiple lists that set different particle effects in the same string. Use LlList2Json() to write the string and llJson2List() to read the particle system defining lists from it. The loss in float-accuracy (Json has BAD float-to-string-conversion) does not matter as much for particle effects.
  • You could store multiple lists, that set different llSetPrimitiveparamsFast() values for different situation (like different animation key frames), in the same Json String.

See Also

Constants

•  JSON_ARRAY
•  JSON_OBJECT
•  JSON_INVALID

Functions

•  llJson2List
•  llJsonGetValue
•  llJsonSetValue
•  llJsonValueType

Articles

•  Typecast

Deep Notes

History

Date of Release 20/05/2013

Signature

function string llList2Json( string type, list values );

Haiku

Their ships list to port
for what reason I know not
we list to JSON

Listen to Jason
cryptically my sensei said
I must have misheard