From 111161f65c4496ea4d8b53e534225aeec0f4373b Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Thu, 16 Mar 2023 15:16:00 +0500 Subject: [PATCH] Documentation for helpers module --- src/deser.nim | 24 +++++++++++++++++++++++- src/deser/pragmas.nim | 2 ++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/deser.nim b/src/deser.nim index 3488b1f..790ecd6 100644 --- a/src/deser.nim +++ b/src/deser.nim @@ -49,7 +49,11 @@ let chat = Message.fromJson(json) echo chat.toJson() ``` -`created` field is the time in unix format, it is not convenient to work with it as `int`. But if we try to just use [Time](https://nim-lang.org/docs/times.html#Time), we get an error: +# De/serialize Time and DateTime + +In previous section we created `Message` type where `created` field is the time in unix format. + +It is not convenient to work with it as `int`. But if we try to just use [Time](https://nim-lang.org/docs/times.html#Time), we get an error: ```nim Error: type mismatch: got @@ -83,6 +87,24 @@ type makeSerializable(Message) makeDeserializable(Message) +``` + +.. Note:: Since deser 0.3.2 you can use the [helpers](https://deser.nim.town/deser/helpers.html) module and [deserWith](https://deser.nim.town/deser/pragmas.html#deserWith.t%2Ctyped) pragma for convenient time de/serialization. + +Example: + +```nim +import std/times + +import + deser, + deser_json + +type + Message = object + id: int + text: string + created {.deserWith(UnixTimeFormat).}: Time const json = """ { diff --git a/src/deser/pragmas.nim b/src/deser/pragmas.nim index e9a1e67..b1100ec 100644 --- a/src/deser/pragmas.nim +++ b/src/deser/pragmas.nim @@ -119,6 +119,8 @@ The given type (or anything actually) must have callable .serialize and .deseria .deserialize must be callable as `proc (self: withType, deserializer: var auto): FieldType` or `proc [T](self: withType, deserializer: var auto): T`. +.. Note:: You dont need to reimplement this example in your project. Please use [helpers](https://deser.nim.town/deser/helpers.html) module. + **Example:** ```nim import std/times