I get a YAML string from REST service response in Apex code and I look for some similar way how to parse it and convert to collection of Salesforce objects.
For instance, I get such YAML as string:
---
item:
-
title: A Grief Observed
author: C. S. Lewis
author_by_last: Lewis, C. S.
isbn: "0060652381"
publisher: ZOND
on_hand: "5"
in_pub_date: 2001-01-01
-
title: "Grief Sanctified: From Sorrow to Eternal Hope: Including Richard Baxter's Timeles"
author: J. I. Packer
author_by_last: Packer, J. I.
isbn: "1581344406"
publisher: CROSS
on_hand: "5"
in_pub_date: 2002-09-01
And I'd like to convert it to list of Book objects. When I worked with JSON I used JSON class which provided deserialization functionality, and I'd like to know, does Salesforce provide something similar for YAML processing?
Best Answer
No, at time of writing (API v41.0), Salesforce does not have any built-in functionality to create or parse YAML. That means you'd need to build your own (or adapt an existing one from another language, like Java, into Apex).
If you aren't looking for a general-purpose parser (something that can handle any valid YAML) and you don't expect the format of the data you're receiving to change, writing a domain-specific parser shouldn't be too much work.
When I find myself working with XML, I like to break the incoming schema into individual apex classes and build parsing methods into each one. Doing so keeps the parser manageable. A similar approach can be taken with YAML.
Using those classes is pretty simple. It does, however, require some additional setup.