NatLabs / serde

Serialization and Deserialization library for motoko
MIT License
16 stars 4 forks source link

Deserialization of types with reserved keywords? #5

Closed janusdotai closed 1 year ago

janusdotai commented 1 year ago

Greetings,

Thanks for this wonderful library. I'm using it to pull JSON in motoko from rosetta and in the response some of the named object properties are of the word "type" which happens to be a reserved keyword in motoko thus the compiler complains.

How do you go about handling this case? In other languages such as c# they use the "@" symbol infront of the property eg @type or @class

capture2 /100089505/a16b6bf6-b6f0-4bbe-97d2-7160efc1efa6)

Capture

tomijaga commented 1 year ago

Hey @janusdotai,

Thank you for bringing the issue to my attention. I have now released a fix for it. I considered your suggestion to use a symbol to escape the reserved keywords, but I thought it would be better if users chose the names themselves.

To achieve this, I have included a new record parameter in the toText and fromText functions. This parameter has a field called renameKeys, which takes an array of Text tuples for mapping field keys in records and variants to their new names. I have also updated the mops version and added an example in the readme.

Please let me know if there are any issues with the fix.

Cheers!

janusdotai commented 1 year ago

awesome thanks!