Skip to content

Commit

Permalink
Merge pull request #18 from gabbhack/add-helpers-documentation
Browse files Browse the repository at this point in the history
Documentation for helpers module
  • Loading branch information
Gabben authored Mar 16, 2023
2 parents 63cce07 + 111161f commit dd1f3ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/deser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typedesc[Time], Deserializer>
Expand Down Expand Up @@ -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 = """
{
Expand Down
2 changes: 2 additions & 0 deletions src/deser/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dd1f3ce

Please sign in to comment.