在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
delphi yaml https://github.com/lim417dev/Neslib.Yaml https://github.com/neslib/Neslib Neslib.Yaml is a library for parsing and emitting YAML and constructing YAML documents and streams. Neslib.Yaml is build on top of the LibYaml library and works on:
Installation and DependenciesTo install: > git clone --recursive https://github.com/neslib/Neslib.Yaml
This library only depends on the Neslib repository, which is included as submodule with this repository. For all platforms except MacOS 32-bit, there are no run-time dependencies: the LibYaml library is linked directly into the executable. For MacOS 32-bit, you need to deploy the YAML in a NutshellHere is a very brief introduction for YAML. For more detailed information take a look at the official YAML site or one of the many on-line resources such as this one. YAML (short for "YAML Ain't Markup Language") is a data serialization language. Unlike many other similar text-based languages (like JSON and XML) a primary goal of YAML is to be human-readable and also easy to create by humans. That's why its is commonly used for configuration files. However, it can be used for all kinds of data, such as this example from the YAML specification: invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
A YAML document is a tree of values, called nodes ( MappingsMappings are similar to Delphi dictionaries. A mapping is a collection of key/value pairs. The root note of the sample document above is a mapping: it maps the key Mappings can be written in block notation (as in the example) or flow notation (using curly braces When using block notation, YAML uses indentation for scoping. Only spaces are allowed for indentation (not tabs). The number of spaces doesn't matter as long as all values at the same level use the same amount of spaces. In the example, the value of the SequencesSequences are like Delphi arrays or lists. Small sequences can be written using flow notation (using square brackets In this example, each product in the sequence is a mapping of 4 key/value pairs. AliasesAll nodes have at least two properties: a An anchor can be used to mark a node in the document. You can then later refer back to this node using an alias. Anchors are prefixed with an ampersand ( Anchors must appear in the document before they can be referenced. Their names don't have to be unique within the document; if an new anchor is declared with the same name, it replaces the old anchor. ScalarsScalars are the simplest types. Everything that is not a mapping, sequence or alias is a scalar. In practice, scalars are just strings. All the keys in the example above are string scalars, but a lot of the values are as well (such as The YAML 1.1 specification (which is what LibYaml uses) treats all these scalars as strings, even if they are numbers or dates as in this example. You can use tags to explicitly state that a specific scalar is of a specific type. The Scalars can be written in different "styles":
There is much more to YAML, but this should cover many use cases. Loading or Parsing YAMLThe main entry point to this library is the A YAML file can contain multiple documents. If that is the case, you should use an Most of the time though, a YAML file contains just a single document and it is easier to start with a var
Doc: IYamlDocument;
begin
Doc := TYamlDocument.Load('invoice.yaml');
end;
You can load from a file or stream, or you can parse YAML text using the You can now use the
For example, to access the Price := Doc.Root.Values['product'].Nodes[0].Values['price'].ToDouble;
You use the To check the type of a node, you can use the Constructing and Emitting YAMLYou can also create a YAML document from scratch and save it to a file or convert it to YAML. To create a YAML document, use one of the Doc := TYamlDocument.CreateMapping;
You can then start to add key/value pairs" Doc.Root.AddOrSetValue('invoice', 34843);
Doc.Root.AddOrSetValue('date', '2001-01-23');
The To add a non-scalar value, use one of the other var
Products: TYamlNode;
begin
Products := Doc.Root.AddOrSetSequence('product');
end;
This adds a sequence to the mapping with the key var
Product: TYamlNode;
begin
Product := Products.AddMapping;
Product.AddOrSetValue('sku', 'BL394D');
Product.AddOrSetValue('quantity', 4);
// etc...
end;
Once you have constructed your document, you can save it to a file or stream using the var
Yaml: String;
begin
Yaml := Doc.ToYaml;
end;
You can pass an optional More InformationThere is more to Neslib.Yaml than described above. For more details you can look at the well-document LicenseNeslib.Yaml is licensed under the Simplified BSD License. See License.txt for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论