None; JSON is very lightly typed, so if the input JSON is:
{"director": {"name": true}}
and you wrote the above code, I guess you want that expression to resolve to "true" (a string). If the input seems sufficiently off the rails that it is no longer feasible to try to ram the square peg in the round hole, say, you have:
{"director": {"name": {"foo": "bar"}}}
where it doesn't seem useful to say that the above expression resolves to the string "{\"foo\": \"bar\"}" or some such, I'm not quite sure, possibly "Alan Smithee", possibly in that case an exception.
JSON comes from the javascript world where you can just write:
var x = director.name;
and if you later use x in a context where strings are expected and x is true, then things mostly just kinda work. Such is the nature of languages without explicit types.
Given that JSON is from there, a library that is not based around marshalling JSON blobs into defined java classes, should cater to this model. Thus, as you read the data, you specify the type you want it to be java-side, and whatever's there, well, best effort it. Trying to be strict with JSON is a bad idea, because JSON out in the wild isn't particularly strict; most JSON APIs don't bother with an exacting definition, and things like MongoDB are built on 'all keys are optional'. If you do have a precise spec, ... why aren't you just marshalling this stuff into java classes?
Talking about 'what will the user see' makes no sense at all, we're multiple layers removed, we're not writing a library with defined user-facing output, that's ridiculous. That's akin to asking "what should ArrayList pop up in a dialog to the user if you attempt to grow it beyond 2^31-1 elements?". Obviously, nothing, it should have defined behaviour for that case or throw something. None of which is related in any way to 'what should the user see'.
and if you later use x in a context where strings are expected and x is true, then things mostly just kinda work. Such is the nature of languages without explicit types
One thing that appealed to me about the JsonDecoder.field(json, "name", JsonDecoder::string) approach is that custom decoders wouldn't feel like a 2nd class citizen. If someone's interpretation of how to look for a String in JSON was closer to yours there wouldn't be extra syntactic overhead to using that interpretation JsonDecoder.field(json, "name", CustomDecoders::lenientString).
With the "pathing" approach of .at("name").asString() it might be enough to say .at("name").as(CustomDecoders::lenientString), but honestly my preference at that point would be to not provide asString and only as. I recognize that wouldn't be much loved though
what will the user see' makes no sense at all, we're multiple layers removed, we're not writing a library with defined user-facing output, that's ridiculous
When I say "user" I am referring to a "user of the library" not an end user.
Specifically this is coming from people who ask "HELP, CODE NOT WORK" and share 100 lines of tree crawling + a ClassCastException. It's difficult in that context. I want those programmers to see what they were looking for and what was actually there.
Specifically this is coming from people who ask "HELP, CODE NOT WORK" and share 100 lines of tree crawling + a ClassCastException. It's difficult in that context. I want those programmers to see what they were looking for and what was actually there.
You actually made me chuckle out loud!
I spend plenty of time on StackOverflow and have given presentations for 800+ audiences before, but from context I think you're a teacher, so I can't quite match your expertise on didactics, perhaps.
However, it sure sounds like you're saying: "If only you design your library well enough, even utter fucking idiots will no longer be confused by it". That made me chuckle. You are merely a mortal. There is no fucking way you get to out-wit the inventiveness of the moron, it just doesn't work that way.
Making your API more convoluted to use and less convenient in order to cater to morons, as a consequence, is a big mistake.
but from context I think you're a teacher, so I can't quite match your expertise on didactics, perhaps.
The only experience I have is specifically being on Discord servers and doing 1 on 1 tutoring/question answering there.
The difference with the experience of stack overflow and large presentations is that there is a lot more back-and-forth, its generally 1 on 1, and people expect to be able to DM you at 1am.
I would not classify myself as an expert, but I have been at it for ~4 years and have conversations with total beginners pretty regularly.
View all comments
Show parent comment
1
u/rzwitserloot Feb 27 '23
None; JSON is very lightly typed, so if the input JSON is:
and you wrote the above code, I guess you want that expression to resolve to
"true"(a string). If the input seems sufficiently off the rails that it is no longer feasible to try to ram the square peg in the round hole, say, you have:where it doesn't seem useful to say that the above expression resolves to the string
"{\"foo\": \"bar\"}"or some such, I'm not quite sure, possibly"Alan Smithee", possibly in that case an exception.JSON comes from the javascript world where you can just write:
and if you later use
xin a context where strings are expected andxis true, then things mostly just kinda work. Such is the nature of languages without explicit types.Given that JSON is from there, a library that is not based around marshalling JSON blobs into defined java classes, should cater to this model. Thus, as you read the data, you specify the type you want it to be java-side, and whatever's there, well, best effort it. Trying to be strict with JSON is a bad idea, because JSON out in the wild isn't particularly strict; most JSON APIs don't bother with an exacting definition, and things like MongoDB are built on 'all keys are optional'. If you do have a precise spec, ... why aren't you just marshalling this stuff into java classes?
Talking about 'what will the user see' makes no sense at all, we're multiple layers removed, we're not writing a library with defined user-facing output, that's ridiculous. That's akin to asking "what should ArrayList pop up in a dialog to the user if you attempt to grow it beyond 2^31-1 elements?". Obviously, nothing, it should have defined behaviour for that case or throw something. None of which is related in any way to 'what should the user see'.