You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+85-1
Original file line number
Diff line number
Diff line change
@@ -62,6 +62,8 @@ Options:
62
62
-j Output as formatted JSON.
63
63
-omitRootBraces
64
64
Omit braces at the root.
65
+
-preserveKeyOrder
66
+
Preserve key order in objects/maps.
65
67
-quoteAlways
66
68
Always quote string values.
67
69
-v
@@ -184,7 +186,7 @@ func main() {
184
186
185
187
## Comments on struct fields
186
188
187
-
By using key `comment` in struct field tags you can specify comments to be written on one or more lines preceding the struct field in the Hjson output.
189
+
By using key `comment` in struct field tags you can specify comments to be written on one or more lines preceding the struct field in the Hjson output. Another way to output comments is to use *hjson.Node* structs, more on than later.
188
190
189
191
```go
190
192
@@ -229,6 +231,88 @@ Output:
229
231
}
230
232
```
231
233
234
+
## Read and write comments
235
+
236
+
The only way to read comments from Hjson input is to use a destination variable of type *hjson.Node* or **hjson.Node*. The *hjson.Node* must be the root destination, it won't work if you create a field of type *hjson.Node* in some other struct and use that struct as destination. An *hjson.Node* struct is simply a wrapper for a value and comments stored in an *hjson.Comments* struct. It also has several convenience functions, for example *AtIndex()* or *SetKey()* that can be used when you know that the node contains a value of type `[]interface{}` or **hjson.OrderedMap*. All of the elements in `[]interface{}` or **hjson.OrderedMap* will be of type **hjson.Node* in trees created by *hjson.Unmarshal*, but the *hjson.Node* convenience functions unpack the actual values from them.
237
+
238
+
When *hjson.Node* or **hjson.Node* is used as destination for Hjson unmarshal the output will be a tree of **hjson.Node* where all of the values contained in tree nodes will be of these types:
These are just the types used by Hjson unmarshal and the convenience functions, you are free to assign any type of values to nodes in your own code.
249
+
250
+
The comments will contain all whitespace chars too (including line feeds) so that an Hjson document can be read and written without altering the layout. This can be disabled by setting the decoding option *WhitespaceAsComments* to `false`.
251
+
252
+
```go
253
+
254
+
package main
255
+
256
+
import (
257
+
"fmt"
258
+
259
+
"github.com/hjson/hjson-go/v4"
260
+
)
261
+
262
+
funcmain() {
263
+
// Now let's look at decoding Hjson data into hjson.Node.
Hjson allows quoteless strings. But if a value is a valid number, boolean or `null` then it will be unmarshalled into that type instead of a string when unmarshalling into `interface{}`. This can lead to unintended consequences if the creator of an Hjson file meant to write a string but didn't think of that the quoteless string they wrote also was a valid number.
0 commit comments