expr-json-story Pub

Json expressions

send::  msg expr.json.create  (customer="Jane"="Jane")

expect::  (cart[="id"] is "Jane")
expect::  ( ((cart as String) contains "Jane"))

send::  msg expr.json.addItem  (sku="book1"="book1", quantity:Number=2=2)

send::  msg expr.json.addItem  (sku="book2"="book2", quantity:Number=3=3)

expect::  (cart is JSON)
expect::  (cart[="items"] is Array)
expect::  ( ((cart[="items"][="1"=1] as String) contains "book2"))

Access inside JSON object using diesel expressions: send::  msg ctx.set  (size=sizeOf(x=c...)

expect::  (size is 2)

Or using JS expression - you have more flexibility this way (costs more CPU) send::  msg ctx.set  (size74=js{{ cart....)

expect::  (size74 is 2)

You can do more fancy access: send::  msg ctx.set  (res78=js{{ cart....)

expect::  (res78 is "book1")

send::  msg ctx.set  (res84:JSON=({a:"aa"} ...)

expect::  (res84[="version"] == "V2")

Accessors

send::  msg ctx.set  (fieldName="items"="items")

send::  msg ctx.set  (res240=cart[="ite...)

expect::  (res240 is Array)
send::  msg ctx.set  (res241=cart[="ite...)

expect::  (res241 is "book1")
send::  msg ctx.set  (res242=cart[=fiel...)

expect::  (res242 is "book1")

More stuff: foreach

send::  msg ctx.foreach  (list=cart[="ite..., item="item"="item", msg="testing.invcheck"="testing.i...)

send::  msg ctx.set  (ajson:JSON={a-num:123...)

expect::  (ajson[="bnum"] is number)
expect::  (ajson[="bnum"] is 321)
send::  msg ctx.set  (line87=js{{ ajson...)

expect::  (line87 is 321)
send::  msg ctx.set  (line89=js{{ ajson...)

expect::  (line89 is 123)

Array of json

they map into array of json, we can acces members or result

send::  msg ctx.set  (g45:Array=[{a:"aa"},...)

send::  msg ctx.set  (g46=(g45 map (...)

expect::  (g46[="0"=0][:String="_id"] is "aa")

More

Support multi-line and ID with quotes, double quotes or nothing...

send::  msg ctx.set  (ajson2:JSON={a-num:123...)

expect::  (ajson2[="bnum"] is number)
expect::  (ajson2[="bnum"] is 321)

Make sure 123 is kept as a number, not "123"

send::  msg ctx.set  (body101:JSON={accountNu...)

expect::  (body101 ~= "(?s).*: 123.")

And also that we preserve the input string send::  msg ctx.set  (body110:JSON={accountNu...)

expect::  (body110 ~= "(?s).:"123".*")

send::  msg ctx.json  (a-num:Number=123=123, bnum:Number=321=321, a-string=abc)

expect::  (payload[="nada"] not defined)
expect::  (payload[="bnum"] is 321)

send::  msg expr.json.create2  (a:Number=1=1, b:Number=2=2)

expect::  (payload[="a"] is 1)

send::  msg expr.json.sum2  (a:Number=1=1, b:Number=2=2)

expect::  (payload[="a"] is 1)
expect::  (payload[="b"] is 2)

More JSON examples

send::  msg ctx.echo  (msg:JSON={})

expect::  (msg is {})

send::  msg ctx.set  (msg:Array=[])

expect::  (msg is [])

send::  msg ctx.set  (msg:Array=[1,"a",3])

expect::  (msg is [1,"a",3])

send::  msg ctx.set  (a:Number=1=1, b:Number=2=2)

send::  msg ctx.echo  (msg:Array=[a,"a",b])

expect::  (msg is [1,"a",2])

send::  msg ctx.set  (msg127:JSON={a:"a"})

expect::  ( ((msg127 as String) contains ""a""))

send::  msg ctx.set  (msg130:JSON={a:[1,"a",...)

TODO access arrays msg.a is an array

// $expect (msg130.a contains 1)

send::  msg ctx.set  (msg133:JSON={a:{a:3}})

expect::  (msg133[="a"][="a"] is 3)

send::  msg ctx.set  (msg:JSON={status:"o...)

Numbers type

send::  msg expr.simplejson.do  (x:Number=3=3)

expect::  (payload[="x"] is 3)
expect::  (payload[="x"] is Number)

calling a message with an object: flattening

This will match (a,b)

send::  msg ctx.json  (a:Number=1=1, b:Number=5=5)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

This will not match (a,b)

send::  msg ctx.json  (a:Number=7=7)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

Complex objects

send::  msg ctx.set  (student:JSON[DieselTestStudent]={name:"Joh...)

send::  msg ctx.echo  (m=student)

send::  msg ctx.echo  (m=student[="...)

// $send ctx.set(student.address.no="65") send::  msg test.diesel.rule40 

send::  msg ctx.echo  (m=student[="...)

expect::  (student[="address"][="no"] is "65")

Note - you can't do this: send::  msg ctx.echo  (m=student[="...)

But you can do this: send::  msg ctx.echo  (m=accessor(a...)

Array operators

send::  msg ctx.set  (a171:Array=[1,"a",3])

expect::  ( ("a" in [1,"a",3]))
expect::  ( ("a" in a171))
expect::  ( (3 in a171))
expect::  ( ("no" not in a171))

send::  msg test.diesel.isin  (x="123"="123", y:Number=3=3)

expect::  (y46 is true)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x="123"="123", y:Number=1=1)

expect::  (y46 not defined)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x="1234"="1234", y:Number=1=1)

expect::  (y46 not defined)

Composing and subtracting from jsons:

send::  msg ctx.set  (j188:JSON={a:"a",b:"...)

expect::  (j188[="b"] is "b")
send::  msg ctx.set  (j189=(j188 + {c...)

expect::  (j189[="c"] is "c")

send::  msg ctx.set  (j192=(j188 - {b...)

expect::  (j192[="b"] is undefined)

CSV/JSON data transformation

send::  msg ctx.set  (payload:Array=[{a:"aa",b...)

send::  msg ctx.jsonToCsv  (list=payload, separator=","=",")

expect::  (payload is Array)
send::  msg ctx.mkString  (separator=" "=" ")

expect::  (payload is String)
expect::  (payload contains "true")

send::  msg ctx.set  (csv ="llos,name,source,nlos,timestam"...="llos,name...)

send::  msg ctx.csvToJson  (payload=csv, separator=","=",", hasHeaders:Boolean=true)

expect::  (payload is Array)
expect::  (payload[="0"=0] is Json)

Accessor

send::  msg ctx.set  (parms="a.a.a,b.b.b"="a.a.a,b.b...)

send::  msg ctx.set  (p0:JSON={a:{a:{a:1...)

send::  msg ctx.set  (x220=js{{ parms...)

send::  msg ctx.echo  (x221=(x220 map ...)

expect::  (x221 is [1,2])

send::  msg ctx.echo  (x5=accessor(o...)

access objects from/to JS

send::  msg ctx.set  (students:Array=[{name:"a"...)

send::  msg testdiesel.jstx  (students=students)

expect::  (payload is 8)

send::  msg testdiesel.jstxo  (students=students)

expect::  (payload[="sum"] is 8)

Interpolated property names

send::  msg ctx.set  (x238:JSON={${1+2}:5})

expect::  (x238[="3"="3"] is 5)

Proper escaping of values

send::  msg ctx.set  (g45:Array=[{a:"aa"},...)

send::  msg ctx.set  (g46=(g45 as st...)

send::  msg ctx.set  (g47:JSON={a:("xx=" ...)

send::  msg ctx.echo  (x=g47[="a"])

expect::  (g47[="a"] is defined)

This one makes sure it's obtained properly unescaped:

expect::  (g47[="a"] contains ""bb"")

Associativity:

send::  msg ctx.set  (j254:Number=((1 + 2) +...)

expect::  (j254 is 6)

send::  msg ctx.set  (j255:JSON=(({a:1} + ...)

expect::  (j255 is {a:1,b:2,c:3})

send::  msg ctx.set  (a:JSON={a:1}, b:JSON={b:2}, c:JSON={c:3})

send::  msg ctx.set  (j256=((a + b) +...)

expect::  (j256 is {a:1,b:2,c:3})

send::  msg ctx.set  (j268=((a + {b:2...)

expect::  (j268 is {a:1,b:2,c:3})

Array of jsons

send::  msg ctx.set  (g273:Array=[])

send::  msg ctx.set  (g274=(g273 + {a...)

expect::  (g274 is array)
expect::  (g274[="0"=0][:String="a"] is "aa")
send::  msg ctx.set  (g274:JSON=({a:"aa"} ...)

expect::  (g274 is array)
expect::  (g274[="0"=0][:String="a"] is "aa")


Was this useful?    

By: Razie | 2019-06-10 .. 2022-03-17 | Tags: story , dsl , sanity


Viewed 323 times ( | History | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2022, all rights reserved.