Skip to content

Commit 554c557

Browse files
committed
HtlcId type converter to fix json serialization
Newtonsoft.Json needs types used as key in dictionaries to be convertible to string so we need to create a type converter to HtlcId.
1 parent ae08358 commit 554c557

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/DotNetLightning.Core/Utils/Primitives.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ open DotNetLightning.Core.Utils.Extensions
1212

1313
open ResultUtils
1414
open ResultUtils.Portability
15+
open System.ComponentModel
1516

1617
[<AutoOpen>]
1718
module Primitives =
@@ -395,12 +396,29 @@ module Primitives =
395396
#if !NoDUsAsStructs
396397
[<Struct>]
397398
#endif
399+
[<TypeConverter(typeof<HTLCIdToStringTypeConverter>)>]
398400
type HTLCId = | HTLCId of uint64 with
399401
static member Zero = HTLCId(0UL)
400402
member x.Value = let (HTLCId v) = x in v
401403

402404
static member (+) (a: HTLCId, b: uint64) = (a.Value + b) |> HTLCId
403405

406+
override self.ToString() =
407+
self.Value.ToString()
408+
409+
and private HTLCIdToStringTypeConverter() =
410+
inherit TypeConverter()
411+
override __.CanConvertFrom(_context, sourceType) =
412+
sourceType = typeof<string>
413+
override __.ConvertFrom(_context, _culture, value) =
414+
match value with
415+
| :? string as stringValue ->
416+
stringValue
417+
|> UInt64.Parse
418+
|> HTLCId.HTLCId
419+
|> box
420+
| _ -> failwith "should not happen"
421+
404422
#if !NoDUsAsStructs
405423
[<Struct>]
406424
#endif

0 commit comments

Comments
 (0)