[Info-vax] XML, JSON and YAML, was: Re: Security, support and VMS, was: Re: A new VMS?
Simon Clubley
clubley at remove_me.eisner.decus.org-Earth.UFP
Wed May 5 08:43:46 EDT 2021
On 2021-05-04, Arne Vajhøj <arne at vajhoej.dk> wrote:
> On 5/4/2021 4:45 PM, Simon Clubley wrote:
>> On 2021-05-04, Arne Vajhøj <arne at vajhoej.dk> wrote:
>>> Which of the below are most readable?
>>
>> JSON, especially if it is formatted in Whitesmiths style.
>
> Really?
>
> The curly brackets and the double quotes make it more readable.
>
Was there a question mark at the end of that sentence ? If so, yes.
They give it nice visible structure without going over the top as XML does.
>>> XML:
>>>
>>> <abc>
>>> <ab>
>>> <a>1</a>
>>> <b>2</b>
>>> </ab>
>>> <c>3</c>
>>> </abc>
>>>
>>
>> This has structure, but is really ugly to read.
>>
>>> JSON:
>>>
>>> { "ab": {
>>> "a": 1,
>>> "b": 2
>>> },
>>> "c" : 3
>>> }
>>>
>>
>> This has structure and it is the best of the 3 options.
>>
>>> YAML:
>>>
>>> ab:
>>> a: 1
>>> b: 2
>>> c: 3
>>
>> This is horrible and does not have enough structure to be robust
>> against editing errors when being manually edited.
>>
>> Errors in both XML and JSON can be detected much more reliably
>> by a parser than errors in the above YAML example.
>
> You do not write much Python do you?
>
I write quite a bit of Python. I also dislike how whitespace is used
in Python as well.
> Using indentation for structure may seem weird for
> those familiar with begin end and { }, but that does
> not necessarily make it more error prone.
>
I am familiar with Wirth style block structure, C style block structure
and whitespace style block structure and the whitespace style block
structure is horrible compared to the first two.
> When I dabble a little bit in Python then I don't experience
> more problems with improper block structure than in
> Pascal or C.
>
> And even if an indentation is wrong then it will typical
> result in an error (rather similar to JSON - XML can
> be validated against a schema, which does a better
> check).
>
> Sometimes it is easier with an example.
>
> I will use Java. I could have used Python, but I know Java
> better than Python.
>
> $ type Demo1.java
> package yaml;
>
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.Map;
>
> import org.yaml.snakeyaml.Yaml;
>
> public class Demo1 {
> public static void main(String[] args) {
> try {
> Yaml parser = new Yaml();
> InputStream is = new FileInputStream(args[0]);
> Map<String, Object> cfg = (Map<String, Object>)parser.load(is);
> is.close();
> @SuppressWarnings("unchecked")
> Map<String, Object> ab = (Map<String, Object>)cfg.get("ab");
> int a = (Integer)ab.get("a");
> int b = (Integer)ab.get("b");
> int c = (Integer)cfg.get("c");
> System.out.printf("%d %d %d\n", a, b, c);
> } catch(Exception ex) {
> ex.printStackTrace();
> }
> }
> }
> $ javac -cp snakeyaml-1_12.jar Demo1.java
> Note: Demo1.java uses unchecked or unsafe operations.
> Note: Recompile with -Xlint:unchecked for details.
> $ type good1.yaml
> ab:
> a: 1
> b: 2
> c: 3
> $ java -cp ..:snakeyaml-1_12.jar "yaml.Demo1" good1.yaml
> 1 2 3
>
> all good, but let us see what happens if the indentation of
> the YAML gets messed up.
>
> $ type bad1.yaml
> ab:
> a: 1
> b: 2
> c: 3
> $ java -cp ..:snakeyaml-1_12.jar "yaml.Demo1" bad1.yaml
> java.lang.NullPointerException
> at yaml.Demo1.main(Demo1.java:20)
>
A NullPointerException error is _really_ bad here. Ideally, the
syntax should allow some type of a syntax error exception or error
along with the suspected line number causing the error.
Failing that, even raising an exception saying that you could not find
an expected element (and including the missing element name)
would be vastly better than just throwing a NullPointerException.
Is the above the acceptable quality of error reporting these days ?
Simon.
--
Simon Clubley, clubley at remove_me.eisner.decus.org-Earth.UFP
Walking destinations on a map are further away than they appear.
More information about the Info-vax
mailing list