|
| 1 | +#region License |
| 2 | +// The PostgreSQL License |
| 3 | +// |
| 4 | +// Copyright (C) 2017 The Npgsql Development Team |
| 5 | +// |
| 6 | +// Permission to use, copy, modify, and distribute this software and its |
| 7 | +// documentation for any purpose, without fee, and without a written |
| 8 | +// agreement is hereby granted, provided that the above copyright notice |
| 9 | +// and this paragraph and the following two paragraphs appear in all copies. |
| 10 | +// |
| 11 | +// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY |
| 12 | +// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, |
| 13 | +// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS |
| 14 | +// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF |
| 15 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | +// |
| 17 | +// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
| 18 | +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 19 | +// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 20 | +// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS |
| 21 | +// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 22 | +#endregion |
| 23 | + |
| 24 | +using System; |
| 25 | +using System.Collections.Generic; |
| 26 | +using System.Text; |
| 27 | +using System.Threading; |
| 28 | +using System.Threading.Tasks; |
| 29 | +using Newtonsoft.Json; |
| 30 | +using Newtonsoft.Json.Linq; |
| 31 | +using Npgsql.BackendMessages; |
| 32 | +using Npgsql.PostgresTypes; |
| 33 | +using Npgsql.TypeHandling; |
| 34 | +using Npgsql.TypeMapping; |
| 35 | + |
| 36 | +namespace Npgsql.Json.NET |
| 37 | +{ |
| 38 | + class JsonHandlerFactory : NpgsqlTypeHandlerFactory |
| 39 | + { |
| 40 | + protected override NpgsqlTypeHandler Create(NpgsqlConnection conn) |
| 41 | + => new JsonHandler(conn); |
| 42 | + } |
| 43 | + |
| 44 | + class JsonHandler : Npgsql.TypeHandlers.TextHandler |
| 45 | + { |
| 46 | + public JsonHandler(NpgsqlConnection connection) |
| 47 | + : base(connection) {} |
| 48 | + |
| 49 | + protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) |
| 50 | + { |
| 51 | + var s = await base.Read<string>(buf, len, async, fieldDescription); |
| 52 | + if (typeof(T) == typeof(string)) |
| 53 | + return (T)(object)s; |
| 54 | + try |
| 55 | + { |
| 56 | + return JsonConvert.DeserializeObject<T>(s); |
| 57 | + } |
| 58 | + catch (Exception e) |
| 59 | + { |
| 60 | + throw new NpgsqlSafeReadException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected override int ValidateAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter = null) |
| 65 | + { |
| 66 | + var s = value as string; |
| 67 | + if (s == null) |
| 68 | + { |
| 69 | + s = JsonConvert.SerializeObject(value); |
| 70 | + if (parameter != null) |
| 71 | + parameter.ConvertedValue = s; |
| 72 | + } |
| 73 | + return base.ValidateAndGetLength(s, ref lengthCache, parameter); |
| 74 | + } |
| 75 | + |
| 76 | + protected override Task Write(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) |
| 77 | + { |
| 78 | + if (parameter?.ConvertedValue != null) |
| 79 | + value = parameter.ConvertedValue; |
| 80 | + var s = value as string ?? JsonConvert.SerializeObject(value); |
| 81 | + return base.Write(s, buf, lengthCache, parameter, async); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments