-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace FsCodec.SystemTextJson | ||
|
||
open System | ||
open System.Linq.Expressions | ||
open System.Text.Json | ||
open System.Text.Json.Serialization | ||
|
||
type RejectNullConverter<'T>() = | ||
inherit System.Text.Json.Serialization.JsonConverter<'T>() | ||
|
||
static let defaultConverter = JsonSerializerOptions.Default.GetConverter(typeof<'T>) :?> JsonConverter<'T> | ||
let msg () = sprintf "Expected value, got null. When rejectNull is true you must explicitly wrap optional %s values in an 'option'" typeof<'T>.Name | ||
|
||
override _.HandleNull = true | ||
|
||
override _.Read(reader, typeToConvert, options) = | ||
if reader.TokenType = JsonTokenType.Null then msg () |> nullArg else | ||
defaultConverter.Read(&reader, typeToConvert, options) | ||
// Pretty sure the above is the correct approach (and this unsurprisingly loops, blowing the stack) | ||
// JsonSerializer.Deserialize(&reader, typeToConvert, options) :?> 'T | ||
|
||
override _.Write(writer, value, options) = | ||
if value |> box |> isNull then msg () |> nullArg | ||
defaultConverter.Write(writer, value, options) | ||
// JsonSerializer.Serialize<'T>(writer, value, options) | ||
|
||
type RejectNullConverterFactory(predicate) = | ||
inherit JsonConverterFactory() | ||
new() = | ||
RejectNullConverterFactory(fun (t: Type) -> | ||
t.IsGenericType | ||
&& let gtd = t.GetGenericTypeDefinition() in gtd = typedefof<Set<_>> || gtd = typedefof<list<_>>) | ||
override _.CanConvert(t: Type) = predicate t | ||
|
||
override _.CreateConverter(t, _options) = | ||
let openConverterType = typedefof<RejectNullConverter<_>> | ||
let constructor = openConverterType.MakeGenericType(t).GetConstructors() |> Array.head | ||
let newExpression = Expression.New(constructor) | ||
let lambda = Expression.Lambda(typeof<ConverterActivator>, newExpression) | ||
|
||
let activator = lambda.Compile() :?> ConverterActivator | ||
activator.Invoke() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
namespace FsCodec.SystemTextJson | ||
|
||
open System.Text.Json.Serialization | ||
type RejectNullStringConverter() = | ||
inherit System.Text.Json.Serialization.JsonConverter<string>() | ||
|
||
module internal Error = | ||
[<Literal>] | ||
let message = "Expected string, got null. When allowNullStrings is false you must explicitly type optional strings as 'string option'" | ||
|
||
type RejectNullStringConverter() = | ||
inherit JsonConverter<string>() | ||
|
||
override _.HandleNull = true | ||
override _.CanConvert(t) = t = typeof<string> | ||
|
||
override this.Read(reader, _typeToConvert, _options) = | ||
let value = reader.GetString() | ||
if value = null then nullArg Error.message | ||
if value = null then nullArg message | ||
value | ||
|
||
override this.Write(writer, value, _options) = | ||
if value = null then nullArg Error.message | ||
if value = null then nullArg message | ||
writer.WriteStringValue(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters