Difference between revisions of "LlJsonSetValue/ja"

From Second Life Wiki
Jump to navigation Jump to search
m
m
 
Line 5: Line 5:
|func=llJsonSetValue
|func=llJsonSetValue
|return_type=string
|return_type=string
|p1_type=string|p1_name=json|p1_desc=source JSON data
|p1_type=string|p1_name=json|p1_desc=元のJSONデータ
|p2_type=list|p2_name=specifiers|p2_desc=location of the of the value to be added, updated or deleted.
|p2_type=list|p2_name=specifiers|p2_desc=追加、更新、または削除する値の場所
|p3_type=string|p3_name=value|p3_desc=new value or [[JSON_DELETE]] flag.|p3_hover=new value or JSON_DELETE flag.
|p3_type=string|p3_name=value|p3_desc=新しい値または[[JSON_DELETE]]フラグ。|p3_hover=新しい値またはJSON_DELETEフラグ。
|func_footnote
|func_footnote
|func_desc=Returns, if successful, a new [http://json.org JSON] text ''string'' which is {{LSLPT|json}} with the value indicated by the {{LSLPT|specifiers}} list set to {{LSLPT|value}}.
|func_desc=成功した場合、指定された{{LSLPT|specifiers}}リストで示される場所の値が{{LSLPT|value}}に設定された新しい[http://json.org JSON]テキスト ''string'' を返します。


If unsuccessful (usually because of specifying an out of bounds array index) it returns [[JSON_INVALID]].
失敗した場合(通常は範囲外の配列インデックスが指定されたため)、[[JSON_INVALID]]を返します。


An "out of bounds array index" is defined to be any Integer {{LSLPT|specifiers}} greater than the length of an existing array at that level within the Json text or greater than 0 (zero) at a level an array doesn't exist.
「範囲外の配列インデックス」とは、Jsonテキスト内のそのレベルにおける既存の配列の長さを超える任意の整数{{LSLPT|specifiers}}、または配列が存在しないレベルにおいて0(ゼロ)より大きい整数{{LSLPT|specifiers}}を定義しています。


A special {{LSLPT|specifiers}}, [[JSON_APPEND]], is accepted which appends the {{LSLPT|value}} to the end of the array at the {{LSLPT|specifiers}} level. Care should be taken- if that level is not an array, the existing Value there will be overwritten and replaced with an array containing {{LSLPT|value}} at it's first (0) index.
特別な{{LSLPT|specifiers}}である[[JSON_APPEND]]は、{{LSLPT|specifiers}}のレベルの配列の末尾に{{LSLPT|value}}を追加します。注意が必要です - そのレベルが配列でない場合、既存の値は上書きされ、{{LSLPT|value}}を含む配列がその最初(0)のインデックスに置き換えられます。


Contrary to [[list]]s and [[string]]s, negative indexing of Json arrays is '''not''' supported.
[[list]][[string]]とは異なり、Json配列の負のインデックスはサポートされていません。


If an existing "Key" is {{LSLPT|specifiers}} at that level, its Value will be overwritten by {{LSLPT|value}} unless {{LSLPT|value}} is the magic value [[JSON_DELETE]]. If a value does not exist at {{LSLPT|specifiers}}, a new Key:Value pair will be formed within the Json object.
そのレベルで既存の「Key」が{{LSLPT|specifiers}}にある場合、その値は{{LSLPT|value}}によって上書きされますが、{{LSLPT|value}}が魔法の値[[JSON_DELETE]]の場合は上書きされません。{{LSLPT|specifiers}}に値が存在しない場合、新しいKey:ValueペアがJsonオブジェクト内に形成されます。


To delete an existing value at {{LSLPT|specifiers}}, use [[JSON_DELETE]] as the {{LSLPT|value}}. Note it will not prune empty objects or arrays at higher levels.
{{LSLPT|specifiers}}で既存の値を削除するには、{{LSLPT|value}}として[[JSON_DELETE]]を使用します。これにより、上位のレベルで空のオブジェクトまたは配列は削除されません。


If {{LSLPT|value}} is [[JSON_TRUE]], [[JSON_FALSE]] or [[JSON_NULL]], the Value set will be the bare words 'true', 'false' or 'null', respectively, at the {{LSLPT|specifiers}} location within {{LSLPT|json}}.
もし{{LSLPT|value}}[[JSON_TRUE]][[JSON_FALSE]]、または[[JSON_NULL]]である場合、設定される値はそれぞれ、{{LSLPT|json}}内の{{LSLPT|specifiers}}の場所に 'true'、'false'、または 'null' となります。
|return_text=
|return_text=
|spec=See [[Json_usage_in_LSL|Json usage in LSL]]
|spec=See [[Json_usage_in_LSL|Json usage in LSL]]
Line 208: Line 208:
}
}
</syntaxhighlight>
</syntaxhighlight>
|caveats={{KBwarning|The below comment in regards to speed is unverified on modern simulator versions, and thus cannot be assumed to be true. '''Always test execution speed claims for yourself.'''}}
|caveats={{KBwarning|速度に関する以下のコメントは、現代のシミュレータバージョンでは検証されておらず、したがって真実とは仮定できません。必ず自分で実行速度の主張をテストしてください。}}
*[[llListReplaceList|llListReplaceList()]] is roughly 2.8x as fast in replacing a single value of a list than [[llJsonSetValue|llJsonSetValue()]] is to replace a single value in a json.
*[[llListReplaceList|llListReplaceList()]]は、リストの単一の値を置換する際に、[[llJsonSetValue|llJsonSetValue()]]よりも約2.8倍速いです。
:The length of the list/json are irrelevant here.
:ここでは、リストまたはJSONの長さは関係ありません。


|helpers
|helpers

Latest revision as of 13:08, 22 November 2023

要約

関数: string llJsonSetValue( string json, list specifiers, string value );

成功した場合、指定されたspecifiersリストで示される場所の値がvalueに設定された新しいJSONテキスト string を返します。

失敗した場合(通常は範囲外の配列インデックスが指定されたため)、JSON_INVALIDを返します。

「範囲外の配列インデックス」とは、Jsonテキスト内のそのレベルにおける既存の配列の長さを超える任意の整数specifiers、または配列が存在しないレベルにおいて0(ゼロ)より大きい整数specifiersを定義しています。

特別なspecifiersであるJSON_APPENDは、specifiersのレベルの配列の末尾にvalueを追加します。注意が必要です - そのレベルが配列でない場合、既存の値は上書きされ、valueを含む配列がその最初(0)のインデックスに置き換えられます。

liststringとは異なり、Json配列の負のインデックスはサポートされていません。

そのレベルで既存の「Key」がspecifiersにある場合、その値はvalueによって上書きされますが、valueが魔法の値JSON_DELETEの場合は上書きされません。specifiersに値が存在しない場合、新しいKey:ValueペアがJsonオブジェクト内に形成されます。

specifiersで既存の値を削除するには、valueとしてJSON_DELETEを使用します。これにより、上位のレベルで空のオブジェクトまたは配列は削除されません。

もしvalueJSON_TRUEJSON_FALSE、またはJSON_NULLである場合、設定される値はそれぞれ、json内のspecifiersの場所に 'true'、'false'、または 'null' となります。
string で返します。

• string json 元のJSONデータ
• list specifiers 追加、更新、または削除する値の場所
• string value 新しい値またはJSON_DELETEフラグ。

specifiers does not support negative indexes.

仕様

警告

KBwarning.png 警告: 速度に関する以下のコメントは、現代のシミュレータバージョンでは検証されておらず、したがって真実とは仮定できません。必ず自分で実行速度の主張をテストしてください。
ここでは、リストまたはJSONの長さは関係ありません。
All Issues ~ Search JIRA for related Bugs

サンプル

string TEST_STRING_JSON;

init()
{
    TEST_STRING_JSON = "[9,\"<1,1,1>\",false,{\"A\":8,\"Z\":9}]";

//  [9,"<1,1,1>",false,{"A":8,"Z":9}]
    say("Original TEST_STRING_JSON: " + TEST_STRING_JSON);
}

run_json_test(string input)
{
    string output;

//  changing values within the json string

//  change the first value in the array to 10
    output = llJsonSetValue(input, [0], "10");

//  [10,"<1,1,1>",false,{"A":8,"Z":9}]
    say("( 1): " + output);

//  change the third value in the array to 'true'
    output = llJsonSetValue(input, [2], JSON_TRUE);

//  [9,"<1,1,1>",true,{"A":8,"Z":9}]
    say("( 2): " + output);

//  change the value of "A" within the Json object to 3
    output = llJsonSetValue(input, [3, "A"], "3");

//  [9,"<1,1,1>",false,{"A":3,"Z":9}]
    say("( 3): " + output);

//  adding a value or new key-value-pair within the input

//  add the value "Hello" to the end of the array
//      NOTE: One cannot insert, only add to the end
    output = llJsonSetValue(input, [JSON_APPEND], "Hello");

//  [9,"<1,1,1>",false,{"A":8,"Z":9},"Hello"]
    say("( 4): " + output);

//  add the key-value-pair "B":10 to the object
    output = llJsonSetValue(input, [3, "B"], "10");

//  [9,"<1,1,1>",false,{"A":8,"B":10,"Z":9}]
    say("( 5): " + output);


//  Things to look out for when modifying Json text
//      ~!~ Be careful when using this function ~!~

//  out of bounds array assignment:
//      defined as attempting to add a value to a position ...
//      ...greater than the length of the array (which may be 0)
//      JSON_APPEND is ALWAYS the preferred way to add to an array
    output = llJsonSetValue(input, [5], "10");

//  %EF%B7%90 (URL escaped JSON_INVALID)
    say("( 6): " + llEscapeURL(output));

//  BUT, this works, since it is in bounds
//      (eqivalent to JSON_APPEND in this case)
    output = llJsonSetValue(input, [4], "10");

//  [9,"<1,1,1>",false,{"A":8,"Z":9},10]
    say("( 7): " + output);

//  careless formation of new arrays
//      ( the 4 and all subsequent 0's are all in bounds.)
    output = llJsonSetValue(input, [4, 0, 0, 0], "10");

//  [9,"<1,1,1>",false,{"A":8,"Z":9},[[[10]]]]
    say("( 8): " + output);

//  overwriting an object with an array:
//      ~!~ mistaken use of JSON_APPEND on an object ~!~
    output = llJsonSetValue(input, [3, JSON_APPEND], "10");

//  [9,"<1,1,1>",false,[10]]
    say("( 9): " + output);

//  careless formation of new objects
//      NOTE: "Key" assignemts will NEVER result in a return of JSON_INVALID!
    output = llJsonSetValue(input, [3, "W", "X"], "10");

//  [9,"<1,1,1>",false,{"A":8,"W":{"X":10},"Z":9}]
    say("(10): " + output);

    output = llJsonSetValue(input, [3, "W", "X", "Y"], "10");

//  [9,"<1,1,1>",false,{"A":8,"W":{"X":{"Y":10}},"Z":9}]
    say("(11): " + output);

//  overwriting an array with an object
    output = llJsonSetValue(input, ["X"], "10");

//  {"X":10}
    say("(12): " + output);

//  special case considerations:


//  BUG-3692: (NOTE: Corrected in release 13.09.21.281328!)
//      a bug where, instead of JSON_INVALID being returned, if the out of
//      bounds index is at a lower level than the topmost (root) level, a
//      non-compliant JSON text would be formed
    output = llJsonSetValue(input, [1, 7], "Disappearing Text");

//  Note the "empty" second position that resulted in the returned array
//  [9,,false,{"A":8,"Z":9}]
// (But now correctly shows JSON_INVALID)
    say("(13): " + output);

//  though there is no way to directly delete a key-value-pair 
//  nor remove a value from an array,
//  the use of JSON_NULL may prove adequate
    output = llJsonSetValue(input, [3, "A"], JSON_NULL);

//  [9,"<1,1,1>",false,{"A":null,"Z":9}]
    say("(14): " + output);

//  if a JSON text object has been formed with llList2Json()
//  that contains one or more duplicated "Keys", (allowable
//  but NOT recommended!) ANY change
//  made to that object will correct the condition,
//  with all but the last such "Key" being removed
    output = llList2Json(JSON_OBJECT, ["A", 1, "A", 2, "A", 3, "B", 4, "B", 4]);

//  both Keys "A" and "B" are duplicated
//  {"A":1,"A":2,"A":3,"B":4,"B":4}
    say("(15): " + output);

//  only the last value of the duplications is accessable though

//  3
    say("(16): " + llJsonGetValue(output, ["A"]));

//  condition corrected by adding a key-value-pair...

//  {"A":3,"B":4,"Z":5}
    say("(17): " + llJsonSetValue(output, ["Z"], "5"));

//  ... or by changing a value

// {"A":5,"B":4}
    say("(18): " + llJsonSetValue(output, ["A"], "5"));
}

say(string message)
{
    llOwnerSay(message);
//  llRegionSayTo(llGetOwner(), PUBLIC_CHANNEL, message);
//  llWhisper(PUBLIC_CHANNEL, message);
}

default
{
    on_rez(integer start_param)
    {
        llResetScript();
    }

    state_entry()
    {
        init();
    }

    touch_end(integer num_detected)
    {
//      copy 'TEST_STRING_JSON' from the following function call
//      to the string 'input' in the function declaration
//      and run a test on 'input' to not (!) modify 'TEST_STRING_JSON'
//      but its copy instead
        run_json_test(TEST_STRING_JSON);
    }
}

関連項目

関数

•  llList2Json
•  llJson2List
•  llJsonGetValue
•  llJsonValueType

記事

•  Typecast

特記事項

経緯

Date of Release 20/05/2013

All Issues

~ Search JIRA for related Issues
   JSON_NULL may be deceptively returned instead of JSON_INVALID when noncompliant Json text is encountered by either llJsonValueType or llJsonGetValue. Fixed with release 13.09.21.281328.

Signature

function string llJsonSetValue( string json, list specifiers, string value );
この翻訳は 原文 と比べて古いですか?間違いがありますか?読みにくいですか?みんなで 修正 していきましょう! (手順はこちら)
この項目はあなたにとって参考にならない項目ですか?もしかしたらLSL Wikiの関連した項目が参考になるかもしれません。