Serde-like de/serialization library for Nim.
nimble install deser
Motivation
Many serializers have already been written for Nim. You can probably find at least two serializers for each format.The problem is that each library's API and customization options are different. I can't declare an object with renamed or skipped fields once and change the data format with one line.
Attempts to generalize the serializer were also made. However, I found only one library that is actively under development - nim-serialization. When installing the library downloaded a quarter of all libraries for Nim, so I did not try it.
Thus, there was no library for Nim that standardized the serialization process, so I wrote deser.
Also read:
- JSON - deser_json
Also read:
import std/[
options,
times
]
import
deser,
deser_json
procfromTimestamp(deserializer: varauto): Time=fromUnix(deserialize(int64, deserializer))
proctoTimestamp(self: Time, serializer: varauto) =
serializer.serializeInt64(self.toUnix())
typeChatType=enumPrivate="private"Group="group"Chat {.renameAll(SnakeCase).} =object
id: int64
username {.skipSerializeIf(isNone).}: Option[string]
created {.serializeWith(toTimestamp), deserializeWith(fromTimestamp).}: Timecase kind {.renamed("type").}: ChatType ofPrivate:
firstName: string
lastName {.skipSerializeIf(isNone).}: Option[string]
bio {.skipSerializeIf(isNone).}: Option[string]
ofGroup:
title: string# Use public to export deserialize or serialize procedures# false by defaultmakeSerializable(Chat, public=true)
makeDeserializable(Chat, public=true)
const
json =""" { "id": 123, "username": "gabbhack", "created": 1234567890, "type": "private", "first_name": "Gabben" }"""
chat =Chat(
id: 123,
username: some"gabbhack",
created: fromUnix(1234567890),
kind: Private,
firstName: "Gabben"
)
echoChat.fromJson(json)
echo chat.toJson()Also read:
Licensed under MIT license.
Deser uses third-party libraries or other resources that may be distributed under licenses different than the deser.
- serde.rs, for all the ideas I stole
- fusion/matching, for making it easier to work with object variants
- anycase