BoostedYAML
  • Overview
  • Getting Started
    • Implementation
    • Usage
    • Content manipulation
  • Routing
    • Routes
  • Settings
    • GeneralSettings
    • LoaderSettings
    • DumperSettings
    • UpdaterSettings
  • Serialization
    • Implementation
  • Updater
    • Updater
    • Document versioning system
      • Advanced topics
Powered by GitBook
On this page
  • Structure
  • Creating a document
  • BoostedYAML has insane File management capabilities!
  • Choosing to provide defaults is also a huge boost.
  • What must I do if I don't provide the document as a File and/or the defaults at all?
  • Examples
  • Reloading
  • Saving (dumping)
  • Updating
  • Changing the settings
  1. Getting Started

Usage

(4min read) Basic file operations to get started.

PreviousImplementationNextContent manipulation

Last updated 1 year ago

Structure

Every entry (key=value pair) in the document is represented as a .

  1. carry the comments and the actual value (Java object).

  2. are extensions of blocks representing values and provide plenty of methods for data accessing and modification. The is also a section - the root section.

Migrating from Spigot/BungeeCord API?

Example plugin showing you everything described on this page is available at -> look at the config.yml and onEnable() method.

Also, sections in BoostedYAML are similar to ConfigurationSection with a few differences, listed at the . You may want to read it to get along with the API quicker.

BoostedYAML uses "routes" (URI-like objects) to access and modify data (like paths in Spigot/BungeeCord API). Please read a short article about them - you'll need it:

Creating a document

You can create and initially a document using any of the provided YamlDocument.create() methods (click for JavaDoc):

// With defaults:
YamlDocument.create(File document, InputStream defaults, Settings... settings)
YamlDocument.create(InputStream document, InputStream defaults, Settings... settings)
// Without defaults:
YamlDocument.create(File document, Settings... settings)
YamlDocument.create(InputStream document, Settings... settings)
  • Document: the actual content that will be loaded and made available to you.

  • Defaults: the default version of the document, usually bundled under "resources". Optional, but are required if you'd like to obtain default values of missing/invalid mappings.

Building with boosted-yaml-spigot?
GeneralSettings.builder().setSerializer(SpigotSerializer.getInstance()).build()

All the provided objects, including settings, are now said to be associated with the document. You will come across this term many times in the documentation.

Defaults are required by the updater and can also be used to substitute missing or invalid content in the document.

You will lose access to all features listed above. But don't worry! That doesn't mean you can't load, save or update the file, you will just need to provide the input/output/defaults each time to the method you're using and you must do everything manually (talking about the settings, which will not be effective for you).

Examples

Uses the class loader to obtain the defaults and uses KeyFormat.OBJECT (Route objects):

YamlDocument.create(new File("file.yml"), getClassLoader().getResource("default_file.yml"), GeneralSettings.builder().setKeyFormat(KeyFormat.OBJECT).build());

Configuration compatible with current state of the Spigot/BungeeCord file API:

YamlDocument.create(new File(getDataFolder(), "config.yml"), getResource("config.yml"));
YamlDocument.create(new File(getDataFolder(), "config.yml"), getResource("config.yml"), GeneralSettings.builder().setSerializer(SpigotSerializer.getInstance()).build();
YamlDocument#reload() throws IOException
YamlDocument#reload(File file) throws IOException
YamlDocument#reload(InputStream inputStream) throws IOException

The document will now contain YAML from the stream.

YamlDocument#save() throws IOException
YamlDocument#save(File file) throws IOException
YamlDocument#save(OutputStream stream, Charset charset) throws IOException

Saves to the provided output with the given charset.

YamlDocument#save(OutputStreamWriter writer) throws IOException

Saves to the provided output.

YamlDocument#dump()

Method returns the dumped contents as a string.

TIP: You're welcome to use other methods as well, if you'd like to save using custom (non-associated settings).

What is the updater?

Updater is used to update content of YAML documents, to contain at least the content defined by the defaults - while simutaneously doing everything automatically and keeping all comments.

This is a perfect solution for "reset your config" headache, with just one line of code.

Use DVS (document versioning system) to index documents by their versions (revisions). This tells the updater if the document is already up to date (ensuring the best performance) and allows you to use cool and useful features the updater offers. We'll get back to that in a second.

YamlDocument#update() throws IOException

Updates against the associated defaults (it will fail if there are not any).

YamlDocument#update(InputStream defaults) throws IOException

Loads and updates against the provided defaults.

To ensure you have always up to date document, it is recommended to call this method after creation and each reload.

TIP: Use the DVS (document versioning system) to index documents using versions (revisions).

The cool features you can use:

  • relocations: if you moved some content from one place in the document to another,

  • ignored routes: for sections which users can freely extend (whose content is not strictly defined),

  • mappers: if you changed the datatype of the value, for example for settings which were true/false but are represented by an enum now,

  • custom logic: apply custom logic to the document - access, delete, change data your way.

Changing the settings

You can associate new settings at any time using the following method:

YamlDocument#setSettings(@NotNull Settings... settings)

The given settings will be now associated with the document.

This method has replaced the 4 association methods listed below since v1.3.2. These are now deprecated and subject for removal:

YamlDocument#setLoaderSettings(@NotNull LoaderSettings loaderSettings)
YamlDocument#setDumperSettings(@NotNull DumperSettings dumperSettings)
YamlDocument#setGeneralSettings(@NotNull GeneralSettings generalSettings)
YamlDocument#setUpdaterSettings(@NotNull UpdaterSettings updaterSettings)

WARNING!

Always refer to the method documentation, as there are a few limitations to mind and be aware of when re-associating settings.

There are 4 types of settings you can provide: , , and . These modify the behaviour of the documents during runtime, loading, dumping and updating.

If you chose to use KeyFormat.OBJECT (article about ) provide it here via (KeyFormat.STRING is the default).

If you want to continue using , provide custom with the Spigot-compatible serializer:

This serializer is only a bridge between BoostedYAML and the Spigot's serialization system. To register classes for serialization, use just as you have been.

BoostedYAML has insane management capabilities!

If you give the document as an instance of , it will be automatically and (yes, everything is customizable). It will be used as input/output, so you can use and methods wihout providing any parameter. The file itself will be accessible using .

These features are not available if the document is given as a .

Choosing to provide defaults is also a huge boost.

Now, the benefits. By providing them, if a file is being created because it does not exist (see above), it's content and content of the loaded document will be the defaults (instead of an empty file and document). The updater will use the given defaults automatically, so you can not only use without any parameter, but also enable , so you get up-to-date data each time you (re)load the document.

Selected section methods may reach out to the defaults to substitute deleted or malformed data, or collect all available data (bulk content getters which return a collection of blocks, values, keys...). We understand that this might not be a benefit. If you'd like to provide the defaults for updater and file creation purposes and consumption benefits, but don't want this behaviour, disable .

What must I do if I don't provide the document as a and/or the defaults at all?

Configuration compatible with current state of the Spigot/BungeeCord file and serialization API (as stated above, continue using to register your classes):

Reloading

Reloads from the associated (it will fail if there is not any).

The behaviour is the same as loading initially and if the file does not exist, the document will be reloaded from a copy of the defaults (or empty if there are not any). It will also automatically be to the file (enabled by default).

Reloads from the provided .

If the file does not exist, the document will be reloaded from a copy of the defaults (or empty if there are not any). It will also automatically be to the file (enabled by default).

Reloads from the provided .

TIP: No matter the method you use, if there are any defaults, you can enable (disabled by default), which will ensure your document is always up to date.

Saving (dumping)

Saves to the associated (it will fail if there is not any) with UTF-8 charset.

Saves to the provided with UTF-8 charset.

Updating

To ensure you have always up to date document, it is recommended to call this method after creation and each reload. Or, let BoostedYAML all of this for you automatically by enabling .

If there's any associated , the document will be (enabled by default).

If the document is already up to date, it does not need an update. Document versions tell the updater if this is case and also allows you to use plenty of cool and useful features it offers. Read quick setup (~2 min).

⚡
♻️
📂
🔝
GeneralSettings
LoaderSettings
DumperSettings
UpdaterSettings
routes
GeneralSettings
ConfigurationSerialization
GeneralSettings
ConfigurationSerialization
👷‍♂️
File
Stream
⁉️
File
ConfigurationSerialization
File
File
InputStream
File
File
here
⚡
automatic updating
🗃️
😄
👍
➕
block
Blocks
Sections
Map
document
https://github.com/dejvokep/boosted-yaml-example
Routes
HERE
load
➕
update()
File
reload()
save()
getFile()
File
method translation table for sections
saved afterwards
saved after each update
use of the defaults via GeneralSettings
created if it does not exist
automatic updating
created and saved
created and saved
automatic updating