hjson/hjson-cpp

Library loses commentary

JanPlitzkow opened this issue · 6 comments

Hello together,
if I use Hjson::Unmarshal and directly Hjson::Marshal on the output, existing commentary will be lost / deleted in resulting output. Is this intended behaviour?
In hjson-cs I'm able to preserve commentary by using LoadWsc and saving with keepWsc == true. Do I miss something or is this impossible with hjson-cpp?
Greatings,
Jan

Hi Jan, that is the expected behavior for the C++ implementation of hjson. The comment-preserving feature has not yet been implemented in C++.

If one would implement this feature (I might), do you have any hints / comments. I'm especially thinking about these topics:

Assignment of comments

One can have different locations of comments:

{
   // comment before A
   keyA : 3 // comment after A
   /*
   comment before B
   */
   keyB : "test"  /* comment after B*/
}

In general it won't be possible to know, which key a comment is refering to. But my most intuitive choice would be the one implied above. In hjson-c# however it is different:

{
   // general comment
   keyA : 3 // comment refering A
   /*
   comment still refering A
   */
   keyB : "test"  /* comment refering B*/
}

IMHO this is not as intuitive. Consider the situation, where keyA is wiped and the hjson-File rewritten afterwards. Intuitivly I would expect:

{
   /*
   comment before B
   */
   keyB : "test"  /* comment after B*/
}

instead of

{
   // general comment
   keyB : "test"  /* comment refering B*/
}

Do you have another view on this topic?

Comment representation

Considering my thoughts above, I would add two variables into ValueImpl

class Value::ValueImpl {
public:
   std::string commentBefore;
   std::string commentBehind;
   Type type;
   ...
}

and fill / use these in hjson_encode and hjson_decode. I haven't figured the correct places there yet...

Access to comments

I would provide some functions to access (read / set) the corresponding comments via public interface for Value. I know, that this is probably not needed in general, but it wouldn't hurt anyone. Would it? In my case I would like to set default commentary for some new values my software creates in an existing hjson file.

I think this feature would be a good addition to hjson-cpp! I agree with you on the comment placement, I would also write comments on the line before the key. But it would not be good to have different behavior in hjson-cpp compared to for example hjson-c#. We should strive to have a single definition of hjson that is valid for all implementations. @laktak what do you think?

The comment placement is more of a concern for hjson-cpp than for hjson-c#, because hjson-cpp does reorder map-entries alphabetically. So by reading and rewriting a Hjson file one would otherwise mess up the commentary with a much higher probability.
In hjson-c# no reordering accurs and new entries are always inserted to the end. However I personally prefer hjson-cpp behaviour, because it fabricates identically output independent of the order of key-value insertion. Thereby it's much easier for a human to find the same entry accross different files.

Oh I had not considered that. Then I agree with you about the comments.

Fixed in #36