Skip to content

Commit 80089f0

Browse files
aaraniknocte
authored andcommitted
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 73b5b56 commit 80089f0

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/DotNetLightning.Core/Utils/Primitives.fs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open NBitcoin.Crypto
66
open System
77
open System.Net
88
open System.Linq
9-
9+
open System.ComponentModel
1010
open System.Diagnostics
1111
open DotNetLightning.Core.Utils.Extensions
1212

@@ -395,12 +395,29 @@ module Primitives =
395395
#if !NoDUsAsStructs
396396
[<Struct>]
397397
#endif
398+
[<TypeConverter(typeof<HTLCIdToStringTypeConverter>)>]
398399
type HTLCId = | HTLCId of uint64 with
399400
static member Zero = HTLCId(0UL)
400401
member this.Value = let (HTLCId v) = this in v
401402

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

405+
override self.ToString() =
406+
self.Value.ToString()
407+
408+
and private HTLCIdToStringTypeConverter() =
409+
inherit TypeConverter()
410+
override __.CanConvertFrom(_context, sourceType) =
411+
sourceType = typeof<string>
412+
override __.ConvertFrom(_context, _culture, value) =
413+
match value with
414+
| :? string as stringValue ->
415+
stringValue
416+
|> UInt64.Parse
417+
|> HTLCId.HTLCId
418+
|> box
419+
| _ -> failwith "CanConvertFrom should not return true for any other types except string so this never happens"
420+
404421
#if !NoDUsAsStructs
405422
[<Struct>]
406423
#endif

0 commit comments

Comments
 (0)