Cope with unit type in JSON (#262)

This commit is contained in:
Patrick Stevens
2024-09-15 15:37:50 +01:00
committed by GitHub
parent e22525c200
commit 9a3ebbf28f
7 changed files with 29 additions and 1 deletions

View File

@@ -210,6 +210,8 @@ module JsonRecordTypeWithBothJsonSerializeExtension =
|> (fun field -> field.ToString "o" |> System.Text.Json.Nodes.JsonValue.Create<string>)) |> (fun field -> field.ToString "o" |> System.Text.Json.Nodes.JsonValue.Create<string>))
) )
node.Add ("unit", (input.Unit |> (fun value -> System.Text.Json.Nodes.JsonObject ())))
node :> _ node :> _
namespace ConsumePlugin namespace ConsumePlugin
@@ -478,6 +480,8 @@ module JsonRecordTypeWithBothJsonParseExtension =
/// Parse from a JSON node. /// Parse from a JSON node.
static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : JsonRecordTypeWithBoth = static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : JsonRecordTypeWithBoth =
let arg_21 = ()
let arg_20 = let arg_20 =
(match node.["timestamp"] with (match node.["timestamp"] with
| null -> | null ->
@@ -759,6 +763,7 @@ module JsonRecordTypeWithBothJsonParseExtension =
IntMeasureNullable = arg_18 IntMeasureNullable = arg_18
Enum = arg_19 Enum = arg_19
Timestamp = arg_20 Timestamp = arg_20
Unit = arg_21
} }
namespace ConsumePlugin namespace ConsumePlugin

View File

@@ -50,6 +50,7 @@ type JsonRecordTypeWithBoth =
IntMeasureNullable : int<measure> Nullable IntMeasureNullable : int<measure> Nullable
Enum : SomeEnum Enum : SomeEnum
Timestamp : DateTimeOffset Timestamp : DateTimeOffset
Unit : unit
} }
[<WoofWare.Myriad.Plugins.JsonSerialize true>] [<WoofWare.Myriad.Plugins.JsonSerialize true>]

View File

@@ -117,6 +117,7 @@ module TestJsonSerde =
IntMeasureNullable = intMeasureNullable IntMeasureNullable = intMeasureNullable
Enum = enum<SomeEnum> someEnum Enum = enum<SomeEnum> someEnum
Timestamp = timestamp Timestamp = timestamp
Unit = ()
} }
} }
@@ -168,6 +169,7 @@ module TestJsonSerde =
IntMeasureNullable = Nullable -883<measure> IntMeasureNullable = Nullable -883<measure>
Enum = enum<SomeEnum> 1 Enum = enum<SomeEnum> 1
Timestamp = DateTimeOffset (2024, 07, 01, 17, 54, 00, TimeSpan.FromHours 1.0) Timestamp = DateTimeOffset (2024, 07, 01, 17, 54, 00, TimeSpan.FromHours 1.0)
Unit = ()
} }
let expected = let expected =
@@ -198,7 +200,8 @@ module TestJsonSerde =
"intMeasureOption": 981, "intMeasureOption": 981,
"intMeasureNullable": -883, "intMeasureNullable": -883,
"enum": 1, "enum": 1,
"timestamp": "2024-07-01T17:54:00.0000000\u002B01:00" "timestamp": "2024-07-01T17:54:00.0000000\u002B01:00",
"unit": {}
} }
""" """
|> fun s -> s.ToCharArray () |> fun s -> s.ToCharArray ()

View File

@@ -280,6 +280,7 @@ module internal JsonParseGenerator =
parseNumberType options propertyName node primType parseNumberType options propertyName node primType
|> SynExpr.pipeThroughFunction (Measure.getLanguagePrimitivesMeasure primType) |> SynExpr.pipeThroughFunction (Measure.getLanguagePrimitivesMeasure primType)
| JsonNode -> node | JsonNode -> node
| Unit -> SynExpr.CreateConst ()
| _ -> | _ ->
// Let's just hope that we've also got our own type annotation! // Let's just hope that we've also got our own type annotation!
let typeName = let typeName =

View File

@@ -147,6 +147,12 @@ module internal JsonSerializeGenerator =
|> SynExpr.createLambda "field" |> SynExpr.createLambda "field"
|> fun e -> e, false |> fun e -> e, false
| JsonNode -> SynExpr.createIdent "id", true | JsonNode -> SynExpr.createIdent "id", true
| Unit ->
SynExpr.createLambda
"value"
(SynExpr.createLongIdent [ "System" ; "Text" ; "Json" ; "Nodes" ; "JsonObject" ]
|> SynExpr.applyTo (SynExpr.CreateConst ())),
false
| _ -> | _ ->
// {type}.toJsonNode // {type}.toJsonNode
let typeName = let typeName =

View File

@@ -16,6 +16,7 @@ module internal Ident =
let createSanitisedParamName (s : string) = let createSanitisedParamName (s : string) =
match s with match s with
| "type" -> create "type'" | "type" -> create "type'"
| "private" -> create "private'"
| _ -> | _ ->
let result = StringBuilder () let result = StringBuilder ()

View File

@@ -193,6 +193,17 @@ module internal SynTypePatterns =
| _ -> None | _ -> None
| _ -> None | _ -> None
let (|Unit|_|) (fieldType : SynType) : unit option =
match fieldType with
| SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) ->
match ident |> List.map (fun i -> i.idText.ToLowerInvariant ()) with
| [ "microsoft" ; "fsharp" ; "core" ; "unit" ]
| [ "fsharp" ; "core" ; "unit" ]
| [ "core" ; "unit" ]
| [ "unit" ] -> Some ()
| _ -> None
| _ -> None
let (|DateOnly|_|) (fieldType : SynType) = let (|DateOnly|_|) (fieldType : SynType) =
match fieldType with match fieldType with
| SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) -> | SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) ->