-
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
1 parent
0fc3cda
commit 700f055
Showing
6 changed files
with
65 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,39 @@ | ||
namespace FsCodec.NewtonsoftJson | ||
|
||
open Newtonsoft.Json | ||
open System.IO | ||
open System.Text | ||
|
||
/// Serializes to/from strings using the supplied JsonSerializerSettings | ||
type Serdes(options : JsonSerializerSettings) = | ||
// Why are we creating a serializer here instead of using JsonConvert.Serialize? | ||
// Because, under the hood, JsonConvert creates a serializer instance for *each* call | ||
// Source: | ||
// https://github.com/JamesNK/Newtonsoft.Json/blob/4dc9af66e07dea321ad101bfb379326127251a80/Src/Newtonsoft.Json/JsonConvert.cs#L817 | ||
// It's consistent with the stream implementation | ||
let serializer = JsonSerializer.Create(options) | ||
|
||
/// <summary>The <c>JsonSerializerSettings</c> used by this instance.</summary> | ||
member _.Options : JsonSerializerSettings = options | ||
|
||
/// Serializes given value to a JSON string. | ||
member _.Serialize<'T>(value : 'T) = | ||
JsonConvert.SerializeObject(value, options) | ||
member _.Serialize<'T>(value : 'T) : string = | ||
use sw = new StringWriter() | ||
use writer = new JsonTextWriter(sw) | ||
serializer.Serialize(writer, value) | ||
sw.ToString() | ||
|
||
/// Deserializes value of given type from JSON string. | ||
member x.Deserialize<'T>(json : string) : 'T = | ||
JsonConvert.DeserializeObject<'T>(json, options) | ||
member _.Deserialize<'T>(json : string) : 'T = | ||
use reader = new JsonTextReader(new StringReader(json)) | ||
serializer.Deserialize<'T>(reader) | ||
|
||
/// Serializes and writes given value to a stream. | ||
member _.SerializeToStream<'T>(value : 'T, utf8Stream : Stream) = | ||
use writer = new JsonTextWriter(new StreamWriter(utf8Stream, Encoding.UTF8), CloseOutput = false) | ||
serializer.Serialize(writer, value) | ||
|
||
/// Deserializes by reading from a stream. | ||
member x.DeserializeFromStream<'T>(utf8Stream : Stream) = | ||
use reader = new JsonTextReader(new StreamReader(utf8Stream, Encoding.UTF8)) | ||
serializer.Deserialize<'T>(reader) |
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,23 @@ | ||
#if SYSTEM_TEXT_JSON | ||
module FsCodec.SytemTextJson.Tests.StreamTests | ||
open FsCodec.SystemTextJson | ||
#else | ||
module FsCodec.NewtonsoftJson.Tests.StreamTests | ||
open FsCodec.NewtonsoftJson | ||
#endif | ||
|
||
open Xunit | ||
open System.IO | ||
open Swensen.Unquote | ||
|
||
let serdes = Serdes Options.Default | ||
|
||
type Rec = { a : int; b : string; c : string } | ||
|
||
let [<Fact>] ``Can serialize/deserialize to stream`` () = | ||
let value = { a = 10; b = "10"; c = null } | ||
use stream = new MemoryStream() | ||
serdes.SerializeToStream(value, stream) | ||
stream.Seek(0L, SeekOrigin.Begin) |> ignore | ||
let value' = serdes.DeserializeFromStream(stream) | ||
<@ value = 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