Re: Parsing YAML into key:value tree nodes with C library
Ingy dot Net <[email protected]> Sat, 21 Aug 2021 05:33:01 -0700
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <CAHJtQJ5qAvmB-=fRCq81QopD8kTAC8Pc=HGvBfeULfnhwDtGQQ@mail.gmail.com> |
--===============7823173278835715961==
Content-Type: multipart/alternative; boundary="000000000000bd105f05ca10fc7c"
--000000000000bd105f05ca10fc7c
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Greetings,
This mailing list isn't used for much anymore, save high level
announcements from time to time.
A couple things:
- Check out https://github.com/pantoniou/libfyaml/ which is more full
featured and under active development.
- Join https://matrix.to/#/#chat:yaml.io which is the new best way to
engage with the YAML community.
- #yaml IRC is still around on the libera.chat IRC network.
Cheers, Ingy
On Mon, Aug 16, 2021 at 12:19 PM Felix Krause <[email protected]> wrote:
> Hi David,
>
> libyaml does contain such an API:
> https://github.com/yaml/libyaml/blob/master/include/yaml.h#L1457
>
> This gives you a yaml_document_t with a root node accessible via
> yaml_document_get_root_node. The API is not well-documented but basically
> nodes can refer to sub-nodes via an int index and you use
> yaml_document_get_node to retrieve subnodes.
>
> mapping and sequence nodes store their children in a stack structure =E2=
=80=93 you
> iterate them via
>
> for (yaml_node_item_t *cur =3D node->data.sequence.start; cur !=3D node->=
data.sequence.top; ++cur) {
> // ...
> }
>
> Now if you load in a YAML that contains a mapping with scalar key/value
> pairs, your root node will be a mapping and the subnodes will be scalars.
> ------------------------------
>
> If you want a more convenient API, I did write this utility in the past:
> https://github.com/flyx/libyaml_constructor
>
> It is a code generator that reads in a C header containing type
> definitions and autogenerates code that loads YAML into those types (usin=
g
> libyaml). With this, you can define how your YAML structure must look via=
C
> types and some annotations in the given header.
>
> That tool was a part of a project which I abandoned, hence I never did a
> proper release. However it did work quite well for all I can remember. Th=
e
> compile-time dependency on libclang (to parse your C header) may be a bit
> of a hurdle.
>
> Cheers,
> Felix
> _______________________________________________
> Yaml-core mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/yaml-core
>
--000000000000bd105f05ca10fc7c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Greetings,<div><br></div><div>This mailing list isn't =
used for much anymore, save high level announcements from time to time.</di=
v><div><br></div><div>A couple things:<br><ul><li>Check out=C2=A0<a href=3D=
"https://github.com/pantoniou/libfyaml/">https://github.com/pantoniou/libfy=
aml/</a> which is more full featured and under active development.</li><li>=
Join=C2=A0<a href=3D"https://matrix.to/#/#chat:yaml.io">https://matrix.to/#=
/#chat:yaml.io</a> which is the new best way to engage with the YAML commun=
ity.</li><ul><li>#yaml IRC is still around on the libera.chat IRC network.<=
/li></ul></ul></div><div>Cheers, Ingy</div><div><br></div></div><br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Aug 16, 2=
021 at 12:19 PM Felix Krause <<a href=3D"mailto:[email protected]">yaml@flyx=
.org</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1=
ex"><u></u>
<div>
<div style=3D"font-family:sans-serif"><div style=3D"white-space:normal">
<p dir=3D"auto">Hi David,</p>
<p dir=3D"auto">libyaml does contain such an API: <a href=3D"https://github=
.com/yaml/libyaml/blob/master/include/yaml.h#L1457" style=3D"color:rgb(57,1=
31,196)" target=3D"_blank">https://github.com/yaml/libyaml/blob/master/incl=
ude/yaml.h#L1457</a></p>
<p dir=3D"auto">This gives you a <code style=3D"background-color:rgb(247,24=
7,247);border-radius:3px;margin:0px;padding:0px 0.4em" bgcolor=3D"#F7F7F7">=
yaml_document_t</code> with a root node accessible via <code style=3D"backg=
round-color:rgb(247,247,247);border-radius:3px;margin:0px;padding:0px 0.4em=
" bgcolor=3D"#F7F7F7">yaml_document_get_root_node</code>. The API is not we=
ll-documented but basically nodes can refer to sub-nodes via an <code style=
=3D"background-color:rgb(247,247,247);border-radius:3px;margin:0px;padding:=
0px 0.4em" bgcolor=3D"#F7F7F7">int</code> index and you use <code style=3D"=
background-color:rgb(247,247,247);border-radius:3px;margin:0px;padding:0px =
0.4em" bgcolor=3D"#F7F7F7">yaml_document_get_node</code> to retrieve subnod=
es.</p>
<p dir=3D"auto">mapping and sequence nodes store their children in a stack =
structure =E2=80=93 you iterate them via</p>
<pre style=3D"background-color:rgb(247,247,247);border-radius:5px;margin-le=
ft:15px;margin-right:15px;max-width:90vw;overflow-x:auto;padding:5px" bgcol=
or=3D"#F7F7F7"><code style=3D"background-color:rgb(247,247,247);border-radi=
us:3px;margin:0px;padding:0px" bgcolor=3D"#F7F7F7">for (yaml_node_item_t *c=
ur =3D node->data.sequence.start; cur !=3D node->data.sequence.top; +=
+cur) {
// ...
}
</code></pre>
<p dir=3D"auto">Now if you load in a YAML that contains a mapping with scal=
ar key/value pairs, your root node will be a mapping and the subnodes will =
be scalars.</p>
<hr style=3D"background:linear-gradient(to right,rgb(204,204,204),rgb(51,51=
,51),rgb(204,204,204)) rgb(51,51,51);border:0px;height:1px" height=3D"1">
<p dir=3D"auto">If you want a more convenient API, I did write this utility=
in the past: <a href=3D"https://github.com/flyx/libyaml_constructor" style=
=3D"color:rgb(57,131,196)" target=3D"_blank">https://github.com/flyx/libyam=
l_constructor</a></p>
<p dir=3D"auto">It is a code generator that reads in a C header containing =
type definitions and autogenerates code that loads YAML into those types (u=
sing libyaml). With this, you can define how your YAML structure must look =
via C types and some annotations in the given header.</p>
<p dir=3D"auto">That tool was a part of a project which I abandoned, hence =
I never did a proper release. However it did work quite well for all I can =
remember. The compile-time dependency on libclang (to parse your C header) =
may be a bit of a hurdle.</p>
<p dir=3D"auto">Cheers,<br>
Felix</p>
</div>
</div>
</div>
_______________________________________________<br>
Yaml-core mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Yaml-c=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/yaml-core" rel=3D"n=
oreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/y=
aml-core</a><br>
</blockquote></div>
--000000000000bd105f05ca10fc7c--
--===============7823173278835715961==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============7823173278835715961==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core
--===============7823173278835715961==--