-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-47011: Extended JSON #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,260 @@ | ||||||||||
| .. _golang-extended-json: | ||||||||||
|
|
||||||||||
| =================================== | ||||||||||
| Document Data Format: Extended JSON | ||||||||||
| =================================== | ||||||||||
|
|
||||||||||
| .. contents:: On this page | ||||||||||
| :local: | ||||||||||
| :backlinks: none | ||||||||||
| :depth: 2 | ||||||||||
| :class: singlecol | ||||||||||
|
|
||||||||||
| .. facet:: | ||||||||||
| :name: genre | ||||||||||
| :values: reference | ||||||||||
|
|
||||||||||
| .. meta:: | ||||||||||
| :keywords: code example, bson, relaxed, canonical, legacy | ||||||||||
|
|
||||||||||
| Overview | ||||||||||
| -------- | ||||||||||
|
|
||||||||||
| JSON is a data format that represents the values of objects, arrays, numbers, | ||||||||||
| strings, booleans, and nulls. The **Extended JSON** format defines a reserved | ||||||||||
| set of keys prefixed with the ``$`` character to represent field type | ||||||||||
| information that directly corresponds to each type in BSON, the format | ||||||||||
| that MongoDB uses to store data. | ||||||||||
|
|
||||||||||
| Extended JSON Formats | ||||||||||
| --------------------- | ||||||||||
|
|
||||||||||
| MongoDB Extended JSON features different string formats to represent BSON data. | ||||||||||
| Each of the formats conforms to the `JSON RFC | ||||||||||
| <https://datatracker.ietf.org/doc/html/rfc8259>`__ and meets specific | ||||||||||
| use cases. | ||||||||||
|
|
||||||||||
| The **extended** format, also known as the **canonical** | ||||||||||
|
||||||||||
| The **extended** format, also known as the **canonical** | |
| The **Extended** format, also known as the **canonical** |
S: Since it's capitalized in the table.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| bidirectional conversion without loss of information. The **Relaxed | |
| mode** format is more concise and closer to ordinary JSON, but does not | |
| bidirectional conversion without loss of information. The **Relaxed | |
| Mode** format is more concise and closer to ordinary JSON, but does not |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Use a consistent pattern for the print in L40, L58, and L67. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| ) | ||
|
|
||
| // begin-person-struct | ||
| type Person struct { | ||
| ID bson.ObjectID `bson:"_id"` | ||
| Name string | ||
| Age int | ||
| Birthday bson.DateTime | ||
| Address Address | ||
| Hobbies []string | ||
| } | ||
|
|
||
| type Address struct { | ||
| Street string | ||
| City string | ||
| State string | ||
| } | ||
|
|
||
| // end-person-struct | ||
|
|
||
| func main() { | ||
|
|
||
| // begin-unmarshal | ||
| var extJsonString = "{\"_id\":{\"$oid\":\"578f6fa2df35c7fbdbaed8c5\"},\"name\":\"Liana Ruiz\",\"age\":46,\"birthday\":{\"$date\":\"1988-01-15T00:00:00Z\"},\"address\":{\"street\":\"500 Slippery Rock Road\",\"city\":\"Round Rock\",\"state\":\"AR\"},\"hobbies\":[\"cycling\", \"baking\"]}" | ||
|
|
||
| var person Person | ||
| err := bson.UnmarshalExtJSON([]byte(extJsonString), false, &person) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Println("Go Struct Representation:\n", person) | ||
| // end-unmarshal | ||
|
|
||
| // begin-marshal | ||
| var person = Person{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ID: bson.NewObjectID(), | ||
| Name: "Matteo Carisi", | ||
| Age: 49, | ||
| Birthday: bson.NewDateTimeFromTime(time.Date(1975, 10, 30, 0, 0, 0, 0, time.UTC)), | ||
| Address: Address{Street: "14a Corner Court", City: "Springfield", State: "IL"}, | ||
| Hobbies: []string{"cooking", "birdwatching"}, | ||
| } | ||
|
|
||
| newExtJsonString, err := bson.MarshalExtJSON(person, false, true) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Printf("Extended JSON Representation:\n%s\n", newExtJsonString) | ||
| // end-marshal | ||
|
|
||
| // begin-marshal-fmt | ||
| formattedString, err := bson.MarshalExtJSONIndent(person, false, true, "", " ") | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Printf("%s\n", formattedString) | ||
| // end-marshal-fmt | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: Can you mark this page with no index, so it is not searchable?