Class JsonHandler
A type handler for the PostgreSQL json and jsonb data type.
Implements
Inherited Members
Namespace: Npgsql.Internal.TypeHandlers
Assembly: Npgsql.dll
Syntax
public class JsonHandler : NpgsqlTypeHandler<string>, INpgsqlTypeHandler<string>
Remarks
See https://www.postgresql.org/docs/current/datatype-json.html.
The type handler API allows customizing Npgsql's behavior in powerful ways. However, although it is public, it should be considered somewhat unstable, and may change in breaking ways, including in non-major releases. Use it at your own risk.
Constructors
JsonHandler(PostgresType, Encoding, Boolean, Nullable<JsonSerializerOptions>)
Declaration
public JsonHandler(PostgresType postgresType, Encoding encoding, bool isJsonb, JsonSerializerOptions? serializerOptions = null)
Parameters
Type | Name | Description |
---|---|---|
PostgresType | postgresType | |
Encoding | encoding | |
Boolean | isJsonb | |
Nullable<JsonSerializerOptions> | serializerOptions |
Methods
GetTextReader(Stream)
Declaration
public TextReader GetTextReader(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream |
Returns
Type | Description |
---|---|
TextReader |
Read(NpgsqlReadBuffer, Int32, Boolean, FieldDescription)
Reads a value of type TDefault
with the given length from the provided buffer,
using either sync or async I/O.
Declaration
public override ValueTask<string> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
Parameters
Type | Name | Description |
---|---|---|
NpgsqlReadBuffer | buf | The buffer from which to read. |
Int32 | len | The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed. |
Boolean | async | If I/O is required to read the full length of the value, whether it should be performed synchronously or asynchronously. |
FieldDescription | fieldDescription | Additional PostgreSQL information about the type, such as the length in varchar(30). |
Returns
Type | Description |
---|---|
ValueTask<String> | The fully-read value. |
Overrides
ReadCustom<T>(NpgsqlReadBuffer, Int32, Boolean, FieldDescription)
Declaration
protected override async ValueTask<T> ReadCustom<T>(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription fieldDescription = null)
Parameters
Type | Name | Description |
---|---|---|
NpgsqlReadBuffer | buf | |
Int32 | byteLen | |
Boolean | async | |
FieldDescription | fieldDescription |
Returns
Type | Description |
---|---|
ValueTask<T> |
Type Parameters
Name | Description |
---|---|
T |
Overrides
ValidateAndGetLength(String, ref NpgsqlLengthCache, NpgsqlParameter)
Called to validate and get the length of a value of a generic NpgsqlParameter<T>.
Declaration
public override int ValidateAndGetLength(string value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type | Name | Description |
---|---|---|
String | value | |
NpgsqlLengthCache | lengthCache | |
NpgsqlParameter | parameter |
Returns
Type | Description |
---|---|
Int32 |
Overrides
ValidateAndGetLengthCustom<TAny>(TAny, ref NpgsqlLengthCache, NpgsqlParameter)
Declaration
protected override int ValidateAndGetLengthCustom<TAny>(TAny value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type | Name | Description |
---|---|---|
TAny | value | |
NpgsqlLengthCache | lengthCache | |
NpgsqlParameter | parameter |
Returns
Type | Description |
---|---|
Int32 |
Type Parameters
Name | Description |
---|---|
TAny |
Overrides
ValidateObjectAndGetLength(Object, ref NpgsqlLengthCache, NpgsqlParameter)
Responsible for validating that a value represents a value of the correct and which can be written for PostgreSQL - if the value cannot be written for any reason, an exception shold be thrown. Also returns the byte length needed to write the value.
Declaration
public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type | Name | Description |
---|---|---|
Object | value | The value to be written to PostgreSQL |
NpgsqlLengthCache | lengthCache | If the byte length calculation is costly (e.g. for UTF-8 strings), its result can be stored in the length cache to be reused in the writing process, preventing recalculation. |
NpgsqlParameter | parameter | The NpgsqlParameter instance where this value resides. Can be used to access additional information relevant to the write process (e.g. Size). |
Returns
Type | Description |
---|---|
Int32 | The number of bytes required to write the value. |
Overrides
Write(String, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean, CancellationToken)
Called to write the value of a generic NpgsqlParameter<T>.
Declaration
public override async Task Write(string value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
String | value | |
NpgsqlWriteBuffer | buf | |
NpgsqlLengthCache | lengthCache | |
NpgsqlParameter | parameter | |
Boolean | async | |
CancellationToken | cancellationToken |
Returns
Type | Description |
---|---|
Task |
Overrides
WriteObjectWithLength(Object, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean, CancellationToken)
Writes a value to the provided buffer, using either sync or async I/O.
Declaration
public override async Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Object | value | The value to write. |
NpgsqlWriteBuffer | buf | The buffer to which to write. |
NpgsqlLengthCache | lengthCache | |
NpgsqlParameter | parameter | The NpgsqlParameter instance where this value resides. Can be used to access additional information relevant to the write process (e.g. Size). |
Boolean | async | If I/O is required to read the full length of the value, whether it should be performed synchronously or asynchronously. |
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is None. |
Returns
Type | Description |
---|---|
Task |
Overrides
WriteWithLengthCustom<TAny>(TAny, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean, CancellationToken)
Typically does not need to be overridden by type handlers, but may be needed in some cases (e.g. ArrayHandler. Note that this method assumes it can write 4 bytes of length (already verified by WriteWithLength<TAny>(TAny, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean, CancellationToken)).
Declaration
protected override async Task WriteWithLengthCustom<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
TAny | value | |
NpgsqlWriteBuffer | buf | |
NpgsqlLengthCache | lengthCache | |
NpgsqlParameter | parameter | |
Boolean | async | |
CancellationToken | cancellationToken |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
TAny |