aws / aws-sdk-ruby-record

Official repository for the aws-record gem, an abstraction for Amazon DynamoDB.
Apache License 2.0
318 stars 41 forks source link

Serialize to JSON #125

Open szechyjs opened 1 year ago

szechyjs commented 1 year ago

It would be nice if there was an easy way to serialize a record to JSON with the type conversions/marshaling.

The to_h method isn't sufficient as it just uses the raw values.

# current state
rec.to_h
=> { timestamp: 0.1666824119e10, days: 0.62e2 }

# desired state
rec.cast_to_h
=> { timestamp: '2022-10-26T18:41:59-04:00', days: 62 }

A to_json method would be ideal, but a method to get a type casted Hash would be sufficient.

alextwoods commented 1 year ago

Thanks for opening this. Can you provide the model class code you're using?

Are you looking for the save_values method? It applies serialization to all of the attributes and returns a hash. (Note though that it ALSO translate the attribute names into their database names).