Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const validOptions = {
idleTimeout: 1,
Promise: 1,
queueLimit: 1,
waitForConnections: 1
waitForConnections: 1,
jsonStrings: 1
};

class ConnectionConfig {
Expand Down Expand Up @@ -180,6 +181,7 @@ class ConnectionConfig {
};
this.connectAttributes = { ...defaultConnectAttributes, ...(options.connectAttributes || {})};
this.maxPreparedStatements = options.maxPreparedStatements || 16000;
this.jsonStrings = options.jsonStrings || false;
}

static mergeFlags(default_flags, user_flags) {
Expand Down
2 changes: 1 addition & 1 deletion lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function readCodeFor(field, config, options, fieldNum) {
// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
// see https://github.com/sidorares/node-mysql2/issues/409
return 'JSON.parse(packet.readLengthCodedString("utf8"));';
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"));';
case Types.LONGLONG:
if (!supportBigNumbers) {
return unsigned
Expand Down
2 changes: 1 addition & 1 deletion lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
// see https://github.com/sidorares/node-mysql2/issues/409
return 'JSON.parse(packet.readLengthCodedString("utf8"))';
return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"))';
default:
if (charset === Charsets.BINARY) {
return 'packet.readLengthCodedBuffer()';
Expand Down
7 changes: 7 additions & 0 deletions typings/mysql/lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ export interface ConnectionOptions {
authPlugins?: {
[key: string]: AuthPlugin;
};

/**
* Force JSON to be returned as string
*
* (Default: false)
*/
jsonStrings?: boolean;
}

declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/documentation/00-index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Please check these [examples](/docs/examples) for **MySQL2**.
This option could lose precision on the number as Javascript Number is a Float!
:::

- By default, the `JSON` type is always returned parsed into an object. However, you can modify this behavior by specifying the following configuration:

```js
{
jsonStrings: true,
}
```

<hr />

## Other Resources
Expand Down