• Do not register here on develop.twiki.org, login with your twiki.org account.
• Use View topic Item7848 for generic doc work for TWiki-6.1.1. Use View topic Item7851 for doc work on extensions that are not part of a release. More... Close
• Anything you create or change in standard webs (Main, TWiki, Sandbox etc) will be automatically reverted on every SVN update.
Does this site look broken?. Use the LitterTray web for test cases.

Item7611: SET and GET with support for JSON objects and JSON path

Item Form Data

AppliesTo: Component: Priority: CurrentState: WaitingFor: TargetRelease ReleasedIn
Extension SetGetPlugin Enhancement Closed   patch 6.0.2

Edit Form Data

Summary:
Reported By:
Codebase:
Applies To:
Component:
Priority:
Current State:
Waiting For:
Target Release:
Released In:
 

Detail

This is an enhancement of the SetGetPlugin that allows using JSON syntax to set and get variables. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language. JSON is explained at http://www.json.org/

Test cases:

  • %SET{ "normal" value="normal value" }% MOVED TO...
    /normal value/
  • %SET{ value="missing name" }% MOVED TO...
    //
  • not existing variable MOVED TO...
    //

  • %SET{ json_string = "json string value" }% MOVED TO...
    /json string value/
  • %SET{ json_n_string = "json string
      new line" }%
    MOVED TO...
    /json string
         new line/

  • %SET{ array = [ 1, 2, 3 ] }% MOVED TO...
    /[1,2,3]/
  • %SET{ json_array = [ "a", "b", "c" ] }% MOVED TO...
    /["a","b","c"]/
    • %GET{ json_array[1] }% MOVED TO...
      /b/
    • %SET{ json_array[4] = "d" }% MOVED TO... - set value using JSON path
    • %GET{ json_array }% MOVED TO...
      /["a","b","c",null,"d"]/

  • %SET{ new_array[1] = "x" }% MOVED TO... - set new array using JSON path
    • %GET{ new_array }% MOVED TO...
      /[null,"x"]/
    • %SET{ new_array[2] = "y" }% MOVED TO... - set new array using JSON path
    • %GET{ new_array }% MOVED TO...
      /[null,"x","y"]/

  • %GET{ persistent_array }% MOVED TO...
    /["a","b","c","d"]/
    • %SET{ persistent_array[1] = "x" remember="1" }% MOVED TO... - set new persistent array using JSON path
    • %GET{ persistent_array }% MOVED TO...
      /["a","x","c","d"]/
    • %SET{ persistent_array[2] = "y" remember="1" }% MOVED TO... - set new persistent array using JSON path
    • %GET{ persistent_array }% MOVED TO...
      /["a","x","y","d"]/
    • %SET{ persistent_array = [ "a", "b", "c", "d" ] remember="1" }% MOVED TO... - set new persistent array using JSON path
    • %GET{ persistent_array }% MOVED TO...
      /["a","b","c","d"]/

  • %SET{ arrayOfArray = [ [ "a", "b", "c" ], [ "d", "e", "f" ], [ "g", "h", "i" ] ] }% MOVED TO...
    /[["a","b","c"],["d","e","f"],["g","h","i"]]/
    • %GET{ arrayOfArray[2] }% MOVED TO...
      /["g","h","i"]/
    • %GET{ arrayOfArray[2][1] }% MOVED TO...
      /h/

  • %SET{ json_object = { "a": "A", "b": "B", "c": "C" } }% MOVED TO...
    /{"c":"C","a":"A","b":"B"}/
    • %GET{ json_object.b }% MOVED TO...
      /B/
    • %SET{ json_object.d = "D" }% MOVED TO... - set value using JSON path
    • %GET{ json_object }% MOVED TO...
      /{"c":"C","a":"A","b":"B","d":"D"}/

  • %SET{ new_object.x = "X" }% MOVED TO... - set new object using JSON path
    • %GET{ new_object }% MOVED TO...
      /{"x":"X"}/
    • %SET{ new_object.y = "Y" }% MOVED TO... - set new object using JSON path
    • %GET{ new_object }% MOVED TO...
      /{"y":"Y","x":"X"}/

  • %SET{ arrayOfObj = [ { "a": "A", "b": "B", "c": "C" }, { "d": "D", "e": "E", "f": "F" }, { "g": "G", "h": "H", "i": "I" } ] }% MOVED TO...
    /[{"c":"C","a":"A","b":"B"},{"e":"E","d":"D","f":"F"},{"h":"H","g":"G","i":"I"}]/
    • %GET{ arrayOfObj[2] }% MOVED TO...
      /{"h":"H","g":"G","i":"I"}/
    • %GET{ arrayOfObj[2].h }% MOVED TO...
      /H/
    • %GET{ arrayOfObj[2].x }% MOVED TO...
      /ERROR: Invalid JSON path, syntax or range./
    • %GET{ arrayOfObj }% MOVED TO...
      /[{"c":"C","a":"A","b":"B"},{"e":"E","d":"D","f":"F"},{"h":"H","g":"G","i":"I"}]/
    • %SET{ arrayOfObj[2].x = "X" }% MOVED TO... - set deep value using JSON path
    • %GET{ arrayOfObj }% MOVED TO...
      /[{"c":"C","a":"A","b":"B"},{"e":"E","d":"D","f":"F"},{"h":"H","g":"G","x":"X","i":"I"}]/

  • %SET{ menu = { "File": { "New": [ "new", "F" ], "Open": [ "open", "F" ] }, "Edit": { "Copy": [ "cpy", "F" ], "Paste": [ "pst", "F" ] } } }% MOVED TO...
    /{"File":{"New":["new","F"],"Open":["open","F"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}/
    • %SET{ menu.File.Open[1] = "T" }% MOVED TO... - set deep value using JSON path
    • %GET{ menu }% MOVED TO...
      /{"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}/
    • %SET{ menu.Edit.Cut = [ "cut", "T" ] }% MOVED TO... - set deep value using JSON path
    • %GET{ menu }% MOVED TO...
      /{"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Cut":["cut","T"],"Paste":["pst","F"]}}/

  • %SET{ complex = {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, true,  false]} }% MOVED TO...
    /{"array":[1,11,234,-5,100000,10000000,true,false],"params":["user1","we were just talking"],"id":null,"method":"handleMessage"}/
    • %GET{ complex.array[3] }% MOVED TO...
      /-5/
    • %GET{ complex.params[1] }% MOVED TO...
      /we were just talking/

  • %SET{ complexWidget = {"widget": {
        "debug": "on",
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        },
        "image": { 
            "src": "Images/Sun.png",
            "name": "sun1",
            "hOffset": 250,
            "vOffset": 250,
            "alignment": "center"
        },
        "text": {
            "data": "Click Here",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
      }} }%
    MOVED TO...
    /{"widget":{"window":{"width":500,"name":"main_window","title":"Sample Konfabulator Widget","height":500},"text":{"hOffset":250,"vOffset":100,"name":"text1","style":"bold","data":"Click Here","size":36,"onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;","alignment":"center"},"debug":"on","image":{"hOffset":250,"src":"Images/Sun.png","alignment":"center","vOffset":250,"name":"sun1"}}}/
    • %GET{ complexWidget.widget.window }% MOVED TO...
      /{"width":500,"name":"main_window","title":"Sample Konfabulator Widget","height":500}/
    • %GET{ complexWidget.widget.window.title }% MOVED TO...
      /Sample Konfabulator Widget/

  • %SET{ error = [ 1, 2, } ] }% MOVED TO...
    /ERROR: malformed JSON string, neither array, object, number, string or atom, at character offset 8 (before "} ]")/

-- TWiki:Main/PeterThoeny - 2015-02-05

Edit | Attach | Watch | Print version | History: r40 < r39 < r38 < r37 < r36 | Backlinks | Raw View |  Raw edit | More topic actions
Topic revision: r40 - 2016-01-22 - PeterThoeny
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback