Signed-off-by: Ian Babrou <ibobrik@gmail.com> (cherry picked from commit824e7e8ae2
) Conflicts: docs/mkdocs.yml Add environment variable override instructions Signed-off-by: Richard Scothern <richard.scothern@gmail.com> (cherry picked from commit937c356585
) Fixing headings Signed-off-by: Mary Anthony <mary@docker.com> (cherry picked from commit7c93c8c265
) Fixes Issue #471 with Publish - Add sed to Dockerfile; this sed exists on publish script; breaks headings/nav in files without metadata - Ensure sed runs over storage-driver/ subdir - Add metadata to all the files (including specs) that don't have it; this ensures they display correctly on publish - Implement the fix for the showing up in Github - Update template with GITHUB IGNORES Signed-off-by: Mary Anthony <mary@docker.com> (cherry picked from commit68c0682e00
)
2.3 KiB
Docker Distribution JSON Canonicalization
To provide consistent content hashing of JSON objects throughout Docker Distribution APIs, the specification defines a canonical JSON format. Adopting such a canonicalization also aids in caching JSON responses.
Rules
Compliant JSON should conform to the following rules:
- All generated JSON should comply with RFC 7159.
- Resulting "JSON text" shall always be encoded in UTF-8.
- Unless a canonical key order is defined for a particular schema, object keys shall always appear in lexically sorted order.
- All whitespace between tokens should be removed.
- No "trailing commas" are allowed in object or array definitions.
Examples
The following is a simple example of a canonicalized JSON string:
{"asdf":1,"qwer":[],"zxcv":[{},true,1000000000,"tyui"]}
Reference
Other Canonicalizations
The OLPC project specifies Canonical JSON. While this is used in TUF, which may be used with other distribution-related protocols, this alternative format has been proposed in case the original source changes. Specifications complying with either this specification or an alternative should explicitly call out the canonicalization format. Except for key ordering, this specification is mostly compatible.
Go
In Go, the encoding/json
library
will emit canonical JSON by default. Simply using json.Marshal
will suffice
in most cases:
incoming := map[string]interface{}{
"asdf": 1,
"qwer": []interface{}{},
"zxcv": []interface{}{
map[string]interface{}{},
true,
int(1e9),
"tyui",
},
}
canonical, err := json.Marshal(incoming)
if err != nil {
// ... handle error
}
To apply canonical JSON format spacing to an existing serialized JSON buffer, one
can use
json.Indent
with the following arguments:
incoming := getBytes()
var canonical bytes.Buffer
if err := json.Indent(&canonical, incoming, "", ""); err != nil {
// ... handle error
}