W3C HTML JSON form submission
w3.org
w3.org
Here's the part that I don't particularly like, speaking of subtle incompatibilities:
EXAMPLE 2: Multiple Values
<form enctype='application/json'>
<input type='number' name='bottle-on-wall' value='1'>
<input type='number' name='bottle-on-wall' value='2'>
<input type='number' name='bottle-on-wall' value='3'>
</form>
// produces
{
"bottle-on-wall": [1, 2, 3]
}
I've seen this ugly pattern before in things that map XML to JSON. Values spontaneously convert to lists when you have more than one of them. Here come some easily overlooked type errors.I don't know of any common patterns for working with "a thing or a list of things" in JSON; that kind of type mixing is the thing you hope to get away from by defining a good API. But all code that handles HTML JSON is going to have to deal with these maybe-list-maybe-not values, in a repetitive and boilerplatey way.
I hope that a standard such as this will eventually be adopted by real-life frameworks such as Django REST Framework, but I also hope that they just reject the possibility of multiple fields with the same name.
<form enctype='application/json'>
<input name='wow[such][deep][3][much][power][!]' value='Amaze'>
</form>
// produces
{
"wow": {
"such": {
"deep": [
null
, null
, null
, {
"much": {
"power": {
"!": "Amaze"
}
}
}
]
}
}
}However, what they've done is workable if all the browser vendors implement it.
But if you have a bug which produces a form with multiple fields with the same name, what should it do? I can think of two strategies: last one wins and the proposal.
Both are probably going to lead to things going wrong in the backend. At least with the proposal you:
A) can detect it with boilerplate that looks for arrays where you expect primitives.
B) figure out what the original data was if you need to try and manually repair it.
I'm sure we'll see lots of bad tutorials suggesting you just use the same field name to make an array though :(
[0] https://jersey.java.net/ [1] https://jax-rs-spec.java.net/
It's not clear if this style is really necessary. Why not just use the indexed syntax in this case?
I guess it may be the authors intention to mirror form encoded submissions, that would send all of these values to the server.
I'd suggest raising the point on the issue tracker so that it at least gets sufficient design discussion.
<form enctype='application/json'>
<input type='text' name='fruit' value='apple'>
<input type='number' name='bottle-on-wall' value='1'>
<input type='number' name='bottle-on-wall' value='2'>
<input type='number' name='bottle-on-wall' value='3'>
</form>
{
"fruit": ["apple"],
"bottle-on-wall": ["1", "2", "3"]
}
Then add attributes to explicitly give the structure. Since this is a browser spec we can add attributes. Here's the same form but with some attributes giving the explicit encoding. <form enctype='application/json'>
<input type='text' name='fruit' value='apple' enc='string'>
<input type='number' name='bottle-on-wall' value='1' enc='array/number'>
<input type='number' name='bottle-on-wall' value='2' enc='array/number'>
<input type='number' name='bottle-on-wall' value='3' enc='array/number'>
</form>
{
"fruit": "apple",
"bottle-on-wall": [1, 2, 3]
}
Dynamically varying JSON structures by form names and number of form elements is definitely going to be a source of bugs and security problems.You need only to look at the crazy ways in which MySQL mangles data to realize that silently "correcting" invalid input is not the way to go. The web has suffered enough of that bullshit, we seriously don't need another. Example 7 (mixing scalar and array types) gives me shudders. Example 10 (mismatched braces) seems to have a reasonable fallback behavior, though I'd prefer dropping the malformed field altogether.
If the form is obviously malformed, transmission should fail, and it should fail as loudly and catastrophically as possible, so that the developer is forced to correct the mistake before the code in question ever leaves the dev box.
Preferably, the form shouldn't even work if any part of it is malformed. If we're too timid to do that, at least we should leave out malformed fields instead of silently shuffling them around. Otherwise we'll end up with frameworks that check three different places and return the closest match, leaving the developer blissfully ignorant of his error.
While we're at it, we also need strict limits on valid paths (e.g. no mismatched braces, no braces inside braces) and nesting depth (most frameworks already enforce some sort of limit there), and what to do when such limits are violated. Again, the default should be a loud warning and obvious failure, not silent mangling to make the data fit.
This is supposed to be a new standard, there's no backward-compatibility baggage to carry. So let's make this as clean and unambiguous as possible!
"files": [{
"name": "dahut.txt",
"src": "data:text/plain;base64,REFBQUFBQUFIVVVVVVVVVVVVVCEhIQo="
}]
http://en.wikipedia.org/wiki/Data_URI_scheme2. While data URIs are awesome, it forces you to process that URI in some way (parse, regex, whatever) just to get the MIME type. Also, this forces 13 bytes of redundancy every time.
{
"name": "Bender"
, "hind": "Bitable"
, "shiny": true
}
Who puts commas at the start of a continuing line? What good could that possibly do?I've seen people do this in the SELECT portion of SQL queries too.
Personally, I hate this.
It also lets you remove the line (for the same reason that you can comment it out) without modifying other lines. This is somewhat convenient if the file is one that you are going to manually edit (and even more useful if it is going to be the subject of line-oriented diffing tools, since the only changed lines will be the ones with meaningful changes.)
I'd agree that its less readable, though.
select *
from foo
where 1=1
AND bar = 1
AND baz = 2
That way you can comment out any of your conditions without breaking the syntax. UPDATE FOO
SET BAR = 1
, BAZ = 2
, QUUX = 3
I like it that way. Makes it clear the relationship between the continuing lines and their parents. Just the natural extension of WHERE ALICE = 1
AND BOB =2
AND CHARLIE = 3In your example, I don't know SET BAR = 1 isn't the end until I read the next line.
I mean, it's kind of the SQL analogue to the
MyObject
.Child
.Grandchild
.ItsMethod(stuff)
.HeyThatReturnedAnotherObject()
.MoreObject(moreStuff)That would only be an advantage above comma at the end on the last line. It really only moves the problem from the last line to the first one. Now you can't comment out the first line without removing a comma...
It can make resolving merges just a little bit easier.
Here's an item there's going to be another one,
Or
Here's an item
, I'm another one
Some encourage this so you can comment out a line without having to add/remove commas. However, this isn't the case if you want to comment out the first line; so in effect, it just moves the problem from the bottom to the top of the list. Other proponents say it's easier to spot typos (i.e., missing commas). For example:
var something = {
foo: 'bar',
abc: '123'
quux: null,
xyz: 456
};
vs: var something = {
foo: 'bar'
, abc: '123'
quux: null
, xyz: 456
};
In the second one, it's clearer that there's a comma missing.Personally, I think it's ugly and, while I'm sure I've erroneously omitted commas before, they're caught by the parser. No example comes immediately to mind where missing a comma can be interpreted as something other than a parse error.
{ "name": "Bender"
, "hind": "Bitable"
, "shiny": true }I'm not a fan, but I don't think we should make it out to be more than it really is.
<input name='kids[1]' value='Thelma'>
<input name='kids[0]' value='Ashley'>That's bad. Really, really bad.
<inputgroup name='kids'>
<input value='Thelma'>
<input value='Ashley'>
</inputgroup>Since these same servers failed to establish a consensus (Django using "a=1&a=2" as array, whilst php uses "a[]=1&a[]=1", for example), someone has to start ...
Of course we are. There are numerous new technologies competing to replace the role we have shoe-horned HTML into playing -- look at all the different templating technologies now in development, for example, and things like Web Components. There are also numerous technologies for marking up semantic content and/or styling such data for presentation.
The only thing that isn't changing right now is that we're still stuck with eventually reducing these alternatives to plain HTML for display in browsers, which is about as good an idea as insisting we reduce all styling to CSS and all programming to JavaScript. It's a historical accident, it's resulted in widespread dependence on tools that are nowhere near fit for the purposes they are now asked to serve, but there is so much momentum in the industry that building tools to accommodate the weaknesses as well as possible is the preferred strategy over completely starting over. See also: Almost everything about programming ever.
I've been writing a lot of ClojureScript/React lately and with that I only really touch HTML to include CSS and scripts. It's pretty glorious, highly productive, and makes a very strong case for opening up lower levels to allow new paradigms to evolve - with this set-up, HTML and JS only get in the way.
The browser is now (almost) an OS in a VM and, imho, the sooner we start treating it like that the better.
Main limitation on actually being able to use this is that `GET` and `POST` continue to be the only supported methods in browser form submissions right now, so eg. you wouldn't be able to make JSON `PUT` requests with this style anytime soon.
Might be that adoption of this would swing the consensus on supporting other HTTP methods in HTML forms.
That said, XForms is dead AFAIK, and that's not a bad thing.
$ jarg wow[such][deep][3][much][power][!]=Amaze
{"wow": {"such": {"deep": [null, null, null, {"much": {"power": {"!": "Amaze"}}}]}}}
[0]: http://jdp.github.io/jarg/In 2014, I think this is a good idea.
It also solves the issues with ambiguous syntax surrounding arrays of values.
<input name="field[1000000]">
Will generate a request that is ~5MB.Instead of encoding the files directly inside JSON, you could add them as extra parts of the MIME message, and just have a reference in the JSON value (as in email, which uses a "cid:<part-identifier>" URI to inline images as such, as described in RFC2392).
You'd still need special support on the server, though.
HTTP connections typically use compression with any modern browser and server, so there is likely to be little overhead in practice.
I personally prefer this new square bracket notation, but being a standard already gets more points.
This proposal allows regular HTML forms with multiple input elements, but submitting over JSON. I can't see how you could define that for arbitrary encodings without first defining how the form fields map to the encoded data for all the encodings you'd want to support.
It's no longer using all this, of course. They hired the guys from wowhead.com and had them redo the whole thing in a saner stack.
<p>This is <b>bold</b> text.</p>
"p": "This is ... uh ... nevermind."
You could make a compelling argument that you shouldn't do this (separate block level and inline elements into separate encodings), but remember that even the relatively minor HTML->XHTML movement to put a bit of sanity into single-use tags like <br> -> <br/> failed miserably. {
p: [
{ text: 'This is' },
{ b: { text: 'bold' }},
{ text: 'text.'}
]
}
Not that I don't agree with you -- my suggestion is pretty messy and doesn't even go into how we'd deal with tag attributes -- but it's doable. <p>
<text>This is</text>
<b><text>bold</text></b>
<text>text.</text>
</p>
(which is a lot easier for a machine to parse, at least.)JSON and YAML are great for what they do: data serialization, but they're just not appropriate choices for text markup. You have to use the right tool for the job.
There was a time when the industry wanted to cram 100% of everything into XML, and that gave us XSLT, XAML, SOAP, etc. We don't want to go back to that, either.
Personally, I'm most fond of an extended Markdown syntax to replace HTML. But I'm not going to hold my breath waiting on web browser vendors to agree on such a syntax, so instead I have my HTTP server do the conversion to HTML for me.
But if you had to force it into JSON, then I would suggest using a different markup for inline elements, eg:
html
body
p: "This is [/italic/] text."
p: "This is [[google.com => a hyperlink.]]"And there goes my interest for this submission. Don't use overused memes in a submission. Liking the idea though.
It reminded me of that show that makes me laugh on Netflix.
It also illustrated the point that he was trying to make. I prefer warm examples, rather than ones using book titles.