File: extended-json.md

package info (click to toggle)
mongo-java-driver 3.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 16,112 kB
  • sloc: java: 102,506; xml: 395; sh: 43; makefile: 4
file content (120 lines) | stat: -rw-r--r-- 5,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
+++
date = "2015-03-19T14:27:51-04:00"
title = "Extended JSON"
[menu.main]
  parent = "BSON"
  weight = 50
  pre = "<i class='fa'></i>"
+++

## JSON

As discussed earlier, the Java driver supports reading and writing BSON documents represented as JSON values. The driver supports four 
standard variants:   

- Extended Mode: Canonical representation that avoids any loss of BSON type information. See the 
[Extended JSON specification](https://github.com/mongodb/specifications/blob/master/source/extended-json.rst) for a description of this 
mode.
- Relaxed Mode:  Relaxed representation that loses type information for BSON numeric types and uses a more human-readable representation
of BSON dates. See the 
[Extended JSON specification](https://github.com/mongodb/specifications/blob/master/source/extended-json.rst) for a description of this 
mode.
- Shell Mode: a superset of JSON that the 
[MongoDB shell](http://docs.mongodb.org/manual/tutorial/getting-started-with-the-mongo-shell/) can parse. 
- Strict Mode: Legacy representation.  Though now deprecated, this is still the default mode when writing JSON in order to avoid breaking
backwards compatibility.  This may change in a future major release of the driver.

Furthermore, the `Document`, `BsonDocument`, and `BasicDBObject` classes each provide two sets of convenience methods for this purpose:

- toJson(): a set of overloaded methods that convert an instance to a JSON string
- parse(): a set of overloaded static factory methods that convert a JSON string to an instance of the class
 
### Writing JSON

Consider the task of implementing a [mongoexport](http://docs.mongodb.org/manual/reference/program/mongoexport/)-like tool using the 
Java driver.  
    
```java
String outputFilename;                 // initialize to the path of the file to write to
MongoCollection<Document> collection;  // initialize to the collection from which you want to query

BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilename));

try {
    JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
    for (Document doc : collection.find()) {
        writer.write(doc.toJson(settings));
        writer.newLine();
    } finally{
        writer.close();
    }
}

```

The `Document.toJson()` method constructs an instance of a `JsonWriter` with its default settings, which will write in strict mode with 
no new lines or indentation.  

You can override this default behavior by using one of the overloads of `toJson()`.  As an example, consider the task of writing a
 JSON string that can be copied and pasted into the MongoDB shell:
 
```java
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yy");
Date first = fmt.parse("01/01/2014");
Date second = fmt.parse("01/01/2015");
Document doc = new Document("startDate", new Document("$gt", first).append("$lt", second)); 
System.out.println(doc.toJson(new JsonWriterSettings(JsonMode.SHELL))); 
```

This code snippet will print out MongoDB shell-compatible JSON, which can then be pasted into the shell:
 
```javascript
{ "startDate" : { "$gt" : ISODate("2014-01-01T05:00:00.000Z"), "$lt" : ISODate("2015-01-01T05:00:00.000Z") } }
```

#### Customizing the output

Often applications have specific requirements on the structure of JSON that is generated from documents stored in MongoDB, where none of
the modes described above will suffice.   For those situations a `JsonWriterSettings` instance can be customized with an application-provided 
[`Converter`]({{< apiref "org/bson/json/Converter" >}}) for each BSON type.
  
Consider a situation where an application wants to output the hex string representation of an ObjectId as a simple JSON string.  Simply 
register a custom `Converter` with `JsonWriterSettings` for the ObjectId type:

```java
        JsonWriterSettings settings = JsonWriterSettings.builder()
                                              .outputMode(JsonMode.RELAXED)
                                              .objectIdConverter((value, writer) -> writer.writeString(value.toHexString()))
                                              .build();
```

A `JsonWriter` configured with these settings will use the given `JsonMode` as the default for all BSON types, but override the mode
with any registered converters.

### Reading JSON

Consider the task of implementing a [mongoimport](http://docs.mongodb.org/manual/reference/program/mongoimport/)-like tool using the 
Java driver.  
    
```java
String inputFilename;                  // initialize to the path of the file to read from
MongoCollection<Document> collection;  // initialize to the collection to which you want to write

BufferedReader reader = new BufferedReader(new FileReader(inputFilename));

try {
    String json;

    while ((json = reader.readLine()) != null) {
        collection.insertOne(Document.parse(json));
    } 
} finally {
    reader.close();
}
```

The `Document.parse()` static factory method constructs an instance of a `JsonReader` with the given string and returns an instance of an
equivalent Document. `JsonReader` automatically detects the JSON flavor in the string, so you do not need to specify it.