Different Yaml Array Representations

Different YAML array representations

Yes, to any YAML parser that follows the spec, they are equivalent. You can read the spec here: http://www.yaml.org/spec/1.2/spec.html

Section 3.2.3.1 is particularly relevant (emphasis mine):

3.2.3.1. Node Styles


Each node is presented in some style, depending on its kind. The node style is a presentation detail and is not reflected in the serialization tree or representation graph. There are two groups of styles. Block styles use indentation to denote structure; In contrast, flow styles styles rely on explicit indicators.

To clarify, a node is any structure in YAML, including arrays (called sequences in the spec). The single-line style is called a flow sequence (see section 7.4.1) and the multi-line style is called a block sequence (section 8.2.1). A compliant parser will deserialize both into identical objects.

YAML equivalent of array of objects in JSON

TL;DR

You want this:

AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015

Mappings

The YAML equivalent of a JSON object is a mapping, which looks like these:

# flow style
{ foo: 1, bar: 2 }
# block style
foo: 1
bar: 2

Note that the first characters of the keys in a block mapping must be in the same column. To demonstrate:

# OK
foo: 1
bar: 2
# Parse error
foo: 1
bar: 2

Sequences

The equivalent of a JSON array in YAML is a sequence, which looks like either of these (which are equivalent):

# flow style
[ foo bar, baz ]
# block style
- foo bar
- baz

In a block sequence the -s must be in the same column.

JSON to YAML

Let's turn your JSON into YAML. Here's your JSON:

{"AAPL": [
{
"shares": -75.088,
"date": "11/27/2015"
},
{
"shares": 75.088,
"date": "11/26/2015"
},
]}

As a point of trivia, YAML is a superset of JSON, so the above is already valid YAML—but let's actually use YAML's features to make this prettier.

Starting from the inside out, we have objects that look like this:

{
"shares": -75.088,
"date": "11/27/2015"
}

The equivalent YAML mapping is:

shares: -75.088
date: 11/27/2015

We have two of these in an array (sequence):

- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015

Note how the -s line up and the first characters of the mapping keys line up.

Finally, this sequence is itself a value in a mapping with the key AAPL:

AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015

Parsing this and converting it back to JSON yields the expected result:

console.log(jsyaml.load(`
AAPL:
- shares: -75.088
date: 11/27/2015
- shares: 75.088
date: 11/26/2015
`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>

YAML Multi-Line Arrays

A YAML sequence is an array. So this is the right way to express it:

key:
- string1
- string2
- string3
- string4
- string5
- string6

That's identical in meaning to:

key: ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']

It's also legal to split a single-line array over several lines:

key: ['string1', 'string2', 'string3', 
'string4', 'string5',
'string6']

and even have multi-line strings in single-line arrays:

key: ['string1', 'long
string', 'string3', 'string4', 'string5', 'string6']

JSON Schema - array with multiple different object types

You have it slightly wrong. Currently your schema says... for the pipeline array, either, all of the items must be foo or all of the items must be baz.

You need to change your location of anyOf and items...

items applies to every item in the array.
anyOf checks that any of the subschema values are valid against the instance location (in this case, each item in the array).

{
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"pipeline": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string"
},
"someOption": {
"type": "integer"
}
},
"required": [
"foo"
]
}, {
"type": "object",
"additionalProperties": false,
"properties": {
"baz": {
"type": "string"
},
"anotherOption": {
"type": "integer"
}
},
"required": [
"baz"
]
}
]
}
}
}
}

How to create an array of arrays in yaml?

I believe what you want is something like this instead:

failoverconfig:
projectname: fairchild
unitconfig:
subunits:
- id: 8
sockets:
- powerunit: power-1
powerport: 10
- powerunit: power-2
powerport: 2
- id: 9
sockets:
- powerunit: power-1
powerport: 10
- powerunit: power-2
powerport: 2

YAML indentation for array in hash

Both ways are valid, as far as I can tell:

require 'yaml'

YAML.load(%q{---
1:
- 1
- 2
- 3
})
# => {1=>[1, 2, 3]}

YAML.load(%q{---
1:
- 1
- 2
- 3
})
# => {1=>[1, 2, 3]}

It's not clear why you think there should be spaces before the hyphens. If you think this is a violation of the spec, please explain how.

Why isn't there indentation for the array?

There's no need for indentation before the hyphens, and it's simpler not to add any.

Spring Boot yaml configuration for a list of strings

My guess is, that the @Value can not cope with "complex" types. You can go with a prop class like this:

@Component
@ConfigurationProperties('ignore')
class IgnoreSettings {
List<String> filenames
}

Please note: This code is Groovy - not Java - to keep the example short! See the comments for tips how to adopt.

See the complete example https://github.com/christoph-frick/so-springboot-yaml-string-list



Related Topics



Leave a reply



Submit