Idle Claus
Idle Claus

Reputation: 11

Parse yml file into tree

I have a yml file that looks like this:

STONE:
  - stone
  - stone block
  - smooth stone
WOOD:
  OAK_WOOD:
    - wood
    - oak wood
  SPRUCE_WOOD:
    - spruce wood
    - spruce wood block

There can be an infinite amount of children on any node.

I need to parse this into a Tree object in java. The root node would look something like this, when printed:

{STONE=[stone, stone block, smooth stone], WOOD=[OAK_WOOD=[wood, oak wood], SPRUCE_WOOD=[spruce wood, spruce wood block] ] }

How would I do this? I have no idea where to begin.

Upvotes: 1

Views: 2122

Answers (2)

dank8
dank8

Reputation: 369

Conversion from one type of data to another, in this case YAML to JSON or YAML to POJO might be for several reasons.

  • loading values into memory for operation on by POJO. Requires the Mapping of values into POJO classes, ObjectMappers are used in Jeremy's question from Jan 2019

  • Transforming values for display or serialisation. From one static structure to another (YAML -> HTML, or YAML -> JSON). Can be referred to as Templating or template engine

Upvotes: 0

MrBackend
MrBackend

Reputation: 667

Snakeyaml would do that pretty much out of the box. The tree structure you want is already the core of the YAML representation model, though that model itself is language independent. The output is just another presentation (called flow style by YAML) of the same model.

Upvotes: 1

Related Questions