ObjectType:jsonvalue

From Lavish Software Wiki
Revision as of 13:59, 7 August 2019 by Lax (talk | contribs)
Jump to navigation Jump to search

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

  • WriteFile[filename]: Writes the JSON to file, in condensed single-line form.
  • WriteFile[filename,bool multiline]: Writes the JSON to file, optionally in multi-line form, with "\r\n" (Windows) line splitting
  • WriteFile[filename,bool multiline,line splitter]: Writes the JSON to file, optionally in multi-line form, with a specified line splitter

Examples

A LavishGUI 2 Item View Generator

See LGUI2:Item View Generator

	method GeneratePartView()
	{
		echo Controller:GeneratePartView ${Context(type)} ${Context.Args}

		; build an itemview lgui2element json
		variable jsonvalue joListBoxItem={}
		variable jsonvalue joStackPanel={}
 
		joListBoxItem:Set["jsonTemplate","\"listboxitem\""]
		joListBoxItem:Set["horizontalAlignment","\"stretch\""]
		joListBoxItem:Set["height":22]

		joStackPanel:Set["type","\"dockpanel\""]
		joStackPanel:Set["horizontalAlignment","\"stretch\""]
		joStackPanel:Set["children","[]"]
		joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":30,\"text\":\"${Context.Args[partNumber]}\"}"]
		joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"right\",\"margin\":[2,2,5,2],\"text\":\"${Context.Args[partName].Escape}\"}"]
		joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":100,\"text\":\"${Context.Args[partTitle].Escape}\"}"]

		joListBoxItem:Set["content","${joStackPanel.AsJSON.Escape}"]

		echo Context:SetView["${joListBoxItem.AsJSON.Escape}"]
		Context:SetView["${joListBoxItem.AsJSON.Escape}"]
	}


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 ${joFromFile.AsJSON}
}

See Also

LavishScript Object Types