Model format conversion

April 5, 2021 ยท View on GitHub

Textrude is normally used to output text but when processing structured files such as Json or Yaml, it's sometimes useful to be able to write them back out in that format.

The Textrude namespace offers some useful methods for this:

  • to_json - convert an object tree to a JSON string representation
  • to_yaml - convert an object tree to a YAML string representation
  • to_csv - convert an object tree to a CSV string representation
  • to_line - convert an object tree to a set of lines
Source formatto_jsonto_yamlto_csvto_line
jsonfull supportfull supportarrays of flat objectsarrays of primitives
yamlfull supportfull supportarrays of flat objectsarrays of primitives
csvfull supportfull supportfull supportno support
linefull supportfull supportno supportfull support

Example

Input

{{
    object1 = { a :"value of a1", b:"value of b1"}
    object2 = { a :"value of a2", b:"value of b2"}

    objectArray =[object1,object2]

"JSON:
"
    objectArray | textrude.to_json
" 
-----------------------------------
YAML:
"
    objectArray | textrude.to_yaml
" 
-----------------------------------
CSV:
"
    objectArray | textrude.to_csv
}}

Output

JSON:
[
  {
    "a": "value of a1",
    "b": "value of b1"
  },
  {
    "a": "value of a2",
    "b": "value of b2"
  }
] 
-----------------------------------
YAML:
- a: value of a1
  b: value of b1
- a: value of a2
  b: value of b2
 
-----------------------------------
CSV:
a,b
value of a1,value of b1
value of a2,value of b2