I agree with @jgaskins. Note thereās no need to use JSON.mapping, for that, just regularly declaring the instance variables or using the record macros and then including DB::Serializable works as well.
Ah, looks like a small inconsistency in the language, while named tuples support keys starting with uppercase letters, keyword arguments do not. The macro does NamedTuple.new passing the keys of our named tuple literal as keyword arguments. So youāll either have to go back to your original code, yet better use a value object like suggested above or give up those weird key names for ones that start with a lower case letter
@jgaskins Your code works fine,Iāve changed my code to first-class object Thanks. @jhass I will give up on wired key names. but do we have struct tag in crystal like go: https://github.com/golang/go/wiki/Well-known-struct-tags
I want to return Unicode none English character in JSON keys.
struct OperationRecord
include DB::Serializable
getter id : Int32
getter quantity : Float64
getter price: Float64
end
operations = OperationRecord.from_rs(DBCON.query("select * from operation"))
(Side snark: Donāt store money as floating point, convert to cents (or whatever precision you need) and cast to integer as early as possible after data input, do operations and storage with that value and only convert back to float for display / data output).