2011/7/27 okay_awright <[email protected]>:
> hello everyone,
Hi!
> I'm currently migrating and translating all my perl webservice adapters
> into liquidsoap scripting language (it's amazing what's new since
> 0.91!), but I've stumbled upon something really odd: of_json is not
> recognized as a built-in function.
of_json was just commited 3 days ago and should be part of the next
beta3 release which should come quite soon (say about a week).
If you want to use it, you will have to compile the latest HG default
branch, which I would recommend anyway considering the important bugs
in harbor that are fixed there.
By the way, we have to write a proper documentation for of_json but
let me write a few words about it now that we are talking about it :-)
In liquidsoap, we need to know the type of everything we manipulate.
Thus, we can't just have a function of_json of the type:
string -> 'a ('a means "anything")
because we would not be able to know later what is 'a exactly..
Thus, the function is of type:
(default:'a, string) -> 'a
In this case, if you pass, say, an integer as the default value, then
we know that the result of of_json will also be an integer. (minor
note: the json RFC specify that a proper JSON string is an array or an
object. We also accept simple integers, floats, strings and null).
This means that, when you use it, you need to provide a default value
which will be returned in case parsing fails.
Let's take an example. Suppose that we want to receive a list of
metadata, encoded as an object:
{ "title": "foo",
"artist": "bar" }
Then, you would use of_json with a default value:
[("error","fail")]
and do:
# Parse metadata from json
m = of_json(default= [("error","fail")], json_string)
In this case, we know exactly what type of data will be parsed from
the json string. Moreover, the type of the default value will also
constraint the parser. For instance, in the above example, a json
string "[1,2,3,4]" will not be accepted and the function will return
the values passed as default.
Finally, you can use the default value in two different ways:
* To detect that the received json string was invalid/could not be
parsed to the expected type. In the example above, if of_json return a
metadata value of [("error","fail")] (the default) then you can detect
in your code that parsing has failed and return an error to your
client.
* As a default value for the rest of the script, if you do not want
to care about parsing errors.. This can be useful for instance for
json-rpc notifications, which should not send any response to the
client anyway..
That's all for today, please let us know what awesome things you do
with liquidsoap and json :-)
Romain
------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Savonet-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/savonet-users