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

converting icalendar into JSON -- a working example #85

Closed
Tracked by #443
rbpasker opened this issue Feb 1, 2013 · 2 comments
Closed
Tracked by #443

converting icalendar into JSON -- a working example #85

rbpasker opened this issue Feb 1, 2013 · 2 comments

Comments

@rbpasker
Copy link

rbpasker commented Feb 1, 2013

this JSONencoder will properly handle icalendar.props and datetime items

I am contributing this in case anyone else needs it.

perhaps it can be added into props.py.

from json import JSONEncoder
import json
from datetime import datetime
from icalendar import Calendar, vDDDTypes, Event

class ICalendarEncoder(JSONEncoder):
    def default(self, obj, markers=None):

        try:
            if obj.__module__.startswith("icalendar.prop"):
                return (obj.to_ical())
        except AttributeError:
            pass

        if isinstance(obj, datetime):
            return (obj.now().strftime('%Y-%m-%dT%H:%M:%S'))

        return JSONEncoder.default(self,obj)    


cal = Calendar.from_ical(open('test.ics','rb').read())

for event in cal.walk(name="VEVENT"):
    suspect = json.dumps(event, cls=ICalendarEncoder)
    working = json.loads(suspect)
@rbpasker
Copy link
Author

rbpasker commented Feb 1, 2013

i added this to props.py and submitted it as a pull request

@jacadzaca
Copy link
Collaborator

jacadzaca commented Sep 5, 2022

@rbpasker would you be okay with us adding this little program as an example script?

@jacadzaca jacadzaca closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2022
MoralCode added a commit to CampusPulse/data-ingest that referenced this issue Feb 25, 2024
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