About

Decodes a string of key-value pairs from data and returns a JSON string - either a JSON_OBJECT or a JSON_ARRAY.

Contrast to Linden Functions

Although for different inputs, the conversion behaviour of the wasKeyValueToJSON function differs from llList2Json (a function meant to convert an LSL list to a JSON string) in that wasKeyValueToJSON assumes that all numeric values are numerals rather than strings. In other words, suppose you have a list l and you use llList2Json to convert the list to JSON (either JSON_ARRAY or JSON_OBJECT:

list l = [  "a", 0.32, "b", "2" ];
llOwnerSay(llList2Json(JSON_ARRAY, l));

then the output will be:

{"a":0.320000,"b":"2"}

with the value 2 being of string type. However, key-value pairs do not have the structure to hold information about types (although some structure could be built-in), such that converting some key-value pair data (in the following example, stored in kvp) to JSON using wasKeyValueToJSON:

string kvp = "a=0.32&b=2";
llOwnerSay(wasKeyValueToJSON(kvp, JSON_ARRAY));

will result in the following output:

{"a":0.32,"b":2}

with 2 being a number of integer type.

In brief, wasKeyValueToJSON assumes that any numeric value in a key-value data string is a number (either a float or an integer). Note that wasKeyValueToJSON respects float and integer types without mixing the two: in the previous example, 0.32 is correctly of type float and 2 of type integer when interpreted by a JSON parser.

Example Usage

When the function is called with:

wasKeyValueToJSON("position=<1,1,0>&toggle=on", JSON_ARRAY);

it will return the string:

["position","<1,1,0>","toggle","on"]

which is equivalent with converting the list ["position",<1,1,0>,"toggle","on"] with llList2Json to a string.

wasKeyValueToJSON supports both JSON_OBJECT and JSON_ARRAY.

Code

This script was tested and works on OpenSim version 0.7.4!

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueToJSON(string kvp, string T) {
    list data = llParseString2List(kvp, ["&", "="], []);
    list temp = [];
    do {
        string k = llList2String(data, 0);
        string v = llList2String(data, 1);
        string o = "";
        o += "\"" + k + "\"";
        if(T == JSON_ARRAY) {
            o += ",";
            jump type;
        }
        o += ":";
@type;
        if((float)v != 0 || v == "0") {
            o += v;
            jump dump;
        }
        o += "\"" + v + "\"";
@dump;
        temp += o;
        data = llDeleteSubList(data, 0, 1);
    } while(llGetListLength(data) != 0);
    if(T == JSON_ARRAY)
        return "[" + llDumpList2String(temp, ",") + "]";
    if(T == JSON_OBJECT)
        return "{" + llDumpList2String(temp, ",") + "}";
    return JSON_INVALID;
}

fuss/data_structures/key-value_pairs/json/to.txt ยท Last modified: 2019/01/03 01:38 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy, data protection and copyright compliance.