Skip to content

Custom Distinct DateTime type breaks serialization of object to string? #38

Closed
@PhilippMDoerner

Description

@PhilippMDoerner

Heyho!
I'm using jsony in a web-application. There I want to use it to serialize an de-serialize JSON strings into objects, specifically of norm-objects (an ORM) which is filled with data coming from a database.
Due to the way norm handles Datetimes, I had to implement a custom DateTime type that borrows/reimplements DateTime's functionality, that appears to cause some problems.

What I found is that, when doing so, jsony drops part of the serialization string silently, without an error message.
Here is a minimum example that demonstrates the issue when I run it on "https://play.nim-lang.org/".

import jsony
import options
import times

type DjangoDateTime* = distinct DateTime
proc format*(x: DjangoDateTime, f: string, loc: DateTimeLocale = DefaultLocale): string =
    let trueDt = x.DateTime 
    result = trueDt.format(f, loc)
    
proc dumpHook*(s: var string, value: DjangoDateTime) =
    s = value.format("yyyy-MM-dd HH:mm:ss'.'ffffff")

proc now*(): DjangoDateTime = DjangoDateTime(times.now())


type 
    A = object
        nameA: string
        datetimeA: DjangoDateTime


var a = A(nameA: "The name of A", datetimeA: now())
echo a.toJson()

This should print out {"nameA": "The name of A", "datetimeA": "<FORMATTED CURRENT DATETIME>"}.
Instead it prints out <FORMATTED CURRENT DATETIME>}.
It drops the parts of the string that come before DjangoDateTime is converted into a string.

I am not quite sure what is happening here, though I can only assume that it's DjangoDateTime causing this. How, I'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions