▲ 1000 ▼ Everyone's asking, 'Who's Json?', but nobody's asking, 'How's Json?' (thelemmy.club) submitted 3 weeks ago by CodiUnicorn@programming.dev to c/programmer_humor@programming.dev 146 comments fedilink hide all child comments
[–] _stranger_@lemmy.world 5 points 3 weeks ago (1 child) I would have accepted this answer. I was hoping for json.dumps, but that'll work. permalink fedilink source parent hideshow 2 child comments replies: [–] mat@jlai.lu 1 point 3 weeks ago (1 child) I think json.dumps would not work if there is a datetime in the dict, right? permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago (1 child) Yeah there's other edge cases too, like Decimal, but as long as you have those things imported when you send it through dumps it should work. permalink fedilink source parent hideshow 2 child comments replies: [–] mat@jlai.lu 1 point 3 weeks ago (1 child) Just tried it in an ipython shell import datetime import json json.dumps({"a": datetime.datetime(1970,1,1)}) And I got the TypeError: datetime not JSON serializable though. permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago* If you don't care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that's a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional. Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad. permalink fedilink source parent
[–] mat@jlai.lu 1 point 3 weeks ago (1 child) I think json.dumps would not work if there is a datetime in the dict, right? permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago (1 child) Yeah there's other edge cases too, like Decimal, but as long as you have those things imported when you send it through dumps it should work. permalink fedilink source parent hideshow 2 child comments replies: [–] mat@jlai.lu 1 point 3 weeks ago (1 child) Just tried it in an ipython shell import datetime import json json.dumps({"a": datetime.datetime(1970,1,1)}) And I got the TypeError: datetime not JSON serializable though. permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago* If you don't care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that's a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional. Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad. permalink fedilink source parent
[–] _stranger_@lemmy.world 1 point 3 weeks ago (1 child) Yeah there's other edge cases too, like Decimal, but as long as you have those things imported when you send it through dumps it should work. permalink fedilink source parent hideshow 2 child comments replies: [–] mat@jlai.lu 1 point 3 weeks ago (1 child) Just tried it in an ipython shell import datetime import json json.dumps({"a": datetime.datetime(1970,1,1)}) And I got the TypeError: datetime not JSON serializable though. permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago* If you don't care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that's a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional. Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad. permalink fedilink source parent
[–] mat@jlai.lu 1 point 3 weeks ago (1 child) Just tried it in an ipython shell import datetime import json json.dumps({"a": datetime.datetime(1970,1,1)}) And I got the TypeError: datetime not JSON serializable though. permalink fedilink source parent hideshow 2 child comments replies: [–] _stranger_@lemmy.world 1 point 3 weeks ago* If you don't care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that's a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional. Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad. permalink fedilink source parent
[–] _stranger_@lemmy.world 1 point 3 weeks ago* If you don't care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that's a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional. Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad. permalink fedilink source parent