expr-json-story Pub Share

Json expressions

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

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

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

send::  msg expr.json.addItem  (sku="book2", quantity:Number=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)

expect::  (size is 2)

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

expect::  (size74 is 2)

You can do more fancy access: send::  msg ctx.set  (res78)

expect::  (res78 is "book1")

send::  msg ctx.set  (res84 :JSON)

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

Accessors

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

send::  msg ctx.set  (res240)

expect::  (res240 is Array)
send::  msg ctx.set  (res241)

expect::  (res241 is "book1")
send::  msg ctx.set  (res242)

expect::  (res242 is "book1")

More stuff: foreach

send::  msg ctx.foreach  (list, item="item", msg="testing.invcheck")

send::  msg ctx.set  (ajson :JSON)

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

expect::  (line87 is 321)
send::  msg ctx.set  (line89)

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)

send::  msg ctx.set  (g46)

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)

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)

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

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

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

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

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

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

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

send::  msg expr.json.sum2  (a:Number=1, b:Number=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=[..])

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

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

send::  msg ctx.echo  (msg :Array)

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

send::  msg ctx.set  (msg127 :JSON)

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

send::  msg ctx.set  (msg130 :JSON)

TODO access arrays msg.a is an array

// $expect (msg130.a contains 1)

send::  msg ctx.set  (msg133 :JSON)

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

send::  msg ctx.set  (msg :JSON)

Numbers type

send::  msg expr.simplejson.do  (x:Number=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, b:Number=5)

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

expect::  (res33 is 6)

This will not match (a,b)

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

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

expect::  (res33 is 6)

Complex objects

send::  msg ctx.set  (student :JSON[DieselTestStudent])

send::  msg ctx.echo  (m)

send::  msg ctx.echo  (m)

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

send::  msg ctx.echo  (m)

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

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

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

Array operators

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

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", y:Number=3)

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

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

expect::  (y46 not defined)

Composing and subtracting from jsons:

send::  msg ctx.set  (j188 :JSON)

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

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

send::  msg ctx.set  (j192)

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

CSV/JSON data transformation

send::  msg ctx.set  (payload :Array)

send::  msg ctx.jsonToCsv  (list, 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"...)

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

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

Accessor

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

send::  msg ctx.set  (p0 :JSON)

send::  msg ctx.set  (x220)

send::  msg ctx.echo  (x221)

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

send::  msg ctx.echo  (x5)

access objects from/to JS

send::  msg ctx.set  (students :Array)

send::  msg testdiesel.jstx  (students)

expect::  (payload is 8)

send::  msg testdiesel.jstxo  (students)

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

Interpolated property names

send::  msg ctx.set  (x238 :JSON)

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

Proper escaping of values

send::  msg ctx.set  (g45 :Array)

send::  msg ctx.set  (g46)

send::  msg ctx.set  (g47 :JSON)

send::  msg ctx.echo  (x)

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)

expect::  (j254 is 6)

send::  msg ctx.set  (j255 :JSON)

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

send::  msg ctx.set  (a :JSON, b :JSON, c :JSON)

send::  msg ctx.set  (j256)

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

send::  msg ctx.set  (j268)

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

Array of jsons

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

send::  msg ctx.set  (g274)

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

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

flatten, flatmap etc

val a290:Array=[[1,2],[3,4]] val a291=flatten(x=a290) expect::  (a291 is [1,2,3,4])

val a294=(a290 flatMap x=>(x + ["a"])()) expect::  (a294 is [1,2,"a",3,4,"a"])

val a297=(a290 flatMap x=>x()) expect::  (a297 is [1,2,3,4])


Was this useful?    

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


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

You need to log in to post a comment!

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