Have you considered the JSONEncoder class?
This should do the trick:
`#require “JSONEncoder.class.nut:1.0.0”
class TimeStamp {
_date = null;
constructor(d=null){
if(d==null){
_date = date()
} else{
_date = d
}
}
function _serializeRaw() {
return format("%d.%06d", _date.time, _date.usec)
}
};
server.log(JSONEncoder.encode({
a = 123,
b = [1, 2, 3, 4],
c = TimeStamp,
d = 5.125,
e = TimeStamp(),
f = null,
g = true,
h = “Some
ùnicode\rstring ø∆ø”
}))
//Logs {“a”:123,“c”:{"_date":null},“b”:[1,2,3,4],“e”:1472688866.071246,“d”:5.125,“g”:true,“f”:null,“h”:“Some
ùnicode\rstring ø∆ø”}`
Parsing the long timestamps back into the Agent is a bit trickier… I use my arbitrary precision big number class, the JSONDecoder, and the following function to deal with it (once it is a big, I can more easily cast to int32 as required by calling split(bigNum.ToString(), “.”)[0]).
`
function JSONParseBig(s){
return JSONParser.parse(s, function(val, type){
if(“number” == type)
return Big(val)
return val;
})
}
`