Difference between revisions of "LlJsonValueType"

From Second Life Wiki
Jump to navigation Jump to search
m (closing comment)
(-30 syntax errors (ran trough mono compiler -nonsense that differenciated data typers for numbers. it's all floats as short strings for JSON !!!)
Line 10: Line 10:
|spec=See [[Json_usage_in_LSL]]
|spec=See [[Json_usage_in_LSL]]
|constants={{LSL_Constants/JSON}}
|constants={{LSL_Constants/JSON}}
|examples=
|examples=<lsl>
<lsl>


//all these functions are untested functions
 
//see them as buggy PSEUDO-code and consider yourself lucky if there are less than 10 syntax errors, 2 wrong counters and 4 bad pointers/names.
//all these functions are untested functions that may have some very strange cases were they acd badly or where the comments are wrong
//see them as buggy PSEUDO-code and consider yourself lucky if there are less than 2 wrong counters and 4 bad pointers/names.
//they are more an example and a concept and unlikely fully functional AS IS.
//they are more an example and a concept and unlikely fully functional AS IS.
 
string JisI(string j){if(llJsonValueType(j,[])==JSON_INTEGER)return 1llJsonGetValue(j,[]);return "";}
string JisN(string j){if(llJsonValueType(j,[])==JSON_NUMBER  )return llJsonGetValue(j,[]);return "";}
//return the JSON if "j" is a JSON_INTEGER, like j="1"  j="-2"  j="0"                 otherwise return "";
//return the JSON if "j" is a JSON_NUMBER, like j="10.1234"  j="-0.1234"  j="3.14159"  j="-1" j="123456"  otherwise return "";
//these would be string representations of an integer because j is a string.
//these would be sting representations of a float because j is a string.
 
//storing floats in a JSON will easily lose you a lot of accuracy. Integers stored in JSON may have a smaller range than 32-bit signed integers.
string JisF(string j){if(llJsonValueType(j,[])==JSON_FLOAT )return 1llJsonGetValue(j,[]);return "";}
//return the JSON if "j" is a JSON_FLOAT, like j="10.1234"  j="-0.1234" j="3.14159"  otherwise return "";
//these would be sting representations of an float because j is a string.
//storing floats in a JSON will easily lose you a lot of accuracy.
 
string JisA(string j){if(llJsonValueType(j,[])==JSON_ARRAY  )return llJsonGetValue(j,[]);return "";}
string JisA(string j){if(llJsonValueType(j,[])==JSON_ARRAY  )return llJsonGetValue(j,[]);return "";}
//return the JSON if "j" is a JSON_ARRAY, like j="[]"  j="[1,2]"  j="[[1],[3,4]]"     otherwise return "";
//return the JSON if "j" is a JSON_ARRAY, like j="[]"  j="[1,2]"  j="[[1],[3,4]]"                       otherwise return "";
//these would be string representations of a (nested) list because j is a string.
//these would be string representations of a (nested) list because j is a string.
 
string JisS(string j){if(llJsonValueType(j,[])==JSON_STRING )return llJsonGetValue(j,[]);return "";}
string JisS(string j){if(llJsonValueType(j,[])==JSON_STRING )return llJsonGetValue(j,[]);return "";}
//return the string that is stored in "j" if "j" is a JSON_STRING, like j="\"PI\""  j="\"3.14\""  j="\"-1\"" otherwise return "";
//return the string that is stored in "j" if "j" is a JSON_STRING, like j="\"PI\""  j="\"3.14\""  j="\"-1\"" otherwise return "";
Line 35: Line 31:
//because j is a string that is also able to store numbers and floats and (nested) lists ... as strings.  
//because j is a string that is also able to store numbers and floats and (nested) lists ... as strings.  
//this returns the string within "j" if "j" is "A string within a string".  
//this returns the string within "j" if "j" is "A string within a string".  
 
/*the above functions return strings to be used in simple  
/*the above functions return strings to be used in simple  
if(JisN(j)){}
if(JisA(j)){}  
if(JisA(j)){}  
if(JisS(j)){}  
if(JisS(j)){}  
Line 42: Line 39:
The functions MAY return a JSON_STRING or JSON_ARRAY can be useful recursively as shown below:
The functions MAY return a JSON_STRING or JSON_ARRAY can be useful recursively as shown below:
*/
*/
 
JstringPARSED(string j){
JstringPARSED(string j){
     //j="\"PI\"";
     //j="\"PI\"";
Line 48: Line 45:
     //j="\"-1\"";
     //j="\"-1\"";
     string JayWeHaveToGoDeeperS = JisS(JisS(j));
     string JayWeHaveToGoDeeperS = JisS(JisS(j));
     llOwnerSay( JayWeHaveToGoDeeperS ); // says "PI", or "0" , or "0", because "3.14" is a JSON_FLOAT and "-1" is a JSON_INTEGER.
     llOwnerSay( JayWeHaveToGoDeeperS ); // says "PI", or ""       , or ""         , because "3.14" is a JSON_NUMBER and "-1" is a JSON_NUMBER but not a JSON_STRING
     string JayWeHaveToGoDeeperI = JisI(JisS(j));
     string JayWeHaveToGoDeeperN = JisN(JisS(j));
     llOwnerSay( JayWeHaveToGoDeeperI ); // says "0", or "0" , or "-1", because "3.14" is a JSON_FLOAT and "PI" is a JSON_STRING.
     llOwnerSay( JayWeHaveToGoDeeperN ); // says "" , or "3.14000" , or "-1.0000",   because "PI" is a JSON_STRING but "PI" is not a JSON_NUMBER
    string JayWeHaveToGoDeeperF = JisF(JisS(j));
    llOwnerSay( JayWeHaveToGoDeeperI ); // says "0", or "3.14" , or "0", because "3.14" is a JSON_FLOAT and "-1" is a JSON_INTEGER.


     if (llJsonValueType(j,[])==JSON_INVALID){}//would be TRUE for j="";
     if (llJsonValueType(j,[])==JSON_INVALID){}//would be TRUE for j="";
}
}
 
//==== End of JstringPARSED(j) ===== start of Jnested(j) for  nested/recursive lists =====
//==== End of JstringPARSED(j) ===== start of Jnested(j) for  nested/recursive lists =====
 
Jnested(string j){
Jnested(string j){
JisA(JisA(string j){
     //j="[]";
     //j="[]";
     //j="[[1],[2]]";
     //j="[[1],[2]]";
Line 67: Line 62:
     //j="[[-3,-4]]";  //the only true case HERE for = JisA(JisA(j)); below
     //j="[[-3,-4]]";  //the only true case HERE for = JisA(JisA(j)); below
     string JayWeHaveToGoDeeperA = JisA(JisA(j));
     string JayWeHaveToGoDeeperA = JisA(JisA(j));
     llOwnerSay("you stored a whole list nested as only entry of another list, that was dumb:" JayWeHaveToGoDeeperA );  
     llOwnerSay("you stored a whole list nested as only entry of another list, that was dumb:"+JayWeHaveToGoDeeperA );  
     //just in case your json is poorly formatted to store a whole list within another whole list as its only entry..
     //just in case your json is poorly formatted to store a whole list within another whole list as its only entry..
 
     string subJ=JisA(j);
     string subJ=JisA(j);
     if (subJ){
     if (subJ){
         //here you actually have to loop trough each entry of the subJ JSON_LIST to test its TYPE for each entry (if we want to go deeper recursively)
         //here you actually have to loop trough each entry of the subJ JSON_LIST to test its TYPE for each entry (if we want to go deeper recursively)
         //You can go deeper once you know if entry number x is a JSON_LIST (NESTED!!!) or JSON_STRING (for some reason)
         //You can go deeper once you know if entry number x is a JSON_LIST (NESTED!!!) or JSON_STRING (for some reason)
         first see how many list entries there are; we go trough entries of subJ starting on the left until its a JSON_INVALID
         //first see how many list entries there are; we go trough entries of subJ starting on the left until its a JSON_INVALID
         list types;  
         list types;  
         integer i;
         integer i;
Line 80: Line 75:
             string type=llJsonValueType(j,[i]);//stores the TYPE of "j"'s entry at position i as "type"
             string type=llJsonValueType(j,[i]);//stores the TYPE of "j"'s entry at position i as "type"
             if (type==JSON_INVALID) i=-1; //this exits this the while-loop
             if (type==JSON_INVALID) i=-1; //this exits this the while-loop
             else i=i+[type]//the list "types" will store all the JSON types, untill the first invalid one is reached. (which which should also be for "out of bounds"
             else types=types+[type];//the list "types" will store all the JSON types, untill the first invalid one is reached. (which which should also be for "out of bounds"
         }
         }
 
         //this is split in 2 loops to explain 1 task by splitting it in 2 smaller tasks. Actually the loops above and below could be merged easily.  
         //this is split in 2 loops to explain 1 task by splitting it in 2 smaller tasks. Actually the loops above and below could be merged easily.  
 
         integer i=llListLength(types);//we now have a list that lists all JSON types of j; lets see how long it is;  
         i=llGetListLength(types);//we now have a list that lists all JSON types of j; lets see how long it is;  
         //and we loop trough all types, calling THIS function recursively if the list contains another nested list in it at position i
         //and we loop trough all types, calling THIS function recursively if the list contains another nested list in it at position i
         while (i--){//i like to go reverse from left to right when ever possible, its faster and costs less memory in sls
         while (i--){//i like to go reverse from left to right when ever possible, its faster and costs less memory in sls
             if llList2String(types,i)==JSON_ARRAY){
             if (llList2String(types,i)==JSON_ARRAY){
                   nestedlisttest(llJsonGetValue(j,[i]));//the function calls itself here, recursively...
                   Jnested(llJsonGetValue(j,[i]));//the function calls itself here, recursively...
                   //but only sends the i#th SUB.list of itself trough an instance of itself.
                   //but only sends the i#th SUB.list of itself trough an instance of itself.
             }
             }
Line 99: Line 94:
         }
         }
     }
     }
 
}
}
//Jnested(j) should be useful to read from large matrices (as in 300x300 fields) that may even contain complex numbers at some places.
//Jnested(j) should be useful to read from large matrices (as in 300x300 fields) that may even contain complex numbers at some places.
Line 107: Line 102:


</lsl>
</lsl>
|caveats=
|caveats=
|helpers
|helpers

Revision as of 17:35, 10 September 2014

{{LSL Function |inject-2= |func_id=|func_sleep=0.0|func_energy=0.0 |func=llJsonValueType|return_type=string |p1_type=string|p1_name=json|p1_desc=A string serialization of a json object. |p2_type=list|p2_name=specifiers|p2_desc=A path to a value in the json parameter. |func_footnote |func_desc=Gets the JSON type for the value in json at the location specifiers. |Return_text=specifying the type of the value at specifiers in json. |spec=See Json_usage_in_LSL |constants=

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;
JSON_NUMBER U+FDD3 64979 "%EF%B7%93" &#xFDD3;
JSON_STRING U+FDD4 64980 "%EF%B7%94" &#xFDD4;
JSON_NULL U+FDD5 64981 "%EF%B7%95" &#xFDD5;
JSON_TRUE U+FDD6 64982 "%EF%B7%96" &#xFDD6;
JSON_FALSE U+FDD7 64983 "%EF%B7%97" &#xFDD7;
JSON_DELETE U+FDD8 64984 "%EF%B7%98" &#xFDD8; Used with llJsonSetValue to remove a key-value pair.

|examples=<lsl>


//all these functions are untested functions that may have some very strange cases were they acd badly or where the comments are wrong //see them as buggy PSEUDO-code and consider yourself lucky if there are less than 2 wrong counters and 4 bad pointers/names. //they are more an example and a concept and unlikely fully functional AS IS.

string JisN(string j){if(llJsonValueType(j,[])==JSON_NUMBER )return llJsonGetValue(j,[]);return "";} //return the JSON if "j" is a JSON_NUMBER, like j="10.1234" j="-0.1234" j="3.14159" j="-1" j="123456" otherwise return ""; //these would be sting representations of a float because j is a string. //storing floats in a JSON will easily lose you a lot of accuracy. Integers stored in JSON may have a smaller range than 32-bit signed integers.

string JisA(string j){if(llJsonValueType(j,[])==JSON_ARRAY )return llJsonGetValue(j,[]);return "";} //return the JSON if "j" is a JSON_ARRAY, like j="[]" j="[1,2]" j="[[1],[3,4]]" otherwise return ""; //these would be string representations of a (nested) list because j is a string.

string JisS(string j){if(llJsonValueType(j,[])==JSON_STRING )return llJsonGetValue(j,[]);return "";} //return the string that is stored in "j" if "j" is a JSON_STRING, like j="\"PI\"" j="\"3.14\"" j="\"-1\"" otherwise return ""; //these would be string representations of a string, typecast with \" as that within itself, //because j is a string that is also able to store numbers and floats and (nested) lists ... as strings. //this returns the string within "j" if "j" is "A string within a string".

/*the above functions return strings to be used in simple if(JisN(j)){} if(JisA(j)){} if(JisS(j)){} //conditions as if they are booleans. //the boolean-ish condition works because "" equals FALSE within sls. The functions MAY return a JSON_STRING or JSON_ARRAY can be useful recursively as shown below:

  • /

JstringPARSED(string j){

   //j="\"PI\"";
   //j="\"3.14\"";
   //j="\"-1\"";
   string JayWeHaveToGoDeeperS = JisS(JisS(j));
   llOwnerSay( JayWeHaveToGoDeeperS ); // says "PI", or ""        , or ""         , because "3.14" is a JSON_NUMBER and "-1" is a JSON_NUMBER but not a JSON_STRING
   string JayWeHaveToGoDeeperN = JisN(JisS(j));
   llOwnerSay( JayWeHaveToGoDeeperN ); // says ""  , or "3.14000" , or "-1.0000",   because "PI" is a JSON_STRING but "PI" is not a JSON_NUMBER 


   if (llJsonValueType(j,[])==JSON_INVALID){}//would be TRUE for j="";

}

//==== End of JstringPARSED(j) ===== start of Jnested(j) for nested/recursive lists =====

Jnested(string j){

   //j="[]";
   //j="[[1],[2]]";
   //j="[-1,[2]]";
   //j="[[1],-2]";
   //j="-3,-4";  //the only true case HERE for = JisA(JisA(j)); below
   string JayWeHaveToGoDeeperA = JisA(JisA(j));
   llOwnerSay("you stored a whole list nested as only entry of another list, that was dumb:"+JayWeHaveToGoDeeperA ); 
   //just in case your json is poorly formatted to store a whole list within another whole list as its only entry..

   string subJ=JisA(j);
   if (subJ){
       //here you actually have to loop trough each entry of the subJ JSON_LIST to test its TYPE for each entry (if we want to go deeper recursively)
       //You can go deeper once you know if entry number x is a JSON_LIST (NESTED!!!) or JSON_STRING (for some reason)
       //first see how many list entries there are; we go trough entries of subJ starting on the left until its a JSON_INVALID
       list types; 
       integer i;
       while (++i){
            string type=llJsonValueType(j,[i]);//stores the TYPE of "j"'s entry at position i as "type"
            if (type==JSON_INVALID) i=-1; //this exits this the while-loop
            else types=types+[type];//the list "types" will store all the JSON types, untill the first invalid one is reached. (which which should also be for "out of bounds"
       }

       //this is split in 2 loops to explain 1 task by splitting it in 2 smaller tasks. Actually the loops above and below could be merged easily. 

       i=llGetListLength(types);//we now have a list that lists all JSON types of j; lets see how long it is; 
       //and we loop trough all types, calling THIS function recursively if the list contains another nested list in it at position i
       while (i--){//i like to go reverse from left to right when ever possible, its faster and costs less memory in sls
            if (llList2String(types,i)==JSON_ARRAY){
                 Jnested(llJsonGetValue(j,[i]));//the function calls itself here, recursively...
                 //but only sends the i#th SUB.list of itself trough an instance of itself.
           }
           else{
               //read from your list what json type is at this position and do something with it.
               //maybe you need a global counter that increases each time you go "one nested list deeper" and decreases when you go "one nested list back up"
               //otherwise you may not know how deep you are within nested lists.
           }
       }
   }

} //Jnested(j) should be useful to read from large matrices (as in 300x300 fields) that may even contain complex numbers at some places. //i tried to read very long JSONs with llGetSubString(), but llGetSubString() too easily stack-heap collides on very long strings, //so you have to go trough the nested list using Jnested(j) to read and print it per entry.


</lsl> |caveats= |helpers |also_functions=|-style="vertical-align:top;" | style="color:gray;" |•  | llList2Json | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJson2List | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJsonSetValue | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJsonGetValue | style="color:gray;" | | | |also_events |also_tests |also_articles=|-style="vertical-align:top;" | style="color:gray;" |•  | Typecast | style="color:gray;" | | | |notes= |permission |negative_index |cat1=List |cat2=String |cat3=Data Conversion |cat4=JSON |history = Date of Release 20/05/2013 }}