Difference between revisions of "ObjectType:jsonvalue"

From Lavish Software Wiki
Jump to navigation Jump to search
Line 13: Line 13:
 
== Methods ==
 
== Methods ==
 
none.
 
none.
 +
 +
== Examples ==
 +
; A walk-through of JSON usage
 +
function main()
 +
{
 +
    ; initialize a json Object. Objects are denoted by {}
 +
    ; json Objects contain "key":value pairs. The key is always a string, but any JSON value will do, including objects or arrays
 +
    variable jsonvalue joMyValue={} 
 +
 +
    ; a variable that is a jsonvalue is really a "jsonvaluecontainer" https://www.lavishsoft.com/wiki/index.php/ObjectType:jsonvaluecontainer
 +
    ; This container is holding an Object, currently empty.
 +
 +
    ; numeric types do not need any special formatting
 +
    joMyValue:Set["SomeInteger",12]
 +
    joMyValue:Set["SomeFloatingPoint",1.234]
 +
 +
    ; JSON strings are denoted by double quotes ""
 +
    ; LavishScript also uses double quotes, so the inner quotes for the JSON must be escaped.
 +
;    joMyValue:Set["SomeString","This is not valid and will produce a JSON error"]
 +
    joMyValue:Set["SomeString","\"This is valid\""]
 +
 +
    ; Do note that the inner quotes are not actually stored as part of the JSON string, so when we show the values in this echo...
 +
    echo ${joMyValue["SomeInteger"]}=12, ${joMyValue["SomeFloatingPoint"].Milli}=1.234, \"${joMyValue["SomeString"].Escape}\"=\"This is valid\"
 +
    ; ... we put our own quotes there
 +
 +
    ; You can also have arrays. Arrays are denoted by []
 +
    joMyValue:Set["SomeArray","[]"]   
 +
 +
    joMyValue["SomeArray"]:Add["\"First item!\""]
 +
    joMyValue["SomeArray"]:Add["\"Second item!\""]
 +
    echo \"${joMyValue[SomeArray].Get[1]}\"=\"First item!\" \"${joMyValue[SomeArray].Get[2]}\"=\"Second item!\"
 +
 +
    joMyValue["SomeArray"]:Add["{\"Ooooh\":\"You can also add entire objects\",\"Yep, and Arrays\":[1,2,3,4,5]}"]
 +
 +
    ; Here's the JSON
 +
    echo JSON=${joMyValue.AsJSON}
 +
/*
 +
JSON={"SomeArray":["First item!","Second item!",{"Ooooh":"You can also add entire objects","Yep, and Arrays":[1,2,3,4,5]}],"SomeFloatingPoint":1.234000,"SomeInteger":12,"SomeString":"This is valid"}
 +
*/ 
 +
 +
    ; File parsing is also built in!
 +
    variable jsonvalue joFromFile
 +
    joFromFile:ParseFile["myjsonfile.json"]
 +
 +
    echo ${joMyValue.AsJSON}
 +
}
  
 
== See Also ==
 
== See Also ==
 
{{LavishScript:ObjectType}}
 
{{LavishScript:ObjectType}}
 
[[Category:JSON]]
 
[[Category:JSON]]

Revision as of 18:04, 30 October 2018

Overview

Object Type Vitals
jsonvalue
Defined By LavishScript
Inherits none
Reduces To same as AsString
Variable Object Type jsonvaluecontainer
Uses Sub-Types no
C/C++ Type void *

A jsonvalue is an immutable json value

Members

  • string AsString: The contained value as a string
  • string AsJSON: The contained value as single-line JSON text
  • string AsJSON[multiline]: The contained value as multiline JSON text
  • ... Value: The contained value
  • string Type: The type of JSON object stored; one of: null, object, string, number, array, true, false, integer. Note that while the JSON standard does not differentiate between floating-point numbers and integers, LavishScript does

Methods

none.

Examples

A walk-through of JSON usage
function main()
{
    ; initialize a json Object. Objects are denoted by {}
    ; json Objects contain "key":value pairs. The key is always a string, but any JSON value will do, including objects or arrays
    variable jsonvalue joMyValue={}  

    ; a variable that is a jsonvalue is really a "jsonvaluecontainer" https://www.lavishsoft.com/wiki/index.php/ObjectType:jsonvaluecontainer
    ; This container is holding an Object, currently empty.

    ; numeric types do not need any special formatting
    joMyValue:Set["SomeInteger",12]
    joMyValue:Set["SomeFloatingPoint",1.234]

    ; JSON strings are denoted by double quotes ""
    ; LavishScript also uses double quotes, so the inner quotes for the JSON must be escaped.
;    joMyValue:Set["SomeString","This is not valid and will produce a JSON error"]
    joMyValue:Set["SomeString","\"This is valid\""]

    ; Do note that the inner quotes are not actually stored as part of the JSON string, so when we show the values in this echo...
    echo ${joMyValue["SomeInteger"]}=12, ${joMyValue["SomeFloatingPoint"].Milli}=1.234, \"${joMyValue["SomeString"].Escape}\"=\"This is valid\"
    ; ... we put our own quotes there

    ; You can also have arrays. Arrays are denoted by []
    joMyValue:Set["SomeArray","[]"]    

    joMyValue["SomeArray"]:Add["\"First item!\""]
    joMyValue["SomeArray"]:Add["\"Second item!\""]
    echo \"${joMyValue[SomeArray].Get[1]}\"=\"First item!\" \"${joMyValue[SomeArray].Get[2]}\"=\"Second item!\"

    joMyValue["SomeArray"]:Add["{\"Ooooh\":\"You can also add entire objects\",\"Yep, and Arrays\":[1,2,3,4,5]}"]

    ; Here's the JSON
    echo JSON=${joMyValue.AsJSON}
/*
JSON={"SomeArray":["First item!","Second item!",{"Ooooh":"You can also add entire objects","Yep, and Arrays":[1,2,3,4,5]}],"SomeFloatingPoint":1.234000,"SomeInteger":12,"SomeString":"This is valid"}
*/  

   ; File parsing is also built in!
   variable jsonvalue joFromFile
   joFromFile:ParseFile["myjsonfile.json"]

   echo ${joMyValue.AsJSON}
}

See Also

LavishScript Object Types