No description
Find a file
Gabben efa0b92765
Merge pull request #28 from gabbhack/extagged-pragma
Fix double call of nextElement if deserWith used
2023-07-30 21:00:15 +05:00
.github 1.6.14 is broken 2023-07-30 20:57:09 +05:00
src Fix double call of nextElement if deserWith used 2023-07-30 15:11:40 +05:00
tests Move tests to one place 2023-04-04 21:03:23 +05:00
.gitignore v0.3.2 (#15) 2023-03-16 14:52:18 +05:00
config.nims v0.3.0 (#13) 2023-02-04 20:02:50 +05:00
deser.nimble Fix test task 2023-04-04 21:03:39 +05:00
LICENSE v0.3.0 (#13) 2023-02-04 20:02:50 +05:00
nim.cfg v0.3.0 (#13) 2023-02-04 20:02:50 +05:00
nimdoc.cfg Documentation fix 2022-08-24 04:00:44 -07:00
README.md v0.3.0 (#13) 2023-02-04 20:02:50 +05:00
THIRD-PARTY-NOTICES.TXT v0.3.0 (#13) 2023-02-04 20:02:50 +05:00

Deser nim-version-img

Serde-like de/serialization library for Nim.

nimble install deser

Documentation


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:

Supported formats

Also read:

Example

import std/[
  options,
  times
]

import
  deser,
  deser_json

proc fromTimestamp(deserializer: var auto): Time =
  fromUnix(deserialize(int64, deserializer))

proc toTimestamp(self: Time, serializer: var auto) =
  serializer.serializeInt64(self.toUnix())

type
  ChatType = enum
    Private = "private"
    Group = "group"

  Chat {.renameAll(SnakeCase).} = object
    id: int64
    username {.skipSerializeIf(isNone).}: Option[string]
    created {.serializeWith(toTimestamp), deserializeWith(fromTimestamp).}: Time

    case kind {.renamed("type").}: ChatType
    of Private:
      firstName: string
      lastName {.skipSerializeIf(isNone).}: Option[string]
      bio {.skipSerializeIf(isNone).}: Option[string]
    of Group:
      title: string

# Use public to export deserialize or serialize procedures
# false by default
makeSerializable(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"
  )

echo Chat.fromJson(json)
echo chat.toJson()

Also read:

License

Licensed under MIT license.

Deser uses third-party libraries or other resources that may be distributed under licenses different than the deser.

THIRD-PARTY-NOTICES.TXT

Acknowledgements