Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yq throws TyperError datetime.datetime on ISOFormat strings in yaml file #5

Closed
jamesonjlee opened this issue Jul 6, 2017 · 3 comments

Comments

@jamesonjlee
Copy link

the default yaml loader will parse ISOFormat strings and create datetime objects which doesn't jsonify well.


Example:

$ yq --version
usage: yq [-h] [--yaml-output] [--width WIDTH] jq_filter [file [file ...]]
yq: error: too few arguments
$ pip freeze | grep yq
yq==2.1.2
$ yq . sec.yaml
yq: Error while running jq: TypeError: datetime.datetime(2016, 12, 20, 22, 7, 36) is not JSON serializable.
parse error: Unfinished JSON term
$ cat sec.yaml
data:
    thing: 1
    thing2: "stuff"
    datething: 2016-12-20T22:07:36Z
    datethingstr: "2016-12-20T22:07:36Z"

this needs to override the default encoder class.

# isoformat
class DateTimeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime):
            return o.isoformat()
        return json.json.JSONEncoder.default(self, o)

# usage
json.dumps(..., cls=DateTimeEncoder)
@kislyuk
Copy link
Owner

kislyuk commented Jul 6, 2017

Thanks! I can work on this tonight and cut a release, but if you wish to open a PR sooner, i can merge today.

@kislyuk kislyuk closed this as completed in fbd8a1a Jul 7, 2017
@kislyuk
Copy link
Owner

kislyuk commented Jul 7, 2017

Released in v2.2.0

@jamesonjlee
Copy link
Author

looks fixed, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants