Hi, I'm very new to Nim, so sorry if this is a noob question.
Firstly, I'd like to thank the Nim developers, Nim really is a fantastic system. I discovered it after trying Golang and finding it produced a 6MB binary for a simple http client application. The application is designed for an OpenWrt device with only 8Mb of flash! Nim on the other hand produces a very useable 100kb https client binary in release mode. Wonderful!
Now my question is regarding the data type used to initialise JSON objects. I understand that the syntax to create the object is:
var body = %*{
"id" : "NIMTEST12345",
"user" : "futurepoint",
"password" : "whatever",
"deviceClass" : 65535,
"pollingRate" : 5,
"event" : 0,
"fault" : false
}
And this works fine. I understand that '%*' is a function name which initialises the JSON object, but what type is the argument that follows? I've tried:
var device_details = {
"id" : "NIMTEST12345",
"user" : "futurepoint",
"password" : "whatever",
"deviceClass" : 65535,
"pollingRate" : 5,
"event" : 0,
"fault" : false
}
body = %*(device_details)
but I get the error Error: type mismatch: got ((string, int)) but expected '(string, string)' at the line "deviceClass" : 65535 which implies to me that tables must have values of a consistent data type.
How can I create the device_details as a separate object (to be referenced by other parts of the application) and convert it to json only when required?
Thanks.
Ok, After much RTFM and going through the JSON modules source code I'm going to answer my own question, please correct me on anything I have misunderstood:
It looks to me like the argument to the JSON constructor '%*' is not actually a data type in the proper sense. It's an expression which is passed to a macro, and broken down into NimNodes for evaluation. It has the limitation that it can only contain compile-time constants because evaluation of the expression is done by the compiler (not by the runtime).
You're almost spot on.
The limitation is that the types must be known at compile-time. The value can be set at runtime.