Giter Site home page Giter Site logo

DeserializationError: Invalid type: "datetime.datetime" when deserializing correct datetime string into Optional[datetime] about jsons HOT 2 CLOSED

aadamvanko avatar aadamvanko commented on June 19, 2024
DeserializationError: Invalid type: "datetime.datetime" when deserializing correct datetime string into Optional[datetime]

from jsons.

Comments (2)

ramonhagenaars avatar ramonhagenaars commented on June 19, 2024

Hi aadamvanko.

In your code, you tried to use jsons.load on a dict that was already (largely) deserialized on this line: withDateDict = jsons.loads(withDateStr). The jsons.load function only allows dicts with types that have a JSON equivalent (i.e. str, int, float, bool, list, tuple, set, dict and NoneType, not datetime.datetime).

If you want to deserialize a string, you could just use jsons.loads.

withDateStr = '''{"created":"2019-12-19T11:40:48Z"}'''
# withDateDict = jsons.loads(withDateStr)  <-- No need for this
# print(withDateDict)
withDateObj = jsons.loads(withDateStr, WithDateOK)  # <-- loads instead of load
withDateObj = jsons.loads(withDateStr, WithDateBad)  # <-- loads instead of load
print(type(withDateObj))
print(withDateObj)

Or, if you actually intended to do it in two steps, you should use json.loads instead:

withDateStr = '''{"created":"2019-12-19T11:40:48Z"}'''
withDateDict = json.loads(withDateStr)  # <-- json instead of jsons
print(withDateDict)
withDateObj = jsons.load(withDateDict, WithDateOK)
withDateObj = jsons.load(withDateDict, WithDateBad)
print(type(withDateObj))
print(withDateObj)

Hope that helps!

Regards,
Ramon

from jsons.

aadamvanko avatar aadamvanko commented on June 19, 2024

Thanks for the clarification, it is kind of misleading with json and also jsons and both have load and loads and works little bit differently from what you have shown, but my code works like it should so thank you.

Regards,
Adam

from jsons.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.