dart-lang / yaml

A Dart YAML parser.
https://pub.dev/packages/yaml
MIT License
169 stars 58 forks source link

Codec | Implement a codec version similar to json #117

Closed KevinBLT closed 2 years ago

KevinBLT commented 2 years ago

We also were in demand of writing yaml (config) files.

This first commit is trying to implement a version that is similar to the json codec.

See test/codec_test.dart for references.

Fixes #113

Example usage:


const Map test = {
  'string' : 'Test text.',
  'number' : 1337,
  'bool'   : false,
  'list'   : [1,2,3,4],
  'map'    : {
    'a' : 1,
    'b' : 2,
    'c' : 'text',
    'd' : ['a', 'b', 'c']
  }
};

print(yaml.encode(test));

/* Output:
---
string: 'Test text.'
number: 1337
bool: false
list:
  - 1
  - 2
  - 3
  - 4
map:
  a: 1
  b: 2
  c: 'text'
  d:
    - 'a'
    - 'b'
    - 'c'
*/
google-cla[bot] commented 2 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

KevinBLT commented 2 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.

What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot I signed it!

jonasfj commented 2 years ago

For encoding and modifying YAML, see package:yaml_edit.

You can use it to write YAML by creating an empty document and updating it.

But in general when rendering YAML, you'll want to preserve comments and formatting which is what yaml_edit does.

KevinBLT commented 2 years ago

I agree with the preserving of comments etc. - and I didn't know the yaml_edit package so far. Nonetheless, I still think it would at least be a "nice to have" to expose it as a codec, too, giving developers unified functionalities for such tasks.