Hello all,
I was hoping someone could point me in the right direction as I am trying out the Einhugur YAML plugin. I have loaded a valid YAML file into a Einhugur yaml document / node, but I am struggling on how to find/build the right method to parse it into an object and then to a JSONItem object/JSON string etc.
Thank you in advance,
Mike
The question is a little broad, but after some thinking I realized its not obvious how to walk the Yaml tree given how the library under the hood sets it up.
So here is some stub that might help maybe…
try
var document as EinhugurYAML.Document = EinhugurYAML.Document.Parse(yaml)
// First important trick to walk the nodes because there is no counter
// is to walk them from node index one until you get nil result.
var i as Integer = 1 // First yaml node is 1
var node as EinhugurYAML.Node = document.NodeAt(i)
while node <> nil
if node.NodeType = EinhugurYAML.NodeTypes.SCALAR then
// You got some value here
elseif node.NodeType = EinhugurYAML.NodeTypes.MAPPING then
// Here you got mapping node so you need to go by the MappingNodeCount property on the node,
// and call Recursive routine, that walks those.
// If you are emitting JSON then mapping would be JSON object.
elseif node.NodeType = EinhugurYAML.NodeTypes.SEQUENCE then
// Here you got mapping node so you need to go by the SequenceNodeCount property on the node,
// and call Recursive routine, that walks those.
// If you are emitting JSON then sequence would probably be JSON array.
else
// We got empty node
end if
i = i + i
node = document.NodeAt(i)
wend
catch e as EinhugurYAML.YAMLException
MessageBox e.Message + " - At line: " + e.Line.ToString() + " - At col: " + e.Column.ToString() + " - At index: " + e.Index.ToString()
end try
And here would be just about example for the Recursive method for the mapping node:
Public Sub WalkMappingNode(parent as EinhugurYAML.Node)
var pair as EinhugurYAML.KeyValuePair
for i as Integer = 0 to parent.MappingNodeCount - 1
pair = parent.MappingNode(i)
// Here you got th key (if you need that for something in your export),
// I guess if translating to JSON it would end as your key to the JSON object
var key as String = pair.Key
// Now we check the value......
if pair.Value.NodeType = EinhugurYAML.NodeTypes.SCALAR then
// You got some value here
elseif pair.Value.NodeType = EinhugurYAML.NodeTypes.MAPPING then
// Here we call oour recursive method for mapping nodes
WalkMappingNode(pair.Value)
// If you are emitting JSON then mapping would be JSON object.
elseif pair.Value.NodeType = EinhugurYAML.NodeTypes.SEQUENCE then
// Here you got mapping node so you need to go by the SequenceNodeCount property on the node,
// and call Recursive routine, that walks those.
// If you are emitting JSON then sequence would probably be JSON array.
else
// We got empty node
end if
next
End Sub
Thank you so much Bjorn! I appreciate this and this is what I was looking for since I was trying to figure out if I had to walk the tree and how I could.
Mike