Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text based data format which supports multiline strings

I search a text based data format which supports multiline strings.

JSON does not allow multiline strings:

>>> import json
>>> json.dumps(dict(text='first line\nsecond line'))
'{"text": "first line\\nsecond line"}'

My desired output:

{"text": "first line
second line"}

This question is about input and output. The data format should be editable with a editor like vi, emacs or notepad.

I don't care if simple quotes " or tripple quotes (like in Python) """ get used.

Is there a easy for human beings readable textual data interchange format which supports this?

Use case

I want to edit data with multiline strings with vi. This is not fun, if the data is in json format.

like image 714
guettli Avatar asked Aug 17 '16 09:08

guettli


2 Answers

I think you should consider YAML format. It supports block notation which is able to preserve newlines like this

data: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don't spit on the floor"
   So he carefully spat on the ceiling

Also there is a lot of parsers for any kind of programming languages including python (i.e pyYaml).

Also there is a huge advantage that any valid JSON is YAML.

like image 126
vsminkov Avatar answered Sep 28 '22 23:09

vsminkov


Apropos of your comment:

I want to use it for configuration. A lot of applications invent their own configuration language. I want to avoid this. But json and ConfigParser don't satisfy me. Json does not allow strings with newlines (only \n) and ConfigParser does not allow nested data structures. Next thing that I am missing: Validation (But this is a different topic).

There're 3 main options you have ConfigParser, ConfigObj, or YAML (PyYAML) - each with their particular pros and cons. All 3 are better then JSON for your use-case i.e. configuration file.

Now further, which one is better depends upon what exactly you want to store in your conf file.


ConfigObj - For configuration and validation (your use-case):

ConfigObj is very simple to use then YAML (also the ConfigParser). Supports default values and types, and also includes validation (a huge plus over ConfigParser).

An Introduction to ConfigObj

When you perform validation, each of the members in your specification are checked and they undergo a process that converts the values into the specified type. Missing values that have defaults will be filled in, and validation returns either True to indicate success or a dictionary with members that failed validation. The individual checks and conversions are performed by functions, and adding your own check function is very easy.

P.S. Yes, it allows multiline values.


Helpful links:

A Brief ConfigObj Tutorial

ConfigObj 5 Introduction and Reference


There are solid SO answers available on the comparison YAML vs ConfigParser vs ConfigObj:

What's better, ConfigObj or ConfigParser?

ConfigObj/ConfigParser vs. using YAML for Python settings file


like image 34
Nabeel Ahmed Avatar answered Sep 29 '22 00:09

Nabeel Ahmed