Skip to content

Commit 4ceff22

Browse files
authored
+ OfJson / ToJson for JsonObject / JsonValue (#71)
1 parent 6f8cef4 commit 4ceff22

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Fleece/Fleece.fs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ module SystemTextJson =
423423
| JObject x -> Success (dictAsJsonObject x)
424424
| a -> Decode.Fail.objExpected a
425425

426+
static member jsonOfJsonObject o = JObject o
427+
426428
#endif
427429

428430
#if NEWTONSOFT
@@ -442,8 +444,9 @@ module SystemTextJson =
442444
match o.Type with
443445
| JTokenType.Object -> Success (o :?> JObject)
444446
| _ -> Decode.Fail.objExpected o
445-
446447

448+
static member jsonOfJsonObject o = o :> JToken
449+
447450
#endif
448451

449452
#if SYSTEMJSON
@@ -463,6 +466,8 @@ module SystemTextJson =
463466
| :? JsonObject as x -> Success x
464467
| _ -> Decode.Fail.objExpected o
465468

469+
static member jsonOfJsonObject (o: JsonObject) = o :> JsonValue
470+
466471
#endif
467472

468473
#if SYSTEMTEXTJSON
@@ -496,6 +501,8 @@ module SystemTextJson =
496501
| JObject x -> Success (dictAsJsonObject x)
497502
| a -> Decode.Fail.objExpected a
498503

504+
static member jsonOfJsonObject (o: JsonObject) = JObject o
505+
499506
#endif
500507

501508
open Helpers
@@ -905,6 +912,7 @@ module SystemTextJson =
905912
static member inline OfJson (_: 'R, _: Default2) = fun js -> (^R : (static member OfJson: JsonValue -> ^R ParseResult) js) : ^R ParseResult
906913

907914
static member OfJson (_: JsonObject, _: Default1) = JsonHelpers.jsonObjectOfJson
915+
static member OfJson (_: JsonValue, _: Default1) = Success
908916

909917

910918
/// Maps Json to a type
@@ -1043,6 +1051,9 @@ module SystemTextJson =
10431051
static member inline ToJson (t: 'T, _: Default3) = (^T : (static member ToJSON : ^T -> JsonValue) t)
10441052
static member inline ToJson (t: 'T, _: Default2) = (^T : (static member ToJson : ^T -> JsonValue) t)
10451053

1054+
static member ToJson (t: JsonObject, _: Default1) = JsonHelpers.jsonOfJsonObject t
1055+
static member ToJson (t: JsonValue , _: Default1) = t
1056+
10461057
/// Maps a value to Json
10471058
let inline toJson (x: 't) : JsonValue = ToJson.Invoke x
10481059

test/Tests/Tests.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,15 @@ let tests = [
444444

445445
}
446446

447+
test "Map roundtrips as JsonValue and JsonObject" {
448+
let p = Map.ofList [ ("1", "one"); ("2", "two")]
449+
let x = p |> toJson |> toJson |> ofJson<Map<string,string>>
450+
let (Ok o) = p |> toJson |> ofJson<JsonObject>
451+
let y = o |> toJson |> toJson |> ofJson<Map<string,string>>
452+
Assert.Equal("roundtrip through JsonValue", Option.ofResult x, Some p)
453+
Assert.Equal("roundtrip through JsonObject", Option.ofResult y, Some p)
454+
}
455+
447456
test "Map with null key" {
448457
let p: Map<string, _> = Map.ofList [null, "a"]
449458
Assert.JSON("{}", p)

0 commit comments

Comments
 (0)