stencila / encoda

↔️ A format converter for Stencila documents
https://stencila.github.io/encoda/
Apache License 2.0
35 stars 9 forks source link

JSON & YAML encoding: order of properties #214

Closed nokome closed 4 years ago

nokome commented 5 years ago

When encoding to json, json5 or yaml the order of properties in the encoded content is dependent up the order that properties are defined. For machine readability that desn't matter but for human readability (e.g. demos of decoding, or test fixtures, using JSON or YAML) it does. For example, this YAML (from a test fixture) is harder to interpret because type isn't at the start:

  - content:
      - contentUrl: elife-46793-fig1-v1.tif
        format: image/tiff
        meta:
          inline: false
        type: ImageObject
    label: Figure 1.
    caption:
      - content:
          - >-
            Strategy identifying genetic vulnerabilities involved with G4
            biology.
        depth: 1
        id: s1
        type: Heading
      - content:
          - "Genome-wide shRNA silencing combined with G4 structure stabilisation by small molecules identifies genes that when depleted compromise cell viability. Cells are infected with a genome-wide pool of shRNA lentiviruses targeting the protein coding genome followed by G4 ligand treatment to stabilise genomic and/or RNA G4 structures. Two general outcomes are possible: a gene is not required in a G4-dependent process so there is no effect on cell viability\_(left); or gene silencing results in cell death either due to loss of a direct G4 interaction (e.g. binding/unwinding) or indirectly through gene loss in a G4-dependent pathway\_(right). In absence of ligand, cells are viable in presence of the shRNA. Dotted boxes highlight genotypes of disease significance for possible G4-based therapies (blue) and genes and biological pathways that involve and/or interact with G4 structures (orange)."
        type: Paragraph
    type: Figure

It would be easier to read, and understand, if type was always first and content (which tends to be long was always last:

  - type: Figure
    label: Figure 1.
    caption:
      - type: Heading
        depth: 1
        id: s1
        content:
          - >-
            Strategy identifying genetic vulnerabilities involved with G4
            biology.
      - type: Paragraph
        content:
          - "Genome-wide shRNA silencing combined with G4 structure stabilisation by small molecules identifies genes that when depleted compromise cell viability. Cells are infected with a genome-wide pool of shRNA lentiviruses targeting the protein coding genome followed by G4 ligand treatment to stabilise genomic and/or RNA G4 structures. Two general outcomes are possible: a gene is not required in a G4-dependent process so there is no effect on cell viability\_(left); or gene silencing results in cell death either due to loss of a direct G4 interaction (e.g. binding/unwinding) or indirectly through gene loss in a G4-dependent pathway\_(right). In absence of ligand, cells are viable in presence of the shRNA. Dotted boxes highlight genotypes of disease significance for possible G4-based therapies (blue) and genes and biological pathways that involve and/or interact with G4 structures (orange)."
    content:
      - type: ImageObject
        contentUrl: elife-46793-fig1-v1.tif
        format: image/tiff
        meta:
          inline: false

We could define an natural order for properties in the schema (currently we use alphabetical order). But instead I suggest we treat this "just-in-time" and write a simple function in Encoda, which codecs could use to reorder properties by walking the tree just before encoding e.g.

if (!isEntity(node)) return node
const {type, meta, content, ...rest} = node
return {type, ...rest, ...(meta === undefined ? {} : {meta}), ...(content === undefined ? {} : {content})}

@beneboy we had a discussion around this topic earlier re test fixtures, so let me know what you think.

stencila-ci commented 4 years ago

:tada: This issue has been resolved in version 0.79.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: