Show / Hide Table of Contents

Class JsonHandler

A type handler for the PostgreSQL json and jsonb data type.

Inheritance
System.Object
NpgsqlTypeHandler
NpgsqlTypeHandler<System.String>
JsonHandler
Implements
INpgsqlTypeHandler<System.String>
Inherited Members
NpgsqlTypeHandler<String>.Read<TAny>(NpgsqlReadBuffer, Int32, FieldDescription)
NpgsqlTypeHandler<String>.CreateArrayHandler(PostgresArrayType)
NpgsqlTypeHandler<String>.CreateRangeHandler(PostgresRangeType)
NpgsqlTypeHandler.CreateConversionButNoParamException(Type)
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Npgsql.TypeHandlers
Assembly: Npgsql.dll
Syntax
public class JsonHandler : NpgsqlTypeHandler<string>, INpgsqlTypeHandler<string>, ITextReaderHandler
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, NpgsqlConnection, Boolean, Nullable<JsonSerializerOptions>)

Declaration
protected JsonHandler(PostgresType postgresType, NpgsqlConnection connection, bool isJsonb, JsonSerializerOptions? serializerOptions = default(JsonSerializerOptions? ))
Parameters
Type Name Description
PostgresType postgresType
NpgsqlConnection connection
System.Boolean isJsonb
System.Nullable<JsonSerializerOptions> serializerOptions

Methods

GetTextReader(Stream)

Declaration
public TextReader GetTextReader(Stream stream)
Parameters
Type Name Description
System.IO.Stream stream
Returns
Type Description
System.IO.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.

System.Int32 len

The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.

System.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<System.String>

The fully-read value.

Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.Read(Npgsql.NpgsqlReadBuffer, System.Int32, System.Boolean, Npgsql.BackendMessages.FieldDescription)

Read<T>(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. Type handlers typically don't need to override this - override Read(NpgsqlReadBuffer, Int32, Boolean, FieldDescription) - but may do so in exceptional cases where reading of arbitrary types is required.

Declaration
protected override ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription fieldDescription = null)
Parameters
Type Name Description
NpgsqlReadBuffer buf

The buffer from which to read.

System.Int32 byteLen
System.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<T>

The fully-read value.

Type Parameters
Name Description
T
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.Read<TAny>(Npgsql.NpgsqlReadBuffer, System.Int32, System.Boolean, Npgsql.BackendMessages.FieldDescription)

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
System.String value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.ValidateAndGetLength(System.String, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter)

ValidateAndGetLength<TAny>(TAny, ref NpgsqlLengthCache, NpgsqlParameter)

Called to validate and get the length of a value of an arbitrary type. Checks that the current handler supports that type and throws an exception otherwise.

Declaration
protected override int ValidateAndGetLength<TAny>(TAny value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type Name Description
TAny value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32
Type Parameters
Name Description
TAny
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.ValidateAndGetLength<TAny>(TAny, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter)

ValidateObjectAndGetLength(Object, ref NpgsqlLengthCache, NpgsqlParameter)

Called to validate and get the length of a value of a non-generic NpgsqlParameter. Type handlers generally don't need to override this.

Declaration
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type Name Description
System.Object value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.ValidateObjectAndGetLength(System.Object, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter)

Write(String, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean)

Called to write the value of a generic NpgsqlParameter<T>.

Declaration
public override Task Write(string value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
System.String value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.Write(System.String, Npgsql.NpgsqlWriteBuffer, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter, System.Boolean)

WriteObjectWithLength(Object, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean)

Called to write the value of a non-generic NpgsqlParameter. Type handlers generally don't need to override this.

Declaration
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
System.Object value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.WriteObjectWithLength(System.Object, Npgsql.NpgsqlWriteBuffer, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter, System.Boolean)

WriteWithLength<TAny>(TAny, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean)

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 Npgsql.TypeHandling.NpgsqlTypeHandler`1.WriteWithLengthInternal``1(``0,Npgsql.NpgsqlWriteBuffer,Npgsql.NpgsqlLengthCache,Npgsql.NpgsqlParameter,System.Boolean)).

Declaration
protected override Task WriteWithLength<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
TAny value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task
Type Parameters
Name Description
TAny
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler<System.String>.WriteWithLength<TAny>(TAny, Npgsql.NpgsqlWriteBuffer, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter, System.Boolean)

Implements

INpgsqlTypeHandler<T>
In This Article
Back to top Generated by DocFX