[squeak-dev] Json dictionary tree accessing

Tobias Pape Das.Linux at gmx.de
Thu Sep 2 09:35:13 UTC 2021


Hi

I think you can do two tings: Model it out or mock with JsonObject

> On 2. Sep 2021, at 01:33, tim Rowledge <tim at rowledge.org> wrote:
> 
> When driving JS libraries from Seaside it is often necessary to build a tree of dictionaries to encode settings - for example, using vega-lite to display charts & graphs. 
> 
> I currently have a very simple case that ends up rather verbose - 
> 	|barchart|
> 	barchart := Dictionary new.
> 	barchart
> 	at: #'$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json';
> 	at: #description put: 'A simple bar chart';
> 	at: #width put: 'container';
> 	at: #usermeta put:
> 		(Dictionary new
> 			at: #embedOptions put: 
> 				(Dictionary new
> 				at: #export put: true;
> 				at: #compiled put: false;
> 				at: #editor put: false;
> 				yourself);
> 			yourself);
> 	at: #mark put: 
> 		(Dictionary new
> 			at: #type put: #bar;
> 			at: #tooltip put: true;
> 			yourself);
> 	at: #encoding put:
> 		(Dictionary new
> 			at: #x
> 			put: (Dictionary new
> 					at: #field put: 'label';
> 					at: #type put: #nominal;
> 					at: #axis put: 
> 						(Dictionary new
> 						at: #labelAngle put: -45;
> 						yourself);
> 					at: #title put: #Group;
> 					yourself); 
> 			at: #y
> 			put: (Dictionary new
> 					at: #field put: 'value';
> 					at: #type put: #quantitative;
> 					at: #title put: #Value;
> 					yourself);
> 	yourself);
> 	at: #height put:'container'.
> 	^barchart
> 
> ... and that's without any actual data!

> 
> If I want to do something like change the label angle I end up with ugliness like 
> 	(((chartSpec at: #encoding)
> 		at: #x)
> 		at: #axis)
> 		at:#labelAngle put: axisLabellingAngle.
> 
> I feel sure I've seen this done better but I can't find anything with my current searches. It wouldn't be too hard to implement a method to do #atPath: #(encoding x axis labelAngle) put: thingy but if it's been done cleanly by somebody I'd much rather just load it.
> 
> Somebody please point me to the ludicrously obvious answer any idiot should have spotted immediately...

Soo, if you mock it like that:

	|barchart|
	barchart := JsonObject new.
	barchart
		at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json';
		description: 'A simple bar chart';
		width: 'container';
		usermeta:
			(JsonObject new
				embedOptions:
					(JsonObject new
					export: true;
					compiled: false;
					editor: false;
					yourself);
				yourself);
		mark: 
			(JsonObject new
				type: #bar;
				tooltip: true;
				yourself);
		encoding:
			(JsonObject new
				x: (JsonObject new
						field: 'label';
						type: #nominal;
						axis: 
							(JsonObject new
							labelAngle: -45;
							yourself);
						title: #Group;
						yourself); 
				y: (JsonObject new
						field: 'value';
						type: #quantitative;
						title: #Value;
						yourself);
		yourself);
		height: 'container'.
		^barchart

you can easily say

	barchart encoding x axis lableAngle: axisLabellingAngle


The other thing is:
Make your Bar chart spec a class, as well as the metadata options etc.pp.
Then implement #jsonWriteOn: 
(see JsonDummyTestObject >> #jsonWriteOn: for an example)

Best regards
	-Tobias



More information about the Squeak-dev mailing list